Send Contact Form not Working - php

I've followed a simple tutorial for a send contact form, however it appears to be not working. Please can someone assist please.
Form below:
<table width="400" border="0" cellspacing="1" cellpadding="0" align="center">
<tbody>
<tr>
<td>
<form action="send.php" method="post" name="form1">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tbody>
<tr>
<td width="16%">Name</td>
<td width="2%">:</td>
<td width="82%"><input id="Name" type="text" name="Name" size="50" /></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input id="customer_mail" type="text" name="customer_mail" size="50" /></td>
</tr>
<tr>
<td>Subject</td>
<td>:</td>
<td><input id="Subject" type="text" name="Subject" size="50" /></td>
</tr>
<tr>
<td>Detail</td>
<td>:</td>
<td><textarea id="detail" cols="50" name="detail" rows="4"></textarea></td>
</tr>
<tr>
<td></td>
<td></td>
<td><input type="submit" name="Submit" value="Submit" /> <input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</tbody>
</table>
</form>
</td>
</tr>
</tbody>
</table>
And here is my PHP:
<?php
$to ='kirsty.harris1985#gmail.com';
$header="from: $name <$mail_from>";
$mail_from="$customer_mail";
$Subject="$Subject";
$detail="$detail";
$send_contact=mail($to,$header,$Subject,$detail);
if($send_contact){
echo "We've recived your contact information";
} else {
echo "ERROR";
}
?>
This is the error:
Server error
The website encountered an error while retrieving http://nqmedia.co.uk/send_contact.php. It may be down for maintenance or configured incorrectly.
Here are some suggestions:
Reload this webpage later.
HTTP Error 500 (Internal Server Error): An unexpected condition was encountered while the server was attempting to fulfill the request.
And also the website is www.nqmedia.co.uk for people to see it.

Given that your inputs are named accordingly
try this:
$name = $_POST['Name'];
$mail_from = $_POST['customer_mail'];
$header = "from: $name <$mail_from>";
$Subject = $_POST['Subject'];
$detail = $_POST['detail'];

You never actually grab all the post values.
Make this your code for send.php:
<?php
$name = $_POST['Name'];
$mail_from = $_POST['customer_mail'];
$subject = $_POST['Name'];
$body = $_POST['detail'];
$to ='kirsty.harris1985#gmail.com';
$header="from: $name <$mail_from>";
$send_contact=mail($to,$Subject,$body,$header);
if($send_contact){
echo "We've received your contact information";
}
else {
echo "ERROR";
}
?>
Also, you misspelling "received" on your echo, so I fixed the spelling mistake.

Change your PHP to the following:
<?php
$to ='kirsty.harris1985#gmail.com';
$name = $_POST['name'];
$customer_mail = $_POST['customer_mail'];
$Subject = $_POST['Subject'];
$detail = $_POST['detail'];
$header="From: $name <$customer_mail>";
$send_contact=mail($to,$Subject,$detail,$header);
if($send_contact){
echo "We've recived your contact information";
} else {
echo "ERROR";
}
?>
No variable was defined properly, you needed to use $_POST to retrive the values from the form.
$mail_from (now $customer_mail) was used before it was defined.
The parameters used in the in the mail() function were used in the wrong order. Look at http://php.net/manual/en/function.mail.php
Check the action attribute on your form. It says "send.php", but the error shows "send_contact.php"

Related

Run PHP Code After Form Submission And Redirect

