[php]mail函数发送邮件(正文+附件+中文)

<?php
$from = "[email protected]";
$to = "[email protected], [email protected]";
$subject = "邮件主题";
$subject = "=?UTF-8?B?".base64_encode($subject)."?=";
$attach_filename = date("Y-m-d") . ".html";

$emailBody =  "
正文第一行
正文第二行
正文第三行
The end!";

# 然后我们要作为附件的HTML文件 
$attachment =  "<html>
<head>
<title>The attached file</title>
</head>
<body>
<h2>This is the attached HTML file</h2>
</body>
</html>";

$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("[email protected]", $subject, $emailBody, $headers);
?> 


按照上例是能发送成功, 但项目中真正使用时, 却出现了乱码的问题:

当正文或附件html一行很长的时候, 收到的内容有乱码和!等异常,  查阅到有人说html邮件一行不能超过80个字符,抱着试一试的态度,

生成html附件内容字符串的时候用类似 $html .= "<tr>";  $html .= "<td>xxx</td>\n"; $html .= "<td>xxxx</td>\n"; $html .= "</tr>";  的方法, html每增加一点内容,加上 \n.既保证邮件body单行不超过80字符,有保证<tr> </tr> <td> </td>不会被wordwrap折断.

经过这么一折腾, 完美解决了乱码.

啊哈...


参考:http://blog.163.com/sunny_526888/blog/static/7989917520084221145942/


你可能感兴趣的:(html,PHP,Date)