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

<?php
$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 =  "<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("xxx@xxx.cn", $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/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值