我用PHP的mail()函数发送电子邮件。我需要将from字段编码为UTF-8所以我设置了这些邮件头:
function mail_headers($from, $contentType = "text/plain") {
$headers = "From: ".mime_header_encode($from)."'n";
$headers .= 'Reply-To: '.mime_header_encode($from)."'n";
$headers .= 'MIME-Version: 1.0' ."'n";
$headers .= 'Content-Type: '.$contentType.'; charset=utf-8' ."'n";
$headers .= 'Content-Transfer-Encoding: 8bit'. "'n";
return $headers;
}
function mime_header_encode($text, $encoding = "utf-8") {
return "=?$encoding?Q?" . imap_8bit($text) . "?=";
}
它在我的PC上运行良好,但在生产服务器上不太好。它不能将编码字符串=?utf-8?Q?Website name <info@myshop.com>?=
识别为有效的电子邮件,因此它试图使其更好。服务器将最终发送一封带有header:
From: =?utf-8?Q?Website name <info@myshop.com>,
?=@hosting-domain.com
有了这个标题,电子邮件收件人看到另一个奇怪的发件人(逗号后的字符串)。我能让PHP不碰我的邮件字符串吗?
谢谢!
php函数mb_encode_mimeheader()旨在根据php.ini指令'mbstring.internal_encoding'对mime头进行编码。如果仅仅使用mb_encode_mimeheader()
没有帮助,你应该检查这个指令的正确设置。
你也应该看看我链接的网站上的评论。