How to format an email that Hotmail / Outlook is happy with?

TijuanaKez picture TijuanaKez · Mar 16, 2014 · Viewed 15.4k times · Source
$body = 'This is a test';
    $subject = 'Confirmation';
$headers = 'From: Testing Site' . "\r\n";
$headers .= 'Reply-To: [email protected]' . "\r\n";
$headers .= 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html;charset=iso-8859-1' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion(). "\r\n";
$headers .= 'Delivery-Date: ' . date("r") . "\r\n";
//$headers .= 'Message-Id: <[email protected]>' . "\r\n";

mail("[email protected]", $subject, $body, $headers, "-f [email protected]");
mail("[email protected]", $subject, $body, $headers, "-f [email protected]");

Emails send fine to Gmail but are always rejected by Hotmail with this error:

host mx1.hotmail.com[65.55.33.119] said: 550 5.7.0 (COL0-MC5-F28) Message could not be delivered. Please ensure the message is RFC 5322 compliant. (in reply to end of DATA command).

Message ID header is generated automatically by the server but it doesn't help to supply one manually either.

Why isn't Hotmail happy?

Mail server has SPF record, reverse DNS, is not blacklisted and passes all checks at mxtoolbox.com.

Answer

Jasper N. Brouwer picture Jasper N. Brouwer · Mar 19, 2014

The From header is invalid. It must have the following syntax:

From: "name" <email-address>

In your case:

From: "Testing Site" <[email protected]>

The same goes for your Reply-To header:

Reply-To: "Testing Site" <[email protected]>

Which you can omit if it's the same as the From header (like in your case).

PS: RFC 2822 doesn't state that the display-name in an address should be quoted. In other words: the following 3 headers should all work:

From: "Testing Site" <[email protected]>
From: 'Testing Site' <[email protected]>
From: Testing Site <[email protected]>