一个客户的PHP网站需要一个邮件订阅功能,我采用一个在PHP开源程序中被大量使用的程序 PHPmailer(PHPMailer_v2.0.4),在正确填写examples文件夹下的test_mail.php后测试邮件系统,被测试邮箱 可以收到邮件,不过title标题却一直是乱码,故想到这个肯定是PHP的编码问题,经过多次调试终于找到解决办法:核心问题就是加 入$mail->CharSet = "UTF-8";以下是个人测试成功的phpmailer邮件系统
1.对于所有的网易的邮箱(163.com,126.com,yeah.net等),一旦发送中文(标题和正文)都会出现乱码;
2.还有,别的邮箱在发中文时,正文都能正常显示,而标题还出现乱码;
实例:
test.php
代码如下 |
复制代码 |
phpmailer Unit Test
请你输入收信的邮箱地址:
|
send.php
代码如下 |
复制代码 |
require("class.phpmailer.php");
$mail = new PHPMailer();
$mail->CharSet = "gb2312"; /
$mail->Encoding = "base64";
$address = $_POST['address'];
$mail->IsSMTP(); // set mailer to use SMTP
$mail->Host = "smtp.126.com"; // specify main and backup server
$mail->SMTPAuth = true; // turn on SMTP authentication
$mail->Username = ""; // SMTP username
$mail->Password = "******"; // SMTP password
$mail->From = "";
$mail->FromName = "rokaye";
$mail->AddAddress("$address", "");
//$mail->AddAddress(""); // name is optional
//$mail->AddReplyTo("", "");
//$mail->WordWrap = 50; // set word wrap to 50 characters
//$mail->AddAttachment("/var/tmp/file.tar.gz"); // add attachments
//$mail->AddAttachment("/tmp/image.jpg", "new.jpg"); // optional name
//$mail->IsHTML(true); // set email format to HTML
$mail->Subject = "PHPMailer测试邮件";
$mail->Body = "Hello,这是rokaye的测试邮件";
$mail->AltBody = "This is the body in plain text for non-HTML mail clients";
if(!$mail->Send())
{
echo "Message could not be sent. ";
echo "Mailer Error: " . $mail->ErrorInfo;
exit;
}
echo "Message has been sent";
?>
|
解决方法一
$mail = new PHPMailer();
$mail->CharSet = "GB2312"; // 设置字符集编码 utf-8
$mail->Encoding = "base64";//设置文本编码方式
如果标题错误可
$mail->Subject = "=?UTF-8?B?" . base64_encode("我的邮件") . "?=";