【问题标题】:php mail with utf-8 encoding displays incorrectly in Microsoft mail clients带有 utf-8 编码的 php 邮件在 Microsoft 邮件客户端中显示不正确
【发布时间】:2016-10-19 18:58:54
【问题描述】:

我已经搜索并搜索了解决此问题的方法,但无济于事。

我正在使用 php 邮件程序发送一个混合文本/html 电子邮件,以 utf8 编码。以下是相关代码:

$headers = "From: $fromPerson\r\n" .
"Content-Transfer-Encoding: 8bit\r\n".
"Reply-To: $fromPerson\r\n" .
"Content-Type: multipart/alternative; boundary=". $mime_boundary_header. "\r\n" .
"X-Mailer: PHP/" . phpversion();

$message = "$notice_text

--$mime_boundary
Content-Type: text/plain; charset='UTF-8'
Content-Transfer-Encoding: 8bit

$textEmail

--$mime_boundary
Content-Type: text/html; charset='UTF-8'
Content-Transfer-Encoding: 8bit

$htmlEmail

--$mime_boundary--";

//mb_detect_encoding($message) returns UTF-8
$mail_sent = @mail( $to, $subject, $message, $headers);

这些消息包含西班牙语以及那些棘手的字符。电子邮件在 gmail、hotmail(在线 Outlook)、mac 邮件、电话等中显示正常,但在 Windows live mail 或 Microsoft Outlook 中显示不佳。

如果我手动将 Windows live Mail 中的默认字体设置为 utf8,则邮件会正确显示,否则不会。如果我将电子邮件从另一个客户端转发到 Outlook 或 windows live,它也显示正常。

我确定我可以找到解决方法,但我错过了什么吗?我不想依赖接收者知道如何更改消息的编码,所以我需要在电子邮件中添加一些内容以提示这些客户端识别编码吗?

如果此问题已在其他地方处理,我深表歉意,我将不胜感激。看起来我应该继续使用 PHPMailer 看看是否能解决问题,但出于个人好奇心,了解为什么会发生这种情况会很有趣......

【问题讨论】:

  • 是否需要 ' 包装字符集?

标签: php email encoding utf-8


【解决方案1】:

我不确定包装字符集的' 是否必要,甚至是否正确。尝试删除它们:

Content-Type: text/plain; charset=UTF-8

【讨论】:

  • 你好!谢谢你回到我身旁!是的,你是对的,删除引号解决了这个问题。你赢得了我的钦佩和永恒的忠诚。 (编辑是因为最初我认为它不起作用......)
【解决方案2】:
$headers  = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: example@example.com\r\n";
$headers .= "Reply-To: example@example.com\r\n";

【讨论】:

    【解决方案3】:

    对 Riko 的回答进行修改可以使代码更简洁。

    $header_array = [
        "MIME-Version: 1.0",
        "Content-type: text/html; charset=UTF-8",
        "From: example@example.com",
        "Reply-To: example@example.com"
    ];
    
    $headers = implode("\r\n", $header_array);
    

    【讨论】:

      猜你喜欢
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 2015-03-17
      • 1970-01-01
      • 2013-04-21
      • 2012-11-25
      • 2017-07-15
      • 2020-08-07
      相关资源
      最近更新 更多