Daily Archives: February 19, 2012

501 5.5.4 Invalid Address when including Sender Name on Windows Server + PHP mail + IIS SMTP or MS Exchange

An interesting issue I came across the other day was that PHP was complaining giving a 501 5.5.4 Invalid Address error when trying to send email. The server uses IIS SMTP and the sender specified using the From: header ie
From: Me
Just specifying the email by itself works fine:
From: [email protected]

It turns out that this is a conflict between IIS SMTP and PHP. You need to specify a from email address separately by setting the ini configuration sendmail_from (for the MAIL FROM: command I presume) ie.

[php]
function mail2($from_address, $from_name, $to_address, $subject, $message, $headers) {
$old_sender = ini_get(‘sendmail_from’);
ini_set(‘sendmail_from’, $from_address);
$headers = "From: " . $from_name . " <" . $from_address . ">\r\n" . trim($headers);
mail($to_address, $subject, $message, $headers);
ini_set(‘sendmail_from’, $old_sender);
}
[/php]

Presumably this is not an issue on UNIX as the external sendmail program handles delivery.