I want to run some php code after a form is submitted on my website. It redirects to another page to email the information, then uses header() to return to the previous page. What I would like to happen is to load a small message that shows that the message was sent successfully, but I'm not sure how to do this after being redirected from another page. I'm hoping to use php or jquery to accomplish this, and I'm including the code I'm using below. I have tried to use the session_start function to do this, but the echoed text remains on the page after the user leaves.
<form method="post" action="mail.php">
<tr>
<td class="label">Name:</td>
<td class="input"><input type="text" maxlength="40" name="name" pattern="[a-zA-Z0-9]+" title="Please enter your name." required></td>
</tr>
<tr>
<td class="label">Email:</td>
<td class="input"><input type="email" maxlength="24" name="email" title="Please enter an email address." required></td>
</tr>
<tr>
<td class="label">Subject:</td>
<td class="input"><input type="text" maxlength="24" name="subject" title="Please enter a subject." required></td>
</tr>
<tr>
<td class="label">Message:</td>
<td class="input"><textarea rows="9" maxlength="1000" name="message" title="Please enter a message." required></textarea></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Submit">
</td>
</tr>
</form>
<?php
$to = "kouen922#gmail.com";
$subject = "Message From Your Website: ".trim(htmlspecialchars($_POST["subject"]));
$message = trim(htmlspecialchars($_POST["message"]));
$headers = "Reply To: ".trim(htmlspecialchars($_POST["email"]))."" ."\r\n". "From: " .trim(htmlspecialchars($_POST["name"]))."";
mail($to,$subject,$message,$headers);
header("Location: index.php#contact");
?>
Easy.
header("Location: index.php?thankyou#contact");
Then do a conditional for isset($_GET['thankyou']) and print out a message.
Instead of using php, use Jquery(since you mentioned about it) and use its potential to send your form data, using ajax post request. In that way you can stay on same page without ever navigating to another page(because you are returning back), and also you can display the message once the post request is complete, ajax POST method returns a callback once the request is complete
Read about it here
According to your description: form page -- php email page -- back to form page, then just add the php email part into form page:
index.php:
<?php
$data = init_data();
if ($data['cmd']) {
$err = main_send($data);
}
if (!$data['cmd'] || $err) {
main_form($data, $err);
}
function init_data() {
$arr = ['cmd', 'name', 'email', 'subject', 'message'];
foreach ($arr as $k) {
$data[$k] = isset($_POST[$k]) ? trim(htmlspecialchars($_POST[$k])) : '';
}
return $data;
}
function main_send($data) {
if (!$data['name']) {
return 'What is your name?';
} elseif (!$data['email']) {
return 'What is your email?';
} elseif (!filter_var($data['email'], FILTER_VALIDATE_EMAIL)) {
return 'Your email is not valid';
} elseif (!$data['message']) {
return 'Please say something';
}
$to = 'kouen922#gmail.com';
$subject = 'Message From Your Website: ' . $data['subject'];
$message = trim(htmlspecialchars($_POST["message"]));
$headers = 'Reply To: ' . $data['email'])) . "\r\nFrom: " . $data['name']));
mail($to, $subject, $message, $headers);
echo '<p>Thank you very much for the feedback</p>';
}
function main_form($data, $err = '') {
if ($err) {
echo '<p class="err">' , $err , '</p>';
}
echo 'your original form html here ...';
}
Try this code:
form code
<div id="message"></div>
<form method="post" id="email-form" action="">
<tr>
<td class="label">Name:</td>
<td class="input"><input type="text" maxlength="40" id="name" name="name" pattern="[a-zA-Z0-9]+" title="Please enter your name." required></td>
</tr>
<tr>
<td class="label">Email:</td>
<td class="input"><input type="email" maxlength="24" id="email" name="email" title="Please enter an email address." required></td>
</tr>
<tr>
<td class="label">Subject:</td>
<td class="input"><input type="text" maxlength="24" id="subject" name="subject" title="Please enter a subject." required></td>
</tr>
<tr>
<td class="label">Message:</td>
<td class="input"><textarea rows="9" id="message" maxlength="1000" name="message" title="Please enter a message." required></textarea></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="Submit">
</td>
</tr>
</form>
email.php code
<?php
//email.php
$to = "kouen922#gmail.com";
$subject = "Message From Your Website: ".trim(htmlspecialchars($_POST["subject"]));
$message = trim(htmlspecialchars($_POST["message"]));
$headers = "Reply To: ".trim(htmlspecialchars($_POST["email"]))."" ."\r\n". "From: " .trim(htmlspecialchars($_POST["name"]))."";
mail($to,$subject,$message,$headers);
echo 'Mail was successfully send';
?>
Jquery code :
<script>
$(document).ready(function () {
$("#email-form").submit(function(event){
event.preventDefault();
var email= $('#email').val();
var name= $('#name').val();
var subject= $('#subject').val();
var messege = $('#messege').val();
$.ajax({
url: "email.php",
type:"POST",
data: 'email='+email + '&name='+name +'&subject='+subject+'&message='+message,
success: function(data){
$('#message').html(data);
setTimeout(function(){ //redirect after 5 sec
window.history.go(-1); // Simulates a back button click
},5000); // time in milli sec
}
});
});
});
</script>

