Search code examples
phpemaillivehtml-email

Very simple HTML / plain-text email not showing at all in microsoft live.com


I have been using a very simple PHP function to construct HTML / plain-text emails. It worked well in gmail and many other clients I saw.

As it turns out, @live.com accounts don't get to see the email content at all.

Here's the relevant code:

$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: server@example.com\r\nReply-To: server@example.com\r\n";
$headers .= "MIME-Version: 1.0\r\n";
//add boundary string and mime type specification
$headers .= "Content-Type: multipart/alternative; boundary=$random_hash\r\n";
$headers .= "Content-Transfer-Encoding: 7bit\r\n";
//define the body of the message.
$message = "This is a MIME encoded message.\r\n\r\n" .
    "--$random_hash\r\n" .
    "Content-Type: text/plain; charset=utf-8\r\n" .
    //"Content-Transfer-Encoding: 7bit\r\n" .
"\r\nthis is plain text.\r\n\r\n" .
"--$random_hash\r\n" .
"Content-Type: text/html; charset=utf-8\r\n" .
    //"Content-Transfer-Encoding: 7bit\r\n" .
    "\r\n<html><body>this is wonderful <b>HTML</b> text.</body></html>" .
    "\r\n\r\n--$random_hash--\r\n";
mail('someAddress@live.com', 'This is u umlaut: ü', $message, $headers);

If I only send either HTML or plain-text, it will display properly.

Any ideas?

I do not want to use an extra library. All I need the HTML for is links, basically. The answers on PHP Multi-Part Text/HTML showing blank don't help me.

And btw: the u umlaut is displayed incorrectly in the email list in the live.com inbox but displayed correctly when I open the email in live.com...


Solution

  • Got it after a few hours.

    The issue are the CR and LF.

    I replaced all CRLF (\r\n) by LF (\n) and it worked.

    On the way, I also replaced 7bit by 8bit.

    And as for the subject line, that was an easy one to google for, and this is the solution for utf-8:

    $subject = '=?utf-8?B?' . base64_encode($subject) . '?=';
    

    Thanks guys!