Page 539 - Beginning PHP 5.3
P. 539

Chapter 16: PHP and the Outside World
                           You can send this email message in much the same way as a plain text email; the only difference is
                         that you need to include two additional headers in the email message:
                               MIME-Version: 1.0

                               Content-Type: text/html; charset=utf-8
                            MIME - Version:  tells the mail reader application to expect a message in MIME format (as well as the
                         version of MIME being used), and   Content - Type:  specifies the type of content to expect. In this case,
                            text/html; charset=utf -   tells the mail reader to expect an HTML email encoded in the UTF - 8
                                                 8
                         (Unicode) character set.
                           So to send the HTML email just shown, you could write:

                             $headers = “From: The Widget Company  < widgets@example.com > \r\n”;
                             $headers .= “MIME-Version: 1.0\r\n”;
                             $headers .= “Content-type: text/html; charset=utf-8\r\n”;
                             $recipient = “John Smith  < johnsmith@example.com > ”;
                             $subject = “Thank you for contacting us”;
                             mail( $recipient, $subject, $message, $headers );

                           If you want to send more than one component in the message  —  for example, an alternate plain text
                         version of the message body, or several image attachments  —  you need to create a  multipart  MIME
                         message. Multipart content types are outside the scope of this book and are more complex than sending
                         a single - part email message. However, the   Mail_Mime  PEAR package makes this process very easy. For
                         more information see  http://pear.php.net/package/Mail_Mime .


                       Try It Out     Create a Contact Form Script
                         A common requirement for a Web site is a “contact us” form that visitors can use to contact the owner
                         of the site. In this example you create such a form, along with the PHP code to process the form and
                         email the results to the site owner.
                         Save the following script as contact.php in your document root folder:

                             <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
                               “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
                             <html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en”>
                               <head>
                                 <title>Contact Us</title>
                                 <link rel=”stylesheet” type=”text/css” href=”common.css” />
                                 <style type=”text/css”>
                                   .error { background: #d33; color: white; padding: 0.2em; margin:
                             0.2em 0 0.2em 0; font-size: 0.9em; }
                                   fieldset { border: none; padding: 0; }
                                   ol {list-style-type: none; padding: 0; margin: 0; }
                                   input, select, textarea { float: none; margin: 1em 0 0 0; width:
                             auto; }
                                   div.element { float: right; width: 57%; }
                                   div.element label { display: inline; float: none; }
                                   select { margin-right: 0.5em; }
                                   span.required { display: none; }
                                 </style>
                               </head>

                                                                                                         501





                                                                                                      9/21/09   9:15:40 AM
          c16.indd   501
          c16.indd   501                                                                              9/21/09   9:15:40 AM
   534   535   536   537   538   539   540   541   542   543   544