PHP Email Exercise (12 May 2008)
Before starting this exercise, make sure that you've completed the Webmonkey PHP from the Ground Up tutorial in this week's readings. You should understand basic PHP syntax, as well as passing variables from a form on one page to a PHP script on another.
In this exercise, you're going to work with two PHP files--one that gathers information from the user using a contact form, and one that takes the information from the form, checks it for errors, and sends out the message.
I've created the starting versions of both of these pages; you should download the zip archive with the two files before you begin.
Start with the the phpmailform file. The only thing it contains is an HTML form, with an action of phpmail.php (the second file we're working with).
The first section of the PHP code grabs several environmental variables, including the user's IP address, the address of the referring page, and the browser's self-identifying information. The getenv() function is used to retrieve those, and they're assigned to PHP variables. Those variables are then used to populate hidden form variables, so that the information can be passed with the form data using either GET or POST methods.
Then the user is prompted for name, email address, subject, and message text, using a variety of HTML form inputs.
Clicking on the send button causes the values of all the form fields to be sent to the phpmail.php script--so now open that one and let's look at how it works.
The first part of the phpmail.php page starts by assigning the POST variables to local variables for easier use in the program. It then does error checking against the form values. It checks to be sure that there are values in the fields (name, email, and message body), and then checks to be sure that the email has both an @ and a . -- without which it would be invalid. If it fails either test, an error message is output and the script processing is ended using the return function.
If the form has been completed properly, the script then cleans up the form data (using the stripslashes() function), generates the email message body by combining the user's message with the data from the hidden fields, and then uses the PHP mail() function to send the message. After sending the message, the user sees a web page showing the data that they submitted, with a link back to the original form.
You'll need to change two things in the phpmail.php script: Your email should go into the mail() function, in place of the YourEmailHere string. And your name should go into the success message, in place of Your Name Here.
Once you've made those changes, upload both files to the server, and test using the form. Try three versions: one filled out properly, to make sure it sends you the email, one with the form blank to make sure it checks for missing inputs, and one with an incomplete email address. Make sure that it catches the error states properly.
After you have it working, think about how you could change the form a bit. For example, what if you wanted the user to be able to pick from multiple recipients (e.g. you or your professor)? How would you implement that?
You should also clean the form up a bit aesthetically. Use CSS to format it. Here are some good resources for CSS form design:
- Smashing Magazine: CSS-Based Forms: Modern Solutions
- The Man in Blue: Accessible, Stylish Form Layout
Another thing to consider is using Javascript to check the form before it's sent to the second page. Here's a good tutorial on that
Once you've finished with this exercise, make sure you add a blog entry pointing to the completed form.
There are no recorded trackback links from other weblogs.
Thanks for signing in, . Now you can comment. (sign out)
(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)
(Trackback link: http://www.it.rit.edu/~ell/mt/mt-tb.cgi/2524)