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

前端 未结 4 1194
北海茫月
北海茫月 2020-12-18 13:49

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

相关标签:
4条回答
  • 2020-12-18 13:59

    If You use postfix<2.9, You can just put sendmail_path = "tr -d '\r'|sendmail -t -i" into php.ini.

    0 讨论(0)
  • 2020-12-18 14:01

    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

    0 讨论(0)
  • 2020-12-18 14:07

    The email is been interpreted as text/plain instead of intended html. The reason for this is that text/html is a multipart subtype thus requiring boundary declarations.

    Your code is missing a the header boundary declaration:

    $message  = get_HTML_email_with_valid_formatting();
    $headers  = "MIME-Version: 1.0\r\n";
    $headers .= "--$boundary\r\n"."Content-Type: text/html; charset=ISO-8859-1\r\n";
    $headers .= "From: example.com <info@example.com>\r\n";
    $headers .= "Reply-To: donotreply@example.com\r\n";
    mail('me@example.com', 'test', $message, $headers);
    

    Check this wiki about MIME & Multipart Messages: http://en.wikipedia.org/wiki/MIME#Multipart_messages

    0 讨论(0)
  • 2020-12-18 14:18

    I suspect it is my version of Postfix - version 2.3.3 is 5 years old and perhaps it is converting LF to CRLF but seeing as I had CRLF already, I think I was sending CRCRLF to the mail clients.

    Unfortunately, I'm not in the situation to upgrade Postfix. So for the moment I have converted the code to use a configurable variable for the line endings so that it is easy to change in the future:

    $eol = "\n";
    $message  = get_HTML_email_with_valid_formatting();
    $headers  = "MIME-Version: 1.0".$eol;
    $headers .= "Content-Type: text/html; charset=UTF-8".$eol;
    $headers .= "From: example.com <info@example.com>".$eol;
    $headers .= "Reply-To: donotreply@example.com".$eol;
    mail('me@example.com', 'test', $message, $headers);
    
    0 讨论(0)
提交回复
热议问题