使用utf-8编码的php邮件在Microsoft邮件客户端中显示不正确


php mail with utf-8 encoding displays incorrectly in Microsoft mail clients

我一直在寻找解决这个问题的方法,但是无济于事。

我使用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 mail、手机等中显示良好,但在Windows live mail或Microsoft outlook中显示不佳。

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

我肯定可以找到工作,但是我错过了什么吗?我不想依赖接收者知道如何更改消息的编码,所以我需要在电子邮件中添加一些东西来提示这些客户端识别编码吗?

如果这件事已经在其他地方处理过,我很抱歉,我将感谢任何建议。看起来我应该继续使用PHPMailer看看是否解决了这个问题,但出于个人的好奇心,了解为什么会发生这种情况会很有趣…

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

Content-Type: text/plain; charset=UTF-8
$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";

对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);