How to receive a failure notice when the recipient cannot be reached after sending an email using the PHP mail() function
I could not receive a failure notice when sending email to an email address that does not exist ($to_address), using this code:
$subject = "Email subject"; $message = "line1\r\nline2\r\nline3"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n"; $headers .= "To: <$to_email>\r\n"; $headers .= "From: Me <$my_email_address>\r\n"; $headers .= "Return-Path: <$my_return_email_address>r\n"; mail($to_email, $subject, $message, $headers);
The problem was that the email address I wanted to receive the failure notices to ($my_return_email_address) was not the same as the value of the configuration option sendmail_from in the php.ini file (Apache web server installed on a machine with the Windows Professional operating system). So the failure notices were sent to the sendmail_from email address if this was an existing address, instead of the email address specified in the Return-path header of the email.
The solution is replacing the lines:
$headers .= "Return-Path: <$my_return_email_address>r\n"; mail($to_email, $subject, $message, $headers);
with:
// when the PHP server runs on Windows ini_set(sendmail_from, $my_return_email_address); mail($to_email, $subject, $message, $headers); ini_restore(sendmail_from);
// when the PHP server runs on UNIX mail($to_email, addslashes($subject), $message, $headers, "-r $my_return_email_address");
This means that, before sending the email, we set the value of the sendmail_from configuration option to $my_return_email_address and we restore the default value of the configuration option after the email is sent.
Warning: mail(): SMTP server response: 451 See http://pobox.com/~djb/docs/smtplf.html. in path_to_php_file on line #
Full error message: Warning: mail(): SMTP server response: 451 See http://pobox.com/~djb/docs/smtplf.html. in path_to_php_file on line #
This is an error message that I got lots of times and it was always the same problem. So I decided to write here the solution for the next time I encounter it and I thought it might also help other people.
So I tried to send an email in plain text using the PHP function mail() :
// consider this being an existing email address $to_address = "abc@def.com"; $subject = "Email subject"; $message = "line1 line2 line3"; $headers = "MIME-Version: 1.0\r\n"; $headers .= "Content-type: text/plain; charset=iso-8859-1\r\n"; $headers .= "To: <".$to_email.">\r\n"; $headers .= "From: Me <".$my_email_address.">\r\n"; mail($to_email, $subject, $message, $headers);
It worked fine with Apache server running on Linux operating system, but I got the error message mentioned before with the server running on Windows.
It looks like while Unix-based systems recognize \n character (the equivalent in PHP of LF – line feed) as a newline even when sending emails, Windows systems are stricter in comunicating using some textual Internet Protocols (such as SMTP) that mandate the line terminator to be the ASCII CR+LF (carriage return + line feed) sequence, which is abstracted in PHP to \r\n character sequence.
So the problem was the $message variable. The correct way to assign the multiple lines value to it is:
$message = "line1\r\nline2\r\nline3"; // separate the lines with \r\n // or, elegantly: $message = "line1\r\n"; $message .= "line2\r\n"; $message .= "line3";
And now it works on Windows, too.
