PHPMailer字符编码问题 [英] PHPMailer character encoding issues

查看:91
本文介绍了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     = "name@mydomain.org";
$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("mytest@gmail.com");
$mail->AddBCC('mytest2@mydomain.org', '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,则可以使用 mb_detect_encoding

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标头字段.

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字符编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