PHPMailer 字符编码问题

问题描述 投票:0回答:6

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

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)都使用不同的编码。

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

我尝试转换邮件标题宽度

mb_convert_encoding()
,但没有成功。

php email phpmailer
6个回答
631
投票

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

另请注意

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

应替换为:

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

And 放置于类的实例化之后(在

new
之后)。属性区分大小写!请参阅 PHPMailer 文档 了解列表和确切的拼写。

PHPMailer 的默认编码是

8bit
,这对于 UTF-8 数据可能会出现问题。要解决此问题,您可以执行以下操作:

$mail->Encoding = 'base64';

请注意,

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


37
投票
$mail -> CharSet = "UTF-8";
$mail = new PHPMailer();

$mail -> CharSet = "UTF-8";
必须位于
$mail = new PHPMailer();

之后

试试这个

$mail = new PHPMailer();
$mail->CharSet = "UTF-8";

7
投票

我自己就是这样工作的

  $mail->FromName = utf8_decode($_POST['name']);

http://php.net/manual/en/function.utf8-decode.php


6
投票

很抱歉在聚会上迟到了。根据您的服务器配置,您可能需要严格使用小写字母 utf-8 指定字符,否则将被忽略。如果您最终在这里寻找解决方案并且上面的答案都没有帮助,请尝试此操作:

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

应替换为:

$mail->CharSet = "utf-8";

6
投票

当上述方法均不起作用时,邮件仍然看起来像

ª הודפסה ×•× ×©×œ
:

$mail->addCustomHeader('Content-Type', 'text/plain;charset=utf-8');
$mail->Subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';;

3
投票

我在 $mail->Subject /w PHPMailer 中收到 ó

所以对我来说完整的解决方案是:

// Your Subject with tildes. Example.
$someSubjectWithTildes = 'Subscripción España';

$mailer->CharSet = 'UTF-8';
$mailer->Encoding = 'quoted-printable';
$mailer->Subject = html_entity_decode($someSubjectWithTildes);

希望有帮助。

© www.soinside.com 2019 - 2024. All rights reserved.