教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 PHPMailer邮件标题中文乱码的解决方法

PHPMailer邮件标题中文乱码的解决方法

发布时间:2016-01-15   编辑:jiaochengji.com
PHPMailer是个相当不错的邮件发送类,不足的是中文邮件标题经常乱码,为大家提供该问题的解决方法,供大家学习参考。

PHPMailer发送邮件的代码如下。
 

复制代码 代码示例:

<?php
/**
 * PHPMailer邮件发送
 * Edit www.jbxue.com
*/
 function smtp_main_send( $to, $subject, $message, $from, $fromName )
 {
   $mail = new PHPMailer();

   $mail->CharSet = "UTF-8"; // 设置编码

   $mail->IsSMTP(); // 设置使用SMTP服务发送
   $mail->Host = "smtp.mail.com";
   $mail->Username = "user";
   $mail->Password = "pass";
   $mail->SMTPAuth = true;

   $mail->From = $from;
   $mail->FromName = $fromName;

   if ( is_array( $to ) ) {
     foreach ( $to as $address ) {
       $mail->AddAddress( $address );
     }
   } else {
     $mail->AddAddress( $to );
   }

   $mail->Subject = $subject;
   $mail->Body = $message;
   $mail->AltBody = $message;
   $mail->IsHTML( true );

   return $mail->Send();
 }
?>

用以上的代码发送英文邮件没有问题,但发送中文邮件时标题会有乱码。

解决方法:
需要对 class.phpmailer.php 做一些修改:

修改1,1137 行:
function EncodeHeader ($str, $position = 'text') {

将函数增加一个参数:
 

复制代码 代码示例:
function EncodeHeader ($str, $position = 'text', $pl = 0) {
   if ( $pl ) return "=?" . $this->CharSet . "?B?" . base64_encode($str) . "?=";

修改2,796 行:
$result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject)));

将调用改为:
 

复制代码 代码示例:
$result .= $this->HeaderLine("Subject", $this->EncodeHeader(trim($this->Subject),'text', 1));
 

即可解决中文标题乱码的问题。

附,PHPMailer邮件发送类V5.1下载地址。
 

您可能感兴趣的文章:
PHPMailer发送邮件的实例分享
phpmailer发送gmail邮件的例子
phpmailer发送网易126邮箱的例子
phpmailer发送yahoo邮件的例子
phpmailer类实现邮件群发的实例代码
PHPMailer发送邮件代码实例(ubuntu系统)
PHPMailer发送带附件邮件的例子
PHPMailer收发邮件标题、发件人、内容乱码问题的终极解决方法
PHPmailer发送邮件及乱码问题的解决
PHPMailer发送邮件中文附件名乱码的解决办法
PHPMailer邮件类发送邮件举例(163邮箱)
phpmailer 发送邮件中文乱码问题的解决方法总结
phpmailer发送邮件及实现密码找回功能的代码
PHPmailer邮件群发的入门例子
PHPmailer 邮件群发的范例参考
phpmailer发邮件中文乱码问题如何解决
phpmailer 类发送邮件乱码解决方法
PHPMailer批量发送邮件的实例代码
有关phpmailer的用法
php使用phpMailer发送邮件的例子
phpmailer实现的简单openvpn用户认证的代码
PHPMailer 中文使用说明
phpmailer发送邮件的例子

您可能感兴趣的文章:
phpmailer发送yahoo邮件的例子
phpmailer 类发送邮件乱码解决方法
PHPmailer邮件群发的入门例子
PHPMailer发送邮件代码实例(ubuntu系统)
PHPMailer发送邮件中文附件名乱码的解决办法
PHPMailer邮件标题中文乱码的解决方法
phpmailer发送gmail邮件的例子
phpmailer 发送邮件中文乱码问题的解决方法总结
phpmailer发送网易126邮箱的例子
phpmailer发邮件中文乱码问题如何解决

关键词: PHPMailer  发送邮件  中文乱码   
[关闭]
~ ~