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. For 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 10.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 very 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 10.10. Creating a Simple Feedback Form
1: <HTML> 2: <HEAD> 3: <TITLE>E-Mail Form</TITLE> 4: </HEAD> 5: <BODY> 6: <FORM action="sendmail.php" method="POST"> 7: <p><strong>Name:</strong><br> <INPUT type="text" size="25" name="name"></p> 8: <p><strong>E-Mail Address:</strong><br> <INPUT type="text" size="25" name="email"></p> 9: <p><strong>Message:</strong><br> 10: <textarea name="message" cols=30 rows=5></textarea></p> 11: <p><INPUT type="submit" value="send"></p> 12: </FORM> 13: </BODY> 14: </HTML>
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 10.5.
Figure 10.5. Form created in Listing 10.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 10.4, which simply printed form responses to the screen. In this script in Listing 10.11, in addition to printing the responses to the screen, you send them to an email address as well.
Listing 10.11. Sending the Simple Feedback Form
1: <html> 2: <head> 3: <title> Sending mail from the form in Listing 10.10</title> 4: </head> 5: <body> 6: <?php 7: echo "<p>Thank you, <b>$_POST[name]</b>, for your message!</p>"; 8: echo "<p>Your e-mail address is: <b>$_POST[email]</b>.</p>"; 9: echo "<p>Your message was:<br>"; 10: echo "$_POST[message] </p>"; 11: //start building the mail string 12: $msg = "Name: $_POST[name]\n"; 13: $msg .= "E-Mail: $_POST[email]\n"; 14: $msg .= "Message: $_POST[message]\n"; 15: //set up the mail 16: $recipient = "email@example.com"; 17: $subject = "Form Submission Results"; 18: $mailheaders = "From: My Web Site <firstname.lastname@example.org> \n"; 19: $mailheaders .= "Reply-To: $_POST[email]"; 20: //send the mail 21: mail($recipient, $subject, $msg, $mailheaders); 22: ?> 23: </body> 24: </html>
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 superglobal. 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 10.6 in your browser.
Figure 10.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 10.7.
Figure 10.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 10.12, a variation of Listing 10.11, changes were made in lines 1214 and lines 1819.
Listing 10.12. Sending the Simple Feedback FormHTML Version
1: <html> 2: <head> 3: <title>Sending the Simple Feedback Form - HTML Version</title> 4: </head> 5: <body> 6: <?php 7: echo "<p>Thank you, <b>$_POST[name]</b>, for your message!</p>"; 8: echo "<p>Your e-mail address is: <b>$_POST[email]</b>.</p>"; 9: echo "<p>Your message was:<br>"; 10: echo "$_POST[message] </p>"; 11: //start building the mail string 12: $msg = "<p><strong>Name:</strong> $_POST[name]</p>"; 13: $msg .= "<p><strong>E-Mail:</strong> $_POST[email]</p>"; 14: $msg .= "<p><strong>Message:</strong> $_POST[message]</p>"; 15: //set up the mail 16: $recipient = "firstname.lastname@example.org"; 17: $subject = "Form Submission Results"; 18: $mailheaders = "MIME-Version: 1.0\r\n"; 19: $mailheaders .= "Content-type: text/html; charset=ISO-8859-1\r\n"; 20: $mailheaders .= "From: My Web Site <email@example.com> \n"; 21: $mailheaders .= "Reply-To: $_POST[email]"; 22: //send the mail 23: mail($recipient, $subject, $msg, $mailheaders); 24: ?> 25: </body> 26: </html>
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 10.8.
Figure 10.8. Email sent from Listing 10.12.