Getting your PHP HTML email to render in Gmail

Ever needed to send an HTML email via PHP only to find that it won’t freakin render as HTML in Gmail?  Well I did.  Of course the email renders correctly in every other mail provider (Yahoo, Hotmail, Outlook etc..).

Turns out that the problem is in the headers.  If you’re doing something like this:

$headers = "From:\r\n";
$headers .= "Reply-To:\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";

or this:

$headers = "From:" . "\r\n";
$headers .= "Reply-To:" . "\r\n";
$headers .= "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";

…then try something like this instead:

$headers = "From:\n";
$headers .= "Reply-To:\n";
$headers .= "MIME-Version: 1.0\n";
$headers .= "Content-type: text/html; charset=iso-8859-1";

The difference is in the line-breaks.  Using ‘\r\n’ causes two line-breaks in Gmail whereas using just ‘\n’ keeps Gmail happy.  And keeping Gmail happy is extremely important if you’re sending out an email!

12 much appreciated responses...

  1. Teddy says:

    Just got this problem , thx for ur info.

  2. Tadhg says:

    Legend, thank you!

  3. Nick says:

    Thanks man, you rule buddy.
    This mail issue has been bugging me for quite a while works perfectly now.
    Thank you so much!

    Kind Regards


  4. Anonymous says:

    Thank you for the evry useful information.

  5. Tulio says:

    Did anyone tested this fix on other email providers?
    Do they parse it correctly?

    • johndoesdesign says:

      Yep, this works across all major email clients – gmail, yahoo, hotmail, outlook etc.

  6. Nazir says:

    Thank you sooooo much your help. u saved my one full day… 🙂

  7. Harke says:

    You’re beautiful, solved my headache with gmail’s silly neurosis.

  8. dexcuit says:

    Dear sir,
    please help me in my php coding,

    1. $headers = ‘MIME-Version: 1.0\n”;
    2. $headers .= ‘Content-type: text/html; charset=iso-8859-1\n”;
    3. $headers .= ‘From: ‘.$_POST[‘businessName’].’ ‘ . “\r\n” .
    4. ‘Reply-To: ‘.$_POST[‘businessName’].’ ‘ . “\r\n” .
    5. ‘X-Mailer: PHP/’ . phpversion();
    7. /*Kirim Email*/
    8. mail($to, $subject, $jcitems, $headers);
    10. /*redirecting user to Thank you page*/
    11. Header(‘Location: ‘.$_POST[‘urlSuccess’].”);
    12. ?>

    in line 3 and 11, i get message:
    Parse error: syntax error, unexpected T_STRING

    Thank you for your ‘\n’ and your help.

    Best Regard.

  9. celine says:

    Merely wanna remark that you have a very decent site, I love the style and design it really stands out.

  10. jeff says:

    Thanks buddy. You saved me alot

  11. Greg says:

    As of February 19, 2014, this did not work for me. I have been trying to figure out why I cannot get gmail to render the html e-mail properly. I tried this, but gmail is still screwing up.

Feel free to add your comment..

Your email address will not be published.