php PHPMailer 字符编码问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2491475/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:41:45  来源:igfitidea点击:

PHPMailer character encoding issues

phpphpmailer

提问by AlexV

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

我尝试使用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;  
}

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

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

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

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

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

我试图转换邮件标题宽度mb_convert_encoding(),但没有运气。

回答by AlexV

If you are 100% sure $message contain ISO-8859-1 you can use utf8_encodeas David says. Otherwise use mb_detect_encodingand mb_convert_encodingon $message.

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

Also take note that

还要注意的是

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

Should be replaced by:

应改为:

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

Andplaced afterthe instantiation of the class (after the new). The properties are case sensitive! See the PHPMailer docfot the list & exact spelling.

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

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

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

$mail->Encoding = 'base64';

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.

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

回答by user2354947

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

line $mail -> CharSet = "UTF-8";must be after $mail = new PHPMailer();and with no spaces!

$mail -> CharSet = "UTF-8";必须在后面$mail = new PHPMailer();并且不能有空格!

try this

尝试这个

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

回答by vzr

Sorry for being late on the party. Depending on your server configuration, You may be required to specify character strictly with lowercase lettersutf-8, otherwise it will be ignored. Try this if you end up here searching for solutions and none of answers above helps:

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

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

should be replaced with:

应替换为:

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

回答by yorkfx

I work myself this way

我自己这样工作

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

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

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

回答by ali ebrahimi

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

回答by biojazzard

I was getting óin $mail->Subject /w PHPMailer.

我得到了ó 在 $mail->Subject /w PHPMailer 中。

So for me the complete solution is:

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

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

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

Hope it helps.

希望能帮助到你。

回答by Ohad Cohen

When non of the above works, and still mails looks like a ×”×?דפ×?×” ×?× ×?×?:

当以上都不起作用时,仍然邮件看起来像a ×”×?דפ×?×” ×?× ×?×?

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

回答by Mohammed Alhfian

To avoid problems of character encoding in sending emails using the class PHPMailer we can configure it to send it with UTF-8 character encoding using the "CharSet" parameter, as we can see in the following Php code:

为避免使用 PHPMailer 类发送电子邮件时出现字符编码问题,我们可以使用“CharSet”参数将其配置为使用 UTF-8 字符编码发送,如下面的 PHP 代码所示:

$mail = new PHPMailer();
$mail->From = '[email protected]';
$mail->FromName = 'Mi nombre';
$mail->AddAddress('[email protected]');
$mail->Subject = 'Prueba';
$mail->Body = '';
$mail->IsHTML(true);


// Active condition utf-8
$mail->CharSet = 'UTF-8';


// Send mail
$mail->Send();

回答by Nikos Kapetanakos

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

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

---this line should be under

---这条线应该在

$mail = new PHPMailer(); line.

$mail = new PHPMailer(); line.

pff..

噗..



Yes, it is correct. You have to place it after the object instantiation.

是的,它是正确的。您必须将它放在对象实例化之后。

回答by Trung Bui

The simplest way and will help you with is set CharSet to UTF-8

最简单的方法是将 CharSet 设置为 UTF-8

$mail->CharSet = "UTF-8"