一边这种问题出现的原因是 mail() 函数的 header 参数里少了 Content-type: text/html; charset=utf-8,或者 charset 不是utf-8。很多国外的PHP程序一开始开发的时候没有吧中文考虑进去,所以中文使用的时候就会出现乱码。
另外我在网路上看到另外一篇文章也是将这个问题,但是我没有去试,暂且把它拷贝过来。
php中mail()函数标题中中文出现乱码的解决
今天用mail函数总是乱码,找了下,用base64_encode解决,如下
先用函数base64_encode() 使用 MIME base64 对数据进行编码
标题字符串前加编码类型例如: =?UTF-8?B?
当然如果是gb2312的话就 =?GB2312?B?
标题字符串后加:?=
举例如下
$to = 'xinple@example.com'; $subject = "=?UTF-8?B?".base64_encode('邮件标题')."?="; $headers = 'MIME-Version: 1.0' . "\r\n"; $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n"; // Additional headers $headers .= 'To: Xinple <<a href="mailto:xinple@example.com">xinple@example.com</a>>' . "\r\n"; $headers .= 'From: Admin <<a href="mailto:admin@example.com">admin@example.com</a>>' . "\r\n"; $headers .= 'Reply-To: Xinple <xinple@example>' . "\r\n"; mail($to, $subject, $message, $headers);

Posted on 18,四 |
Posted by 姚 迎迎 











There are no comments yet