Create a Readable Email Contact Form with PHP Tutorial - no Programming Skills Required
Just about every website has a contact page, but for many sites, it's not enough to provide an email. Sometimes we want to make contacting us as easy as possible for our readers. There are quite a few solutions out there to build contact forms and have them emailed, but formatting the email for the recipient can be quite cumbersome! And if you don't know php or any other scripting language, you're pretty much stuck with formmail. Yuk.
This tutorial will teach you how to build a form, with optional required fields, and have a nicely formatted email using a php function, yet you do not need to know php! You should, however, know HTML and be quite familiar with forms.
If you can't wait to see the results,
- Try out the sample form emailed as HTML;
- Try out the sample form emailed as plain text;
- Download the file processemailform.php (zip);
- View the source code (new window).
Update 8-21-2008! Added a psuedo-captcha option for even more anti-spam.
Update 10-31-2007! The "required fields" function also validates email addresses.
Update 4-15-2007! Thanks to two users of my form, two bugs were found and fixed. One had to do with the new required fields (it was using a POST variable instead of the defined variable). The other bug had to do with the last field, which sometimes would not display. I've also updated the script to insert hard returns in the email where the person submitting the form has entered a hard return, such as in comments boxes.
New 4-1-2007! Now you can put required fields on your form.
New 12-31-2006! I've added a 'testemail.php' script in the zip package you can use first to check what your host will require to process the script.
New 5-8-2006! Don't have PHP? Don't have to worry! I can host your processemailform.php script. Contact me (jenny - at - whitewavedesigns.com) for details! This is a FREE service; no charges, no obligations!
New 9-3-2006! Do you want the email message reiterated on your thankyou page? Just put this line of code where you want it to be placed: <? echo $_SESSION['message']; ?>.
Some notes on the PHP settings
My processemailform.php script was written for PHP4/PHP5 on a UNIX box, with magic_quotes_gpc set to Off. However, it has also been tested with magic_quotes_gpc set to On, and PHP Version 4.3.11-dev IIS 6.0 with Windows 2003 Server. Error reporting has been turned off for this particular script as well. To see what your configurations are, create a php file called "test.php" and include in it only this line of code: <? phpinfo(); ?>.
I'd love for others who has successfully used my form to drop me an email with the settings of your server. Also, if there are any issues, let me know as well (jenny - at - whitewavedesigns.com). The more robust my script can be, the more helpful it will be to others! Thanks!
Some notes on anti-spam
Who doesn't hate spam? My processemailform.php script is pretty good (if I may say so myself) at preventing spam abuse. For example, if a spambot inserts additional header code into any of the form fields, the script redirects to Google.com (a site I picked at random) rather than send you the spam email. Likewise, if a spambot inserts the URL prefix "http://" into any form field, the script again redirects instead of sends you the email.
Since it is possible that a "real" contact user would enter a full URL as opposed to one beginning with "www", be sure to indicate this on your contact page if you are asking for websites or URLs. Example:
Your website: (do not enter "http://" or your form will be rejected as spam)
Let's get started by building the form.
Page 1 ~ >>