Mail sending is failure in php contact us form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 6 years ago.
I am creating a contact us form, In that i am using HTML and PHP .Now my doubt is after click the submit button the details are not sending to mail,I want to know where i am mistaken on my code.
Here my HTML code for create the form
<form action="sendmail.php" method="post" >
<table width="100%" border="0"align="center"cellpadding="3"cellspacing="1">
<tr>
<td width="10%">Subject</td>
<td width="2%">: </td>
<td width="82%"><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td>Detail</td>
<td width="2%">: </td>
<td width="82%"><textarea name="detail" cols="50" rows="4"id="detail"></textarea></td>
</tr>
<tr>
<td>Email</td>
<td>: </td>
<td><input name="customer_mail" type="text" id="customer_mail" size="50"></td>
</tr>
<tr>
<td>Name</td>
<td>: </td>
<td><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td><input type="submit" name="send" value="Submit">
<input type="reset" name="submit" value="Reset">
</td>
</tr>
</table>
</form>
PHP code is below:
<?php
$subject="$subject";
$message="$detail";
$mail_from="customer_mail";
//From
$header="from: $name <$mail_from>";
//Enter your email address
$to="simon#abcinfomedia.in";
$sendmail=mail($to,$subject,$message,$header);
//check mail send to ur mail
if($sendmail){
echo"success";
}
else{
echo"Error";
}
?>
please try this:
<?php
if(isset($_POST['send']) && !empty($_POST['send'])) {
$subject = $_POST['subject'];//if you sent by post method else put manually
$message = $_POST['detail'];
$mail_from = $_POST['customer_mail'];
$header = "from: $name <$mail_from>";
//Enter your email address
$to = "simon#abcinfomedia.in";
$sendmail = mail($to, $subject, $message, $header);
//check mail send to ur mail
if ($sendmail) {
echo "success";
} else {
echo "Error";
}
}
?>
This is not working because you're not filling the variables with the data from the form by using $_POST[''].
The HTML doesn't change, but try this PHP:
<?php
$subject = $_POST['subject'];
$message = $_POST['detail'];
$mail_from = $_POST['customer_mail'];
$header = "from: $name <$mail_from>";
$to = "simon#abcinfomedia.in";
//check mail send to ur mail
if(mail($to,$subject,$message,$header)){
echo"success";
}
else{
echo"Error";
}
?>
I have defined subject, message and mail_from using the $_POST data from the form. For the if statement, I have dropped the part where you store the mail() function as a variable and just put it straight into the if.
This will simultaneously send the email and check it at the same time.
In my test, the email worked but there were flaws because some of your fields are missing and not defined, but I believe the above solves your initial problem. :)

PHP email formatted content

