« Put watermark on images using PHPMake php counter »

Make contact form and send email in PHP Posted in PHP Tutorials | 300 Comments
Having a contact form on your web site is vital when you need to know what your site visitors think about your web site. We will first create a simple contact form with 3 fields - Email address, Name, Comments. I will use a table to align the 3 fields and the Send button. Create a new file and paste the code below in it. Save it as test.php and upload it to your web server. Now, you have a web page (http://www.yourdomain.com/test.php) with a simple contact form on it.

<form action="test.php" method="post"> 
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"></textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>


Then we will need the actual PHP code which will send the email when the above form is submitted. We will need to define the email that the message should be sent to ($ToEmail) and also the subject for the message that will be sent ($EmailSubject). Change youremail@site.com to your email address where the message should be sent and also add an appropriate subject for you message. The $mailheader variable is used to define the email message header. We set the From, Reply-To and Content-type fields for the message. There are some more fields that can be used but for this example we will only use these 3. Depending on your server configuration you may need to have the From and Reply-to fields be a valid email address from your server.If you have a domain name mysite.com, then you should use a valid email address such as contact@mysite.com. In this example I am sending the email using the actual email address that is submitted via the form on site. Next all the data submitted via the web form is taken from the $_POST variable and saved in the $MESSAGE_BODY variable. Using the nl2br function you will make all the new lines in your comments box appear as new lines in your email message too. Having all the needed data for our email message we will use the mail() function which will send that email for us.

<?php 
$ToEmail = 'youremail@site.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br />";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br />";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br />";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>


All we have to do now is to combine the web form and email sending code into a single page. We will use an IF statement to check if the form has been submitted and if so it will send that email and will show a "Your message was sent" message on the screen instead of the web form.

<?php 
if ($_POST["email"]<>'') {
$ToEmail = 'youremail@site.com';
$EmailSubject = 'Site contact form';
$mailheader = "From: ".$_POST["email"]."\r\n";
$mailheader .= "Reply-To: ".$_POST["email"]."\r\n";
$mailheader .= "Content-type: text/html; charset=iso-8859-1\r\n";
$MESSAGE_BODY = "Name: ".$_POST["name"]."<br />";
$MESSAGE_BODY .= "Email: ".$_POST["email"]."<br />";
$MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])."<br />";
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure");
?>
Your message was sent
<?php
} else {
?>
<form action="test.php" method="post">
<table width="400" border="0" cellspacing="2" cellpadding="0">
<tr>
<td width="29%" class="bodytext">Your name:</td>
<td width="71%"><input name="name" type="text" id="name" size="32"></td>
</tr>
<tr>
<td class="bodytext">Email address:</td>
<td><input name="email" type="text" id="email" size="32"></td>
</tr>
<tr>
<td class="bodytext">Comment:</td>
<td><textarea name="comment" cols="45" rows="6" id="comment" class="bodytext"></textarea></td>
</tr>
<tr>
<td class="bodytext"> </td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form>
<?php
};
?>

Do you know PHP / HTML / CSS / JS well?

Write tutorial on a topic you are good in and become a trusted PHP jabber! Share your knowledge with thousands of webmasters and we will reward you for your generosity by giving you bonus points which you can use as a voucher to buy any of our commercial products. Read more about our reward program.

300 Replies to "Make contact form and send email in PHP"

priti May 21, 2013 at 9:45 pm | Reply

0

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\xampp\Kroy-Bikroy.com\contact1.php on line 70
Failure



This is my problem !!! what to do man??
Holda Alin May 15, 2013 at 2:33 am | Reply

0

Thanks for tutorial
Crix May 1, 2013 at 2:03 am | Reply

0

it shows failure. please help.
Hart April 23, 2013 at 9:35 am | Reply

+1

Pls. help to correct this code for my feedback form....i try it success but nothing email come to my inbox...i used my email add...pls correct this code. ty!

<?php
/* Subject and Email Variables */

$emailSubject = 'Request Project help @ Staircase Warehouse!';
$webMaster = 'infoxxx0922@gmail.com';

