« Put watermark on images using PHPMake php counter »

Make contact form and send email in PHP Posted in PHP Tutorials | 341 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 experience with millions of other webmasters visiting our website. Contact us for more information how to become a contributor.

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

frinto September 30, 2014 at 3:18 pm | Reply

0

adf
sneha September 30, 2014 at 3:12 pm | Reply

0

Nice tutorial
sneha September 30, 2014 at 2:54 pm | Reply

0

hello
nirob September 28, 2014 at 10:12 am | Reply

0

hi ,
how are you?
cuong September 3, 2014 at 6:30 pm | Reply

0

xin chao tran van cuong
mandeep maan August 21, 2014 at 3:46 pm | Reply

0

Nice tutorial really helpful to me i had made nice form with the help of this
you can check at jobdeoz.com/test.php
manvendrasinghbhati August 19, 2014 at 12:20 pm | Reply

+3


$name=$_REQUEST['name'];
$email=$_REQUEST['email'];
$mobile=$_REQUEST['mobile'];
$message=$_REQUEST['message'];
$err_check=0;
$err_msg='';
$err_msg2='';
$err_check1=0;

if(isset($name) && $name ==''){//name check

$err_msg="Please enter your name!<br/>";
$err_check=1;
}
if(isset($email) && $email ==''){//email blank check

$err_msg.="Please enter your <b>email</b> id!<br/>";
$err_check=1;
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL))
{
$err_msg2.="Please enter your <b>Vaild-email id</b>! ($email)<br/>";
$err_check1=2;
}

if(isset($mobile) && $mobile ==''){//mobile number blank check

$err_msg.="Please enter your <b>mobile</b> !<br/>";
$err_check=1;
}
elseif (isset($mobile) && !ctype_digit($mobile)) {//mobile number check
$err_msg2.="Please enter your <b>Vaild</b> mobile ! ($mobile)<br/>";
$err_check1=2;
}if (isset($mobile) && (strlen($mobile) < 10) ) {

$err_msg2.="Please enter your <b>Minmum 10 digits</b> mobile ! ($mobile)<br/>";
$err_check1=2;

}
if(isset($message) && $message ==''){

$err_msg.="Please enter your <b> Message!</b><br/>";
$err_check=1;
}

if(isset($message) && (strlen($message) < 50) ){

$err_msg2.="Please enter <b> Min 50 characters !</b><br/>";
$err_check1=2;
}


if($err_check==1 && $err_check1==2){
echo '<div class="notification closeable error"><p><b>Errors:'.$name.'</b><br/> '.$err_msg.' </p></div>';
echo '<div class="notification closeable warning"><p><b>Errors:'.$name.'</b><br/> '.$err_msg2.' </p></div>';

}elseif($err_check==1){
echo '<div class="notification closeable error"><p><b>Errors:'.$name.'</b><br/> '.$err_msg.' </p></div>';
}elseif($err_check1==2){
echo '<div class="notification closeable warning"><p><b>Errors:'.$name.'</b><br/> '.$err_msg2.' </p></div>';
}else{
$msg_box='';
$msg='<table width="50%">
<tr><td colspan="2"><b>MAIL HEADING</b></td></tr>
<tr><td><b>Name</b></td><td>'.$name.'</td></tr>
<tr><td><b>Email :</b></td><td>'.$email.'</td></tr>
<tr><td><b>Mobile :</b></td><td>'.$mobile.'</td></tr>
<tr><td><b>Message :</b></td><td>'.$message.'</td></tr>
</table>';


/* To send HTML mail, you can set the Content-type header. */
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= 'Cc: man9024@gmail.com' . "\r\n";
/* additional headers */
$headers .= 'meet to bhati' . "\r\n";


$to="man9024@gmail.com";

$subject="Enquiry form Sai deepa";
//send mag with condation
if(mail($to,$subject,$msg,$headers))
{
$msg_box= '<div class="notification closeable success"><p> Success! You did it, now relax and enjoy it. </p></div>';
}
else
{
$msg_box=
'<div class="notification closeable warning"><p> Warning! Change this and that and try again. </p></div>';
}
echo $msg_box;
}

Brandt Fourie July 12, 2014 at 9:36 pm | Reply

+2

Hi, i copied everything as per above but my php code is showing on the webpage. what am i doing wrong? Please help.
blu September 5, 2014 at 4:55 pm

0

save your form as a .php file
Matt September 7, 2014 at 6:08 am

+1

You must use it on a server with PHP running.
creativeyoung July 1, 2014 at 6:21 am | Reply

+3

Hi, this seems like an awesome form!
Abel June 18, 2014 at 7:16 pm | Reply

+2

I allready have a contact form on my website with a php file connecting to the db all of that is working but the email notification is not any help somebody????


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 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 ~