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 }; ?>
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 |
| George posted on March 15, 2010, 12:04 pm |
| Hey there, thank you for the code, works swell with my own form. I've encountered a small issue though. I've got the email form "hardwired" so to speak to send emails to a gmail account. When I open the email, I get everything fine, except that the line "Content-type: text/html; charset=iso-8859-1" Prints in the actual message. Removing this from the code does not affect the message, except of course this line doesn't print. Is this just to set the encoding to appear in a webpage(and therefore not needed for gmail?)? |
| sdad posted on March 13, 2010, 8:19 am |
| asdasdas |
| Ben posted on February 11, 2010, 1:10 am |
| I need assistance setting this up for automation when people do not finish the form. I would like to send an email to them to invite them to utilize my website so that I do not have to send a manual email out to them. I am using .php extensions on my site |
| Deepak posted on January 25, 2010, 10:42 am |
| Well..in easy and efficient way...gud luck |
| Dany posted on January 4, 2010, 3:38 pm |
| Thank u for help.I'm gonna try it.I'm using Edit plus3 and Apache,mysql database.Will mail function work properly with my text editor?Is there any special configuration in Apache?Thanks. |
| David Lafko posted on December 31, 2009, 10:31 pm |
| Scratch my last post.. My new post is I got everthing to work and was able to change colors. When you open the page you see links then a bar and then the form.. My question is how do i get rid of the 1st bar untill the e-mail is sent |
| David Lafko posted on December 31, 2009, 3:31 am |
| I am playing with the example above. IT works ok but does not return to the entry page.. I change the form action and can get it to return to other pages but not the one the people entered on.. The link is http://www.orwigsburg.net/history.php How can i get it to move to a page to say thank you for your e-mail with this same look with the links that will be there they can return to other things in the site... |
| Ganesh posted on December 30, 2009, 9:06 am |
| \'\') { $ToEmail = \'youremail@site.com\'; $EmailSubject = \'Site contact form \'; $mailheader = \"From: \".$_POST[\"email\"].\"rn\"; $mailheader .= \"Reply-To: \".$_POST[\"email\"].\"rn\"; $mailheader .= \"Content-type: text/html; charset=iso-8859-1rn\"; $MESSAGE_BODY = \"Name: \".$_POST[\"name\"].\" \"; $MESSAGE_BODY .= \"Email: \".$_POST[\"email\"].\" \"; $MESSAGE_BODY .= \"Comment: \".nl2br($_POST[\"comment\"]).\" \"; mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die (\"Failure\"); ?> Your message was sent how can I fix it? someone please reply... Can I send the Email of the Form to a Yahoo or Gmail as receiver\'s address? |
| Ganesh posted on December 30, 2009, 9:06 am |
| '') { $ToEmail = 'youremail@site.com'; $EmailSubject = 'Site contact form '; $mailheader = "From: ".$_POST["email"]."rn"; $mailheader .= "Reply-To: ".$_POST["email"]."rn"; $mailheader .= "Content-type: text/html; charset=iso-8859-1rn"; $MESSAGE_BODY = "Name: ".$_POST["name"]." "; $MESSAGE_BODY .= "Email: ".$_POST["email"]." "; $MESSAGE_BODY .= "Comment: ".nl2br($_POST["comment"])." "; mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die ("Failure"); ?> Your message was sent how can I fix it? someone please reply... Can I send the Email of the Form to a Yahoo or Gmail as receiver's address? |
| Vikas posted on December 8, 2009, 1:50 pm |
| You must have not saved it in .php extension |