/* Gathering Data Variables */

$nameField = $_POST['name'];
$telephoneField = $_POST['telephone'];
$emailField = $_POST['email'];

$body = <<<EOD
<br><hr><br>
Name: $name <br>
Telephone: $telephone <br>
Email: $email <br>
EOD;

$headers = "From: $email\r\n";
$headers .= "Content-type: text/htm\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);

/* Results rendered as HTML */

$theResults = <<<EOD
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Thank you!</title>
<!-- define some style elements-->
<style>
h1
{
font-family : Arial, Helvetica, sans-serif;
font-size : 16px;
font-weight : bold;
}
label,a,body
{
font-family : Arial, Helvetica, sans-serif;
font-size : 12px;
}

</style>
<!-- a helper script for vaidating the form-->
</head>
</head>

<body>
<h1>Thank you!</h1>
Thank you for submitting the form. We will be in touch with you very soon. <br /><br />


</body>
</html>
EOD;
echo "$theResults";

?>
Megan April 10, 2013 at 10:37 pm | Reply

0

This was super helpful! What if I wanted to add additional fields to the form? What part of the code do I need to tweak to include the extra fields in the email message?
dhananjay April 10, 2013 at 8:57 am | Reply

+1

sir,

i Apply this sourcecode...
in test.php form i have type my email address on line no 7 and in html form i hv write another email address of my friend.. then it displays "Your mail was sent.."

But problem is that email is not send to my friend..actually the email is written back to my email address in inbox.... the email is not send to multiple users plz suggest me solution..


thanx...
keeponcammin April 7, 2013 at 6:13 pm | Reply

0

Hi tried using this codes but when i click send. It will show the the codes instead of saying thank you. What seems to be the problem with what i've done?
rakibroni April 4, 2013 at 2:38 pm | Reply

0

so good.really it very help full.
Tariq khan March 26, 2013 at 7:19 pm | Reply

0

Respected admin! i m facing problem in below code.Please correct the errors. i will be very thankful to you for this kindness.
if(isset($_POST["submit"])){
$txtname = $_POST['txtname'];
$txtPN = $_POST['txtPN'];
$txtEmail = $_POST['txtEmail'];
$txtAdd = $_POST['txtAdd'];
$txtMarbleName = $_POST['txtMarbleName'];
$txtSize = $_POST['txtSize'];
$txtThickness = $_POST['txtThickness'];
$txtAmount = $_POST['txtAmount'];
$textAreaMessage = $_POST['textAreaMessage'];
$headers = "From: mohmandms.eu5.org" . "\r\n" .
"CC: somebodyelse@example.com";

$message.='
Name: ".$txtname."<br>
Phone No: ".$txtPN."<br>
Email: ".$txtEmail."<br>
Address: ".$txtAdd."<br>
Marble Name: ".$txtMarbleName."<br>
Marble Size: ".$txtSize."<br>
Thickness ".$txtThickness."<br>
Amount: ".$txtAmount."<br>
Message: ".$textAreaMessage;
ini_set('localhost','25');
mail('khan_ge68@yahoo.com','Online Order Record',$message,$headers);

echo "<script>alert('Information Send')
window.location.href='onlineOrder.html';</script>";
Junu March 21, 2013 at 8:21 pm | Reply

0

Warning: mail() [function.mail]: Failed to connect to mailserver at "localhost" port 25, verify your "SMTP" and "smtp_port" setting in php.ini or use ini_set() in C:\xampp\htdocs\xampp\Kroy-Bikroy.com\contact1.php on line 70
Failure



This is my problem !!! what to do man??


Please be polite and helpful and do not spam or offend others. We promise you will be treated the same way.

Log in your free account or if you still haven't joined our Webmaster Community Reward Program, you can create your free account now.

Posting tip:
if you use code in your comments please put it in these tags [php], [sql], [css], [js]
PHP code example: [php] echo date("Y-m-d"); [/php]

Thank you,
~ PHPJabbers team ~