Home > php教程 > php手册 > php mail发邮件标题中文乱码的问题解决办法

php mail发邮件标题中文乱码的问题解决办法

WBOY
Release: 2016-05-25 16:49:17
Original
1283 people have browsed it

当使用下面的PHP语句发送电子邮件的时候,如果编码和接收邮箱编码不相同,会发现邮件的标题是乱码,而邮件正文却是正确的,如何才能使得邮件标题不是乱码呢?

$subject = stripslashes($the_post['Title']);  
$headers = "MIME-Version: 1.0\r\n";  
$headers .= "Content-type: text/plain; charset=utf-8\r\n";  
$headers .= "Content-Transfer-Encoding: 8bit\r\n";  
$message = stripslashes(strip_tags($the_post['Content']));  
mail($to, $subject, $message, $headers);
Copy after login

先用函数base64_encode() — 使用 MIME base64 对数据进行编码,标题字符串前加编码类型例如: =?UTF-8?B?,标题字符串后加:?=,例如:

$subject = "=?UTF-8?B?".base64_encode($subject)."?=";

将上面一句添加到代码之中,这样,发送的中文邮件标题就不是乱码了,代码如下:

<?php  
$to    = &#39;junhuibai@gmail.com&#39;;  
$subject = iconv(&#39;&#39;,&#39;GB2312&#39;,&#39;亲爱的&#39;.$s_user.&#39;,请取回您的密码!&#39;);  
$subject = "=?GB2312?B?".base64_encode($subject)."?=";  
$message = $s_user.&#39;,您好!&#39;.  
&#39;您的新密码是:&#39;.$pwd.&#39;&#39;.  
&#39;为了保证您用户的安全性,请登录更改您的密码。&#39;.  
&#39;此信是由系统发出,系统不接收回信,请勿直接回复!&#39;;  
$headers = &#39;From: junhuibai@tom.com&#39; . "\r\n" .  
&#39;Reply-To:junhuibai@tom.com&#39; . "\r\n" .  
&#39;X-Mailer: PHP/&#39; . phpversion();  
if(mail($to, $subject, $message,$headers))  
echo&#39;ok&#39;;  
else 
echo &#39;no&#39;;  
phpinfo();  
?>
Copy after login

最终解决办法,代码如下:

标题乱码:$subject = "=?UTF-8?B?".base64_encode('邮箱验证 —— ' . SITE_NAME)."?=";

正文乱码,设置header : charset=utf-8'

$headers = &#39;From: webmaster@webinno.cn&#39; . "\r\n" .  
&#39;Content-type: text/html; charset=utf-8&#39; . "\r\n" .  
// &#39;Reply-To: webmaster@example.com&#39; . "\r\n" .  
&#39;X-Mailer: PHP/&#39; . phpversion();
Copy after login


永久链接:

转载随意!带上文章地址吧。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template