本文介绍了PHPMailer 字符编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用PHPMailer发送注册、激活.等邮件给用户:

I try to use PHPMailer to send registration, activation. etc mail to users:

require("class.phpmailer.php");
$mail -> charSet = "UTF-8";
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->Host     = "smtp.mydomain.org";
$mail->From     = "[email protected]";
$mail->SMTPAuth = true;
$mail->Username ="username";
$mail->Password="passw";
//$mail->FromName = $header;
$mail->FromName = mb_convert_encoding($header, "UTF-8", "auto");
$mail->AddAddress($emladd);
$mail->AddAddress("[email protected]");
$mail->AddBCC('[email protected]', 'firstadd');
$mail->Subject  = $sub;
$mail->Body = $message;
$mail->WordWrap = 50;
if(!$mail->Send()) {
   echo 'Message was not sent.';
   echo 'Mailer error: ' . $mail->ErrorInfo;
}

$message 包含拉丁字符.不幸的是,所有网络邮件(gmail、webmail.mydomain.org、emailaddress.domain.xx)都使用不同的编码.

The $message contains latin characters. Unfortunately all webmail (gmail, webmail.mydomain.org, emailaddress.domain.xx) is using a different coding.

如何强制使用 UTF-8 编码以在所有邮箱上显示完全相同的邮件?

How can I force to use UTF-8 coding to show my mail exactly the same on all mailboxes?

我尝试转换邮件标题宽度 mb_convert_encoding(),但没有成功.

I tried to convert the mail header width mb_convert_encoding(), but without luck.

推荐答案

如果您 100% 确定 $message 包含 ISO-8859-1,您可以使用 utf8_encode 正如大卫所说.否则使用 mb_detect_encodingmb_convert_encoding 在 $message 上.

If you are 100% sure $message contain ISO-8859-1 you can use utf8_encode as David says. Otherwise use mb_detect_encoding and mb_convert_encoding on $message.

还要注意

$mail -> charSet = "UTF-8";

应替换为:

$mail->CharSet = 'UTF-8';

放在之后类的实例化(在new之后).属性区分大小写!请参阅列表中的 PHPMailer 文档准确拼写.

And placed after the instantiation of the class (after the new). The properties are case sensitive! See the PHPMailer doc fot the list & exact spelling.

此外,PHPMailer 的默认编码是 8bit,对于 UTF-8 数据可能会出现问题.要解决此问题,您可以执行以下操作:

Also the default encoding of PHPMailer is 8bit which can be problematic with UTF-8 data. To fix this you can do:

$mail->Encoding = 'base64';

请注意,'quoted-printable' 在这些情况下也可能会起作用(甚至可能是 'binary').有关更多详细信息,您可以阅读 RFC1341 - Content-Transfer-Encoding Header Field.

Take note that 'quoted-printable' would probably work too in these cases (and maybe even 'binary'). For more details you can read RFC1341 - Content-Transfer-Encoding Header Field.

这篇关于PHPMailer 字符编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!