Example 2. Sending mail with extra headers.
<?php
mail("nobody@example.com", "the subject",
$message,
"From: webmaster@{$_SERVER['SERVER_NAME']}\r\n"
.
"Reply-To: webmaster@{$_SERVER['SERVER_NAME']}\r\n"
.
"X-Mailer: PHP/" . phpversion());
?>
The additional_parameters parameter can be used to pass
an additional parameter to the program configured to use
when sending mail using the sendmail_path configuration
setting. For example, this can be used to set the envelope
sender address when using sendmail with the -f sendmail
option. You may need to add the user that your web server
runs as to your sendmail configuration to prevent a 'X-Warning'
header from being added to the message when you set the
envelope sender using this method. Example 3. Sending mail
with extra headers and setting an additional command line
parameter.
<?php
mail("nobody@example.com", "the subject",
$message,
"From: webmaster@{$_SERVER['SERVER_NAME']}", "-fwebmaster@{$_SERVER['SERVER_NAME']}");
?>
Note: This fifth parameter was added in PHP 4.0.5. Since
PHP 4.2.3 this parameter is disabled in safe_mode and the
mail() function will expose a warning message and return
FALSE if you're trying to use it.
You can also use simple string building techniques to build
complex email messages. Example 4. Sending complex email.
<?php
/* recipients */
$to = "mary@example.com" . ", " ; //
note the comma
$to .= "kelly@example.com";
/* subject */
$subject = "Birthday Reminders for August";
/* message */
$message = '
<html>
<head>
<title>Birthday Reminders for August</title>
</head>
<body>
<p>Here are the birthdays upcoming in August!</p>
<table>
<tr>
<th>Person</th><th>Day</th><th>Month</th><th>Year</th>
</tr>
<tr>
<td>Joe</td><td>3rd</td><td>August</td><td>1970</td>
</tr>
<tr>
<td>Sally</td><td>17th</td><td>August</td><td>1973</td>
</tr>
</table>
</body>
</html>
';
/* To send HTML mail, you can set the Content-type header.
*/
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
/* additional headers */
$headers .= "To: Mary <mary@example.com>, Kelly
<kelly@example.com>\r\n";
$headers .= "From: Birthday Reminder <birthday@example.com>\r\n";
$headers .= "Cc: birthdayarchive@example.com\r\n";
$headers .= "Bcc: birthdaycheck@example.com\r\n";
/* and now mail it */
mail($to, $subject, $message, $headers);
?>
Note: Make sure you do not have any newline characters in
the to or subject, or the mail may not be sent properly.
Note: The to parameter should not be an address in the
form of "Something <someone@example.com>".
The mail command may not parse this properly while talking
with the MTA (Particularly under Windows).
See also imap_mail().