I am creating an email form for my website which will allow a user to create an email and appear to be sent from any email address they wish. i.e. davidcameron#downingstreet.com.
The form works fine, until I made one small change. I previously allowed the user to submit the content of the email but was finding difficulty in formatting the content (i.e. making areas bold, using a table etc).
(Form code now)
<html>
<body>
<form method="GET" action="send.php">
<p>To: <input type="text" name="to" /></p>
<p>From-Name: <input type="text" name="name" /></p>
<p>From-Email: <input type="text" name="from" /></p>
<p>Subject: <input type="text" name="subject" /></p>
<input type="submit" value="Send E-Mail" ></p>
</form>
</body>
</html>
So I thought it would be easier to submit the content myself in my 'send.php' code that actually sends the email message. This is displayed below:
<html>
<body>
<?php
$to =$_REQUEST['to'];
$subject = $_REQUEST['subject'];
$name =$_REQUEST['name'];
$from = $_REQUEST['from'];
$content = ?><font face="arial"><b>Your question has been received/b>
Your booking has been confirmed with the supplier.
Please visit the Question Portal for more information.
<table>
<tr>
<td>Question subject</td>
<td> Room</td>
</tr>
<tr>
<td>Traveller</td>
<td>Maria Smith</td>
</tr>
<td>Requester</td>
<td>Tony Smith</td>
</tr>
</table>
<br/>
</font>
<?
$header="From: $from"."<$sender_email>\r\n";
mail($to,$subject,$content,$header);
echo 'sent successfully';
?>
</body>
</html>
However, when I click submit, my server finds an error. I can only presume it is because of my HTML element as this error was not occurring previously without it.
Can anyone advise how I can fix this / if you can suggest an easier way for me to format the email content I would like?
Many thanks
Set your content value to
$content = ''
Since your $content is undefined that is why you are getting error.
Hope this will fix your issue.
Try this
<?php
$to = $_REQUEST['to'];
$subject = $_REQUEST['subject'];
$name = $_REQUEST['name'];
$from = $_REQUEST['from'];
$content =
'<span style="font-family: arial"><b>Your question has been received</b>
<br>
Your booking has been confirmed with the supplier.
Please visit the Question Portal for more information.
<table>
<tr>
<td>Question subject</td>
<td> Room</td>
</tr>
<tr>
<td>Traveller</td>
<td>Maria Smith</td>
</tr>
<td>Requester</td>
<td>Tony Smith</td>
</tr>
</table>
<br/>
</span>';
$header = "From:".$from;
$result = mail($to,$subject,$content,$header);
if(isset($result))
{
echo 'sent successfully';
}
else
{
echo 'Failed';
}
?>
Note: <font> tag not supported in HTML5
EDIT 01
Include this lines too
$header .= 'MIME-Version: 1.0';
$header .= 'Content-Type: text/html; charset="ISO-8859-1';

How to send response email to user on form submit PHP

I have a rather basic php form that submits on the same page it is called "registration.php". I have it set to collect the form data, store it in the $to, $subject and $body variables then I call the mail() method to send off an email to myself.
please see code below ( Don't worry! there is a question comin' right up! ):
<?php
ob_start(); //output buffering because I like it.
if (isset($_POST['submit'])) {
// Process the form
$message = "Thank you for registering! We will respond to your request shortly";
$name = $_POST['name'];
$email = $_POST['email'];
$address = $_POST['address'];
$comments = $_POST['userComment'];
$date = gmdate("M d Y");
$to = "myemail#outlook.com";
$subject = "Registration Submission";
$body = " Date: $date \n Registrant Name/Name's: $name \n Registrant E-mail: $email \n \n User Comments: \n $comments \n \n";
mail($to,$subject,$body);
}
?>
<form id="contactForm" name="contactForm" action="registration.php" method="post">
<table>
<tr>
<td><label for="name"><strong>Registrant Name / Names</strong></label></td>
<td><input required='required' type="text" id="name" name="name" /></td>
</tr>
<tr>
<td><label for="email"><strong>Registrant E-mail</strong></label></td>
<td><input required='required' type="email" maxlength="40" id="email" name="email" /></td>
</tr>
<tr>
<td><label for="subject"><strong>Registrant Address</strong></label></td>
<td><input required='required' type="text" maxlength="100" id="address" name="address" /></td>
</tr>
<tr>
<td>Message:</td>
<td> </td>
</tr>
<tr>
<td id="textAreaInput" colspan="2"><textarea rows="3" id="userComment" name="userComment" placeholder="Let us know your thoughts!"></textarea></td>
</tr>
</table>
<br />
<button type="submit" name="submit" id="submit">Submit</button>
</form>
What I want to know is Can I send a custom response email to the users collected $email with a custom message using just php? can I send other variables through the mail() method? like $to2, $subject2, $body2 or must they be named $to, $subject & $body when passed?
I have a working solution using PHPMailer found here:
https://github.com/PHPMailer/PHPMailer
I am just curious as to weither or not there is an easy process using PHP that wouldn't rely on a required library.
I think all you have to do is
if(mail($to,$subject,$body)){
mail($email,"Thanks!!","Thank you very much for registering!!!")
}

