Page 537 - Beginning PHP 5.3
P. 537
Chapter 16: PHP and the Outside World
$extraHeaders = “From: Bob Jones < bob@example.com > \r\n” .
“Cc: Anna James < anna@example.com > \r\n” .
“X-Priority: 1 (Highest)\r\n” .
“X-Mailer: Matt’s PHP Script”;
mail( “Jim Smith < jim@example.com > ”, “Hello”, $message, $extraHeaders );
This code sets four headers: From: (the message sender), Cc: (an additional carbon - copied recipient),
X - Priority: (a value from 1 to 5 indicating the importance of the message), and X - Mailer: (a header
specifying the software that sent the message).
RFC 2822 defines the format of email messages, including all the different headers you can use in a
message. You can read it at: http://www.faqs.org/rfcs/rfc2822 .
X - Priority: and X - Mailer: are known as experimental headers and are not officially part of
RFC 2822 (though their usage is widespread).
Controlling the Return Path Email Address
If your script is running on a Unix Web server, you can pass additional command - line arguments to the
MTA as a fifth argument to mail() . Often this is used to add a – command - line argument in order to
f
set the so - called envelope sender or return path email address:
mail( “Jim Smith < jim@example.com > ”, “Hello”, $message, “From: Bob Jones
< bob@example.com > ”, “-f bob@example.com” );
Most email messages contain two “ from ” headers: the From: header and the Return - Path: header. The
From: address is the one usually displayed when the email is viewed in a mail program, and the
Return - Path: address is the one used to determine the “ real ” sender of the email for the purposes of
sending back bounce messages, determining if the email might be spam, and so on.
Often the two headers contain the same email address. However, when sending mail via a Web script the
Return - Path: header is usually set to the Web server ’ s username (for example, www@example.com ).
This can be a problem if you want to receive bounce messages (so you can determine if the email address
you ’ re trying to contact no longer exists) and you don ’ t have access to the “ www ” mailbox on the server.
By using the additional – argument as just shown, you can set the Return - Path: address to be the
f
same as the From: address (or you can set it to any email address where you can pick up email).
There ’ s one caveat with using – . If the Web server user isn ’ t trusted by the MTA, the MTA adds a
f
warning header to the email message similar to the following:
X-Authentication-Warning: www.example.com: user set sender to bob@example.com
using -f
Though this isn ’ t usually shown to the recipient, it often results in the message being flagged as spam or
otherwise treated as suspicious email. To tell the MTA to trust the Web server user you usually need to
add the Web server username to the /etc/mail/trusted - users file on the server. (If you don ’ t have
access to this file, ask your Web hosting provider for assistance.)
499
9/21/09 9:15:39 AM
c16.indd 499
c16.indd 499 9/21/09 9:15:39 AM