Page 536 - Beginning PHP 5.3
P. 536

Part III: Using PHP in Practice
                   At a minimum,  mail()  requires three arguments:

                   ❑       A string containing the recipient ’ s email address (or a comma - separated list of email addresses if
                       sending to multiple recipients)
                   ❑       The email subject line, as a string
                   ❑       The message body, as a string

                   mail()  returns  true  if the mail was accepted for delivery by the mail server, or  false  if there was a
                problem. (Note that an email message might still eventually bounce, even if the mail server accepted it
                for delivery.)

                  For example, the following code sends a short email entitled  “ Hello ”,  with a message body of  “ Hi Jim,
                how are you? ”,  to jim@example.com:

                    mail( “jim@example.com”, “Hello”, “Hi Jim, how are you?” );

                   You can also include the recipient ’ s real name in the recipient string, provided you follow it with the
                 recipient ’ s email address in angle brackets. For example:


                    mail( “Jim Smith  < jim@example.com > ”, “Hello”, “Hi Jim, how are you?” );
                   To send a multi - line message, pass in a string that contains newline characters. Here ’ s an example:

                    $message = “Hi Jim,
                    How are you?
                    “;

                    mail( “Jim Smith  < jim@example.com > ”, “Hello”, $message );

                   Lines of text in an email message body should not exceed 70 characters in length. To ensure that your
                 lines are of the correct length you can use PHP ’ s   wordwrap()  function:

                    $message = wordwrap( $message, 70 );

                  Specifying the Sender Address and Adding Headers

                   By default, when running on a Unix server such as Linux or Mac OS X,  mail()  usually sends messages
                from the Web server ’ s username, such as  “ www ”  or  “ www - data ”.  (On Windows servers you need to
                specify a default  “ from ”  address with the   sendmail_from  option in the  php.ini  configuration file.) If
                 you want to send your email from a different  “ from ”  address, you can specify the address in a fourth
                 argument to   mail() , as follows:

                    mail( “Jim Smith  < jim@example.com > ”, “Hello”, $message, “From: Bob Jones
                      < bob@example.com > ” );

                   This fourth argument lets you specify additional headers to include in the mail message. In this case, just
                 one header was added  —  the   From:  header  —  but you can add as many headers as you like. Just make
                 sure you separate each header by both a carriage return (  \r ) and line feed ( \n ) character; this is required
                 by the specification for Internet email messages:


              498





                                                                                                      9/21/09   9:15:39 AM
          c16.indd   498                                                                              9/21/09   9:15:39 AM
          c16.indd   498
   531   532   533   534   535   536   537   538   539   540   541