我有一个非常简单的发送电子邮件的功能。我开始实现电子邮件的翻译版本,并出现了特殊字符,例如é和ó。每当我在电子邮件主题中包含这些字符时,它就会导致我的amavis出现BAD_HEADER错误。
显然,它不是8位编码的,这一点起初是有道理的。然而,我找不到任何关于如何正确编码主题的指南或说明。
只是为了好玩,我尝试使用é代替é,当然问题被解决了。但同时,电子邮件的主题中出现了é,而不是é。
这是我目前拥有的脚本:
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));
}