Search code examples
phpmimepostfix-mtahtml-emailemail-formats

PHP mail formatting issue - Why do CRLF header line endings break HTML email in Outlook?


I'm using the PHP native mail() function to send HTML emails and have a formatting problem in the users most common email client - Outlook 2007 (in addition to some other email clients) - all the html tags are exposed so it looks like gibberish to a non-web-developer.

I'm sending HTML email the same way that the PHP manual demos it. Example:

$message  = get_HTML_email_with_valid_formatting();
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
$headers .= "From: example.com <[email protected]>\r\n";
$headers .= "Reply-To: [email protected]\r\n";
mail('[email protected]', 'test', $message, $headers);

Because testing various email clients is hard, I have signed up with http://litmusapp.com/ so I can see a screenshot of the emails in 47 different email clients. Most are ok (e.g. gmail, thunderbird, Lotus Notes) but all the different versions of Outlook are not ok.

To fix the formatting issue I had do the following:

  1. Remove the $headers = "MIME-Version: 1.0\r\n"; mail header.
  2. Ensure I end my headers with only "\n" instead of "\r\n".

Does anyone know why I am getting better results with HTML emails when I do not conform to the manual?

Info:

  • I'm using postfix version 2.3.3 on RHEL 5.5.
  • PHP version 5.3.2

Solution

  • You have 2 solutions:

    1. Upgrade Postfix to +2.9 which has "sendmail_fix_line_endings" (finally!!!) See: Postfix documentation
    2. Install Sendmail (working fine!)

    I have a VMware image with a LAMP stack. In order to send email, I finally decided to:

    • install Sendmail
    • use the smtp of my ISP (as it is only a dev box).

    For the sendmail part, you can follow this: http://www.geoffke.be/nieuws/13/

    IMPORTANT: Some webhosters may use only stable packages which means you can have... a Postfix older than 2.9!!! Exemple: http://packages.debian.org/search?keywords=postfix