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.

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

  1. PHP Headache

    I’m trying to deal with this issue at the moment. I believe it’s a bug in Win PHP coupled with the fact that IIS SMTP is quite strict.
    The correct format for a friendly SMTP address is:
    My Name
    However PHP’s send mail is using this format:
    <My Name >
    Which as far as I’m aware is just plain wrong!

  2. PHP Headache

    Hmm – looks like email addresses are being stripped out.
    Let me try again!

    The correct format for a friendly SMTP address is:
    My Name
    However PHP’s send mail is using this format:
    <My Name >
    Which as far as I’m aware is just plain wrong!

Leave a Reply

Your email address will not be published. Required fields are marked *