$from = "xxx@xxx.cn";
$to = "xxx@xxx.cn, xxx@xxx.com";
$subject = "邮件主题";
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
$attach_filename = date("Y-m-d") . ".html";
$emailBody = "
正文第一行
正文第二行
正文第三行
The end!";
# 然后我们要作为附件的HTML文件
$attachment = "
The attached fileThis is the attached HTML file
";
$boundary = uniqid("");
$headers = "From: $from
To: $to
Content-type: multipart/mixed; boundary=\"$boundary\"";
$emailBody = "--$boundary
Content-type: text/plain; charset=utf-8
Content-transfer-encoding: 8bit
$emailBody
--$boundary
Content-type: text/html; name=$attach_filename
Content-disposition: inline; filename=$attach_filename
Content-transfer-encoding: 8bit
$attachment
--$boundary--";
mail("xxx@xxx.cn", $subject, $emailBody, $headers);
?>
按照上例是能发送成功, 但项目中真正使用时, 却出现了乱码的问题:
当正文或附件html一行很长的时候, 收到的内容有乱码和!等异常, 查阅到有人说html邮件一行不能超过80个字符,抱着试一试的态度,
生成html附件内容字符串的时候用类似 $html .= "
"; $html .= "xxx\n"; $html .= "xxxx\n"; $html .= ""; 的方法, html每增加一点内容,加上 \n.既保证邮件body单行不超过80字符,有保证 不会被wordwrap折断.经过这么一折腾, 完美解决了乱码.
啊哈...