PHPjabbers - tools for webmasters Stay in touch
HOME
ORDER
PHP TUTORIALS
PHP FORUM
ABOUT US
CONTACT US
Our products

Make contact form and send email in PHP

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">&nbsp;</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">&nbsp;</td>
<td align="left" valign="top"><input type="submit" name="Submit" value="Send"></td>
</tr>
</table>
</form> 
<?php 
}; 
?>

You can combine that contact form example with the CAPTCHA verification to prevent spammers from flooding your mail box. Did you check our Contact Form script?

COMMENTS
Tom

Hi it says the mail is sent but I'm not getting it in my inbox? It's no where to be found. What am I doing wrong?
--------------------------
Veselin: it is possible that you server is set to not allow sending emails using mail() function. Contact your hosting company and ask for the right PHP code to send email using a PHP script
Paolo

Hi... The whole thing went directly to my junk mail, is there a way that I could fix this thing. Thanks for the help I found it very helpful and is working for my website. Now I'm going to use it for my clients.
ja

when you copy all this test ,so u need test it in server ,or u should to have mail sever first ,so that it can work ..
karen

i don't understand how to do this.

1. i make a file test.php and paste the first set of codes there

then where do i put the next two set of codes?
Bridget

How would I capture the IP address of the person sending the form? The CGI formmail I was using captured the IP Address, HTTP USER AGENT, and Date. How would I incorporate that into this script?
------------------
Veselin: you can use this to get user IP address
$access_ip = (getenv(HTTP_X_FORWARDED_FOR)) ? getenv(HTTP_X_FORWARDED_FOR) : getenv(REMOTE_ADDR);
Charles Sandor

How would you write the coding if you have incorporated a selection using radio buttons and a selection from a drop down list?
Thanks
----------------------
Veselin: you can the same code for getting radion button value or drop down value - $_POST["dropdownname"] or $_POST["radiobuttonname"]
life

May i ask you how to make the button send active?
-----------------
PHPjabbers.com : can you please elaborate
tyson

This worked great!

Is there a way to redirect to a thank you page instead of "Your form has been submitted" after the submission?
raghu

I am getting the Message \" Failure\"
what to do.
The form upon filling goes through and i get this message
----------------------
Veselin: you need to check with your hosting company how to send emails using PHP
Mike

Killer tutorial, this has worked perfectly for me until I put it on my host\'s server. I am getting this error:
Notice: Undefined index: email in F:\\users\\madcap-pdc\\NEWSITE\\contact.php on line 20
Funny thing is that it sends the e-mail just fine...it just leaves this unsightly blemish on my contact page *scratches head*

-------------------
Veselin: this is a notice which can be hidden by adding
error_reporting(0);
at the top of your php script or instead of
if ($_POST["email"]<>'') {
which I think is your line 20 you use
if (isset($_POST["email"]) AND $_POST["email"]<>'') {
 
POST A COMMENT
Your name:
Your email: (email address will not be posted on the website)
Comment:
Verification code:
type characters on the image in the text box