Sending Email with Headers


You can do more with the mail functionfor example, you can set additional email headers to cc ("carbon copy") or bcc ("blind carbon copy," where the recipient doesn't know a copy was also sent to someone else) people. To set such headers, you use the additional_headers parameter in the call to the mail function:

 mail(string to, string subject, string message [, string additional_headers [, string additional_parameters]]) 

Note that you use \r\n to separate headers (although some Unix mail transfer agents may work with just a single newline, \n). Example 9-9, phpemailheaders.html, lets the user set "cc" and "bcc" addresses.

Example 9-9. Using headers, phpemailheaders.html
 <HTML>     <HEAD><TITLE>Send me some email with headers</TITLE></HEAD>     <BODY><CENTER>             <H1>Send me some email with headers</H1>             <BR>             <FORM METHOD=POST ACTION="phpemailheaders.php">                 Please enter your message and click Submit:                 <BR>                 cc: <INPUT TYPE="TEXT" NAME="cc">                 bcc: <INPUT TYPE="TEXT" NAME="bcc">                 <BR>                 <TEXTAREA NAME="message" COLS="50" ROWS="5"></TEXTAREA>                 <BR>                 <INPUT TYPE="SUBMIT" VALUE="Submit">             </FORM>           </CENTER></BODY> </HTML> 

You can see what this page looks like in Figure 9-9.

Figure 9-9. Sending email with headers.


In phpemailheaders.php, Example 9-10, we check the cc and bcc data, and if either exists, we add them to the variable $headers:

 if(isset($_REQUEST["cc"])){     $headers .= "cc:" . $_REQUEST["cc"] . "\r\n"; } if(isset($_REQUEST["bcc"])){     $headers .= "bcc:" . $_REQUEST["bcc"] . "\r\n"; } 

After we've set the headers as needed, we use the mail function to send the email, as you see in phpemailheaders.php, Example 9-10.

Example 9-10. Sending email with headers, phpemailheaders.php
 <HTML>     <HEAD><TITLE>Sending email</TITLE></HEAD>     <BODY>         <CENTER>             <H1>Sending email</H1>             Your email was sent.             <BR>             <?php                 $headers = "";                 if(isset($_REQUEST["cc"])){                     $headers .= "cc:" . $_REQUEST["cc"] . "\r\n";                 }                 if(isset($_REQUEST["bcc"])){                     $headers .= "bcc:" . $_REQUEST["bcc"] . "\r\n";                 }                 $result = mail("steve@chooseanisp.com", "Web mail",                     $_REQUEST["message"], $headers);             ?>          </CENTER>     <BODY> </HTML> 

This script sends the email to steve@chooseanisp.com, along with the cc and bcc addresses.



    Spring Into PHP 5
    Spring Into PHP 5
    ISBN: 0131498622
    EAN: 2147483647
    Year: 2006
    Pages: 254

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net