Sending Mail on Form Submission

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 = ; 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:


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 =

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

 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 /> 9:  <input type="text" size="25" name="email" /></p> 10: <p><strong>Message:</strong><br /> 11: <textarea name="message" cols="30" rows="5"></textarea></p> 12: <p><input type="submit" value="send" /></p> 13: </form> 14: </body> 15: </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 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

  1: <html>  2: <head>  3: <title>Sending mail from the form in Listing 11.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 = ""; 17: $subject = "Form Submission Results"; 18: $mailheaders = "From: My Web Site <> \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 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 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

  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 = ""; 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 <> \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 11.8.

Figure 11.8. Email sent from Listing 11.12.

Sams Teach Yourself PHP, MySQL And Apache All in One
Sams Teach Yourself PHP, MySQL and Apache All in One (3rd Edition)
ISBN: 0672328739
EAN: 2147483647
Year: 2004
Pages: 327 © 2008-2017.
If you may any questions please contact us: