使用 utf-8 编码的 php 邮件在 Microsoft 邮件客户端中显示不正确
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16715071/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php mail with utf-8 encoding displays incorrectly in Microsoft mail clients
提问by Michael Beeson
I've searched and searched for a solution to this but to no avail.
我已经搜索并搜索了解决方案,但无济于事。
I'm using the php mailer to send out a mixed text / html email, encoded in utf8. Here's the relevant code:
我正在使用 php 邮件程序发送以 utf8 编码的混合文本/html 电子邮件。这是相关的代码:
$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);
The messages contain Spanish along with those tricky characters. The emails display fine in gmail, hotmail (online outlook), mac mail, phones etc. but not in Windows live mail or Microsoft outlook.
这些消息包含西班牙语以及那些棘手的字符。电子邮件在 gmail、hotmail(在线 Outlook)、mac 邮件、手机等中显示良好,但在 Windows live mail 或 Microsoft Outlook 中显示不正常。
If I manually set the default font in Windows live Mail to utf8 the message displays correctly, but otherwise it does not. If I forward the email from another client to outlook or windows live it displays fine as well.
如果我手动将 Windows live Mail 中的默认字体设置为 utf8,则消息会正确显示,否则不会。如果我将来自其他客户的电子邮件转发到 Outlook 或 Windows Live,它也会显示正常。
I could find work arounds I'm sure, but am I missing something? I don't want to rely on the receivers knowing how to change the encoding of the message, so is there something I need to add to the email to prompt these clients to recognise the encoding?
我可以确定我可以找到解决方法,但是我错过了什么吗?我不想依赖接收者知道如何更改消息的编码,所以我需要在电子邮件中添加一些东西来提示这些客户端识别编码吗?
I apologise if this has been dealt with elsewhere, and I'll appreciate any advice. It looks like I should just go ahead and use PHPMailer to see if that fixes the problem, but out of personal curiosity it would be interesting to learn why this is happening...
如果这已在其他地方处理过,我深表歉意,我将不胜感激任何建议。看起来我应该继续使用 PHPMailer 来查看是否可以解决问题,但是出于个人的好奇心,了解为什么会发生这种情况会很有趣......
回答by Arnaud Le Blanc
I'm not sure that the '
wrapping the charset are necessary, or even correct. Try removing them:
我不确定'
包装字符集是必要的,甚至是正确的。尝试删除它们:
Content-Type: text/plain; charset=UTF-8
回答by Rashad
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=UTF-8\r\n";
$headers .= "From: [email protected]\r\n";
$headers .= "Reply-To: [email protected]\r\n";
回答by Chris
A modification to Riko's answer makes for a little cleaner code.
对 Riko 答案的修改使代码更简洁。
$header_array = [
"MIME-Version: 1.0",
"Content-type: text/html; charset=UTF-8",
"From: [email protected]",
"Reply-To: [email protected]"
];
$headers = implode("\r\n", $header_array);