Trouble with a Simple PHP Contact Form [duplicate]

This question already has answers here:
PHP mail function doesn't complete sending of e-mail
(31 answers)
Closed 8 years ago.
I'm having trouble figuring out why my contact form isn't working. I'm relatively new to PHP and from what I can see, everything is linked, but when I go to send the email, nothing is sent to the designated delivery address. Any help would be appreciated!! Thank you!!
My code is below:
HTML
<table width="400" border="0" align="left" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="mail.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td width="16%">Subject</td>
<td width="2%">:</td>
<td width="82%"><input name="subject" type="text" id="subject" size="50"></td>
</tr>
<tr>
<td>Message</td>
<td>:</td>
<td><textarea name="detail" cols="50" rows="4" id="detail"></textarea></td>
</tr>
<tr>
<td>Name</td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50"></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td><input name="customer_mail" type="text" id="customer_mail" size="50"></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit"> <input type="reset" name="Submit2" value="Reset"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>
PHP
<?php
// Contact subject
$subject ="$subject";
// Details
$message="$detail";
// Mail of sender
$mail_from="$customer_mail";
// From
$header="from: $name <$mail_from>";
// Enter your email address
$to ='jordanmcowan#gmail.com';
$submit=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($submit){
echo "Thank you for contacting us at Allstar Therapies, Inc.<br /> We will be in touch shortly!";
}
else {
echo "ERROR";
}
?>
Your PHP Code seems wrong to me. This is a corrected version
<?php
// Contact subject
$subject = $_POST['subject'];
// Details
$message = $_POST['detail'];
// Mail of sender
$mail_from = $_POST['customer_mail'];
// From
$header = "from: " . $_POST['name'] . "<" . $_POST['customer_mail'] . ">";
// Enter your email address
$to = 'jordanmcowan#gmail.com';
$submit = mail($to, $subject, $message, $header);
// Check, if message sent to your email
// display message "We've recived your information"
if ($submit) {
echo "Thank you for contacting us at Allstar Therapies, Inc.<br /> We will be in touch shortly!";
} else {
echo "An error has been encountered while sending your message. We sincerely apologize and ask you to try again. If that fails as well, please contact us at XYZ-Y234-SADF";
}
The variables haven't been initialized. Please note that these symbols: "" mark the beginning and the end of a string.
These lines are wrong:
// Contact subject
$subject ="$subject";
// Details
$message="$detail";
// Mail of sender
$mail_from="$customer_mail";
// From
$header="from: $name <$mail_from>";
You're trying to assign the variables to themselves. Try:
$subject = $_POST['subject'];
$message = $_POST['detail'];
etc.
You need to use $_POST variable:
// Contact subject
$subject = isset($_POST["subject"]) ? ($_POST["subject"]) : "";
You haven't set your PHP variables (IE $subject) to your post variables yet. Try changing your PHP variables to use the $_POST variables.
<?php
// Contact subject
$subject = $_POST['subject'];
// Details
$message= $_POST['detail'];
// Mail of sender
$mail_from= $_POST['customer_mail'];
// From
$header="from: {$_POST['name']} <{$_POST['mail_from']}>";
// Enter your email address
$to ='jordanmcowan#gmail.com';
$submit=mail($to,$subject,$message,$header);
// Check, if message sent to your email
// display message "We've recived your information"
if($submit){
echo "Thank you for contacting us at Allstar Therapies, Inc.<br /> We will be in touch shortly!";
}
else {
echo "ERROR";
}
?>
You can access to POST parametars by $_POST / GET parametars by $_GET. First check if your redirect works correct.
type:
echo "welcome";
in mail.php
If redirect works the print all Post parametars:
print_r($_POST);
Last you can get one by one
$variable = $_POST['input_name_from_form'];
Hope this is solution for your problem

Categories