【发布时间】:2014-01-16 17:31:39
【问题描述】:
我有一个相当简单的功能来发送电子邮件。 我开始实现电子邮件的翻译版本,随之而来的是特殊字符,例如 é 和 ó。每当我在电子邮件的主题中包含这些内容时,该电子邮件都会在我的 amavis 中引发 BAD_HEADER 错误,从而造成麻烦。
显然它不是 8bit 编码的,这首先是有道理的。但是,我在网上找不到任何指南或解释如何正确编码主题。
只是为了好玩,我尝试了 é 而不是 é,当然问题已经解决了。但与此同时,电子邮件的主题是 é,而不是 é。
这是我目前拥有的脚本:
function sendEmail() {
// Build HTML version
ob_start();
include('emailhtml.php');
$msgHTML = ob_get_contents();
ob_end_clean();
// Build TXT version
ob_start();
include('email.php');
$msgTxt = ob_get_contents();
ob_end_clean();
// Subject & headers
$subject = "áéíóú";
$boundary = md5(uniqid(rand()));
$headers = array();
$headers[] = "MIME-Version: 1.0";
$headers[] = "Content-Type: multipart/alternative; boundary = ".$boundary;
$headers[] = "From: ".$from." <".$from_email.">";
$headers[] = "Reply-To: ".$reply2_email;
// Plain text version of message
$body = "--$boundary\r\n" .
"Content-Type: text/plain; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($msgTxt));
// HTML version of message
$body .= "--$boundary\r\n" .
"Content-Type: text/html; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: base64\r\n\r\n";
$body .= chunk_split(base64_encode($msgHTML));
$body .= "--$boundary--\r\n";
// BAM! Shoot it off...
mail($receiver, $subject, $body, implode("\r\n", $headers));
}
【问题讨论】:
-
您需要使用带引号的可打印或 base64 对主题进行编码。这是一个例子:stackoverflow.com/questions/13645869/…
-
@grebneke 谢谢,我正在尝试这个解决方案。
-
@grebneke 我尝试使用您的链接中给出的这个quoted_printable_encode()。但是当我使用它时,我的脚本完全停止了。 ://