$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下载地址。
