235

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

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

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

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

4

9 回答 9

558

如果你 100% 确定 $message 包含 ISO-8859-1,你可以使用utf8_encode,就像 David 说的那样。否则 在 $message 上使用mb_detect_encodingmb_convert_encoding 。

另请注意

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

应替换为:

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

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

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

$mail->Encoding = 'base64';

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

于 2010-03-22T14:36:17.407 回答
28
$mail -> CharSet = "UTF-8";
$mail = new PHPMailer();

$mail -> CharSet = "UTF-8";必须在之后$mail = new PHPMailer();

试试这个

$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
于 2013-05-06T14:00:30.757 回答
6

我自己这样工作

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

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

于 2013-04-30T03:08:13.653 回答
5

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

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

应替换为:

$mail->CharSet = "utf-8";
于 2016-02-09T15:28:29.613 回答
3

我越来越ó 在 $mail-> 主题 /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);

希望能帮助到你。

于 2018-11-27T10:42:54.160 回答
3

当上述方法均无效时,邮件仍然如下所示ª הודפסה ×•× ×©×œ

$mail->addCustomHeader('Content-Type', 'text/plain;charset=utf-8');
$mail->Subject = '=?UTF-8?B?' . base64_encode($subject) . '?=';;
于 2020-02-12T16:03:24.137 回答
1
$mail = new PHPMailer();
$mail->CharSet = "UTF-8";
$mail->Encoding = "16bit";
于 2016-05-28T04:59:40.630 回答
0

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

$mail->CharSet = "UTF-8"
于 2015-12-04T03:07:19.370 回答
0

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

$mail = new PHPMailer();
$mail->From = 'midireccion@email.com';
$mail->FromName = 'Mi nombre';
$mail->AddAddress('emaildestino@email.com');
$mail->Subject = 'Prueba';
$mail->Body = '';
$mail->IsHTML(true);


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


// Send mail
$mail->Send();
于 2017-03-13T07:18:46.713 回答