PHPMailer字符编码问题

kpbpu008  于 2023-02-07  发布在  PHP
关注(0)|答案(9)|浏览(133)

我尝试使用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(),但没有运气。

xwbd5t1u

xwbd5t1u1#

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

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

应改为:

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

放在**类的示例化之后(new之后)。属性区分大小写!请查看PHPMailer doc的列表和准确拼写。

另外,PHPMailer的默认编码是8bit,这对于UTF-8数据可能会有问题。要解决这个问题,您可以:

$mail->Encoding = 'base64';

请注意'quoted-printable'可能也适用于这些情况(甚至'binary')。更多细节请参阅RFC1341 - Content-Transfer-Encoding Header Field

r6hnlfcb

r6hnlfcb2#

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

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

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

bakd9h0s3#

我就是这样工作的

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

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

lb3vh1jj

lb3vh1jj4#

当以上都不起作用时,邮件仍显示为ª הודפסה ×•× ×©×œ

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

kgqe7b3p5#

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

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

应改为:

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

5n0oy7gb6#

我ó在$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);

希望有帮助。

jm81lzqq

jm81lzqq7#

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

44u64gxh8#

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

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

ifmq2ha29#

为了避免使用类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();

相关问题