You've already seen how to take form responses and print the results to the screen, so you're only one step away from sending those responses in an email message.
Before learning about sending mail, however, read through the next section to make sure that your system is properly configured.
System Configuration for the mail() Function
Before you can use the mail() function to send mail, a few directives must be set up in the php.ini file so that the function works properly. Open php.ini with a text editor and look for these lines:
[mail function] ; For Win32 only. SMTP = localhost ; For Win32 only. sendmail_from = email@example.com ; For Unix only. You may supply arguments as well (default: "sendmail -t -i"). ;sendmail_path =
If you're using Windows as your web server platform, the first two directives apply to you. For the mail() function to send mail, it must be able to access a valid outgoing mail server. If you plan to use the outgoing mail server of your ISP (in the following example, we use EarthLink), the entry in php.ini should look like this:
SMTP = mail.earthlink.net
The second configuration directive is sendmail_from, which is the email address used in the From header of the outgoing email. It can be overwritten in the mail script itself but normally operates as the default value, as in this example:
sendmail_from = firstname.lastname@example.org
A good rule of thumb for Windows users is that whatever outgoing mail server you've set up in your email client on that machine, you should also use as the value of SMTP in php.ini.
If your web server is running on a Linux/UNIX platform, you use the sendmail functionality of that particular machine. In this case, only the last directive applies to you: sendmail_path. The default is sendmail -t -i, but if sendmail is in an odd place or if you need to specify different arguments, feel free to do so, as in the following example, which does not use real values:
sendmail_path = /opt/sendmail -odd arguments
After making any changes to php.ini on any platform, you must restart the web server process for the changes to take effect.
Creating the Form
In Listing 11.10, you see the basic HTML for creating a simple feedback form, let's call it feedback.html. This form has an action of sendmail.php, which we will create in the next section. The fields in feedback.html are simple: Line 7 contains a name field, line 8 contains the return email address field, and line 10 contains the text area for the user's message.
Listing 11.10. Creating a Simple Feedback Form
Put these lines into a text file called feedback.html and place this file in your web server document root. Now access the script with your web browser, and you should see something like Figure 11.5.
Figure 11.5. The form created in Listing 11.10.
In the next section, you create the script that sends this form to a recipient.
Creating the Script to Send the Mail
This script is only slightly different in concept from the script in Listing 11.4, which simply printed form responses to the screen. In the script shown in Listing 11.11, in addition to printing the responses to the screen, you send them to an email address.
Listing 11.11. Sending the Simple Feedback Form
The variables used in lines 79 are $_POST["name"], $_POST["email"], and $_POST["message"]the names of the fields in the form, their values saved as part of the $_POST super-global. That's all well and good for printing the information to the screen, but in this script, you also want to create a string that's sent in email. For this task, you essentially build the email by concatenating strings to form one long message string, using the newline (\n) character to add line breaks where appropriate.
Lines 12 through 14 create the $msg variable, a string containing the values typed by the user in the form fields (and some introductory text for good measure). This string will form the body of the email. Note the use of the concatenation operator (.=) when adding to the $msg variable in lines 13 and 14.
Lines 16 and 17 are hard-coded variables for the email recipient and the subject of the email message. Replace email@example.com with your own email address, obviously. If you want to change the subject, feel free to do that, too!
Lines 18 and 19 set up some mail headers, namely the From: and Reply-to: headers. You could put any value in the From: header; this is the information that displays in the From or Sender column of your email application when you receive this mail.
By the Way
If your outbound mail server is a Windows machine, the \n newline character should be replaced with \r\n.
The mail() function requires four parameters: the recipient, the subject, the message, and any additional mail headers. The order of these parameters is shown in line 21, and your script is complete after you close up your PHP block and your HTML elements in lines 2224.
Put these lines into a text file called sendmail.php and place that file in your web server document root. Use your web browser and go back to the form, enter some information, and click the submission button. You should see something like Figure 11.6 in your browser.
Figure 11.6. Sample results from sendmail.php.
If you then check your email, you should have a message waiting for you. It might look something like Figure 11.7.
Figure 11.7. Email sent from sendmail.php.
Formatting Your Mail with HTML
The "trick" to sending HTML-formatted email is not a trick at all. In fact, it only involves writing the actual HTML and modifying the headers sent by the mail() function. In Listing 11.12, a variation of Listing 11.11, changes were made in lines 1214 and lines 1819.
Listing 11.12. Sending the Simple Feedback FormHTML Version
In lines 1214, the message string now contains HTML code. Additional headers are created in lines 1819, which set the Mime Version header to 1.0 and the Content-type header to text/html with a character set of ISO-8859-1. When opened in an HTML-enabled mail client, the HTML in the message string will appear as intended, as shown in Figure 11.8.
Figure 11.8. Email sent from Listing 11.12.