PHP mail() And Gmail – A Warning on Headers

Today, out of nowhere, we started receiving reports that HTML email the site was sending were showing up as just that: HTML code and not much else. Running a few tests confirmed these reports… but only in my gmail account. We went back and checked the reports and sure enough – they were all coming from gmail users.

After some tinkering and futzing around we realized that the Windows line breaks that we had after the headers (rn) were the problem. Here’s an example:

This will show up as two line breaks in gmail and trash your HTML formatting:
$headers = "MIME-Version: 1.0rn";

This, however, will fix the problem and show up in gmail just fine:
$headers = "MIME-Version: 1.0n";

I’m sure if you were sending mail from a Windows server this may not be the case, but for those on a Unix box, just stick with n. Google will thank you by displaying your HTML email the way it was meant to be displayed – without raw code!

  1. Thanks man. Very helpful.

    • setxi
    • November 12th, 2007

    Thanks, this save me a while

    • Bert
    • December 18th, 2007

    You’re the man! Thanks for the Christman gift!! 😉

  2. Thank you 🙂 Worked just fine.

  3. Thank you! You have completly resolved my problem 🙂

    • Avani
    • March 17th, 2008

    Thanks a lot 🙂

  4. You just saved my ass, thank you sir!

  5. but is it cause any problem for yahoo or hotmail user?

    • KaEL
    • July 1st, 2008

    Thank you, Just what i need.

  6. Desde México un agradecimiento, ya llevábamos bastante tiempo investigando que pasaba

  7. Thanks, I hope that fixes it. What’s interesting is that I have a gmail account and two Google Apps accounts. All the emails that I sent to myself were properly formatted. It was only some of my readers who were seeing the HTML code. Odd.

    But ultimately who cares. I think this fixes it for everyone.

  8. thanx man

    • deepak kumar
    • June 8th, 2010

    i am using the email code everything is ok but images and css not show in gmail.

    $headers.=”MIME-Version: 1.0n”;
    $headers.=”Content-type: text/html; charset=utf-8rn”;
    $headers.=”Content-Transfer-Encoding: quoted-printablern”;

  1. No trackbacks yet.