如何发送UTF-8电子邮件?

时间:2021-08-20 10:56:21

When I send out the email, the email does not show characters other than english. It does show like below:

当我发送电子邮件时,电子邮件不会显示除英语以外的字符。它确实如下所示:

余生ä»ä»

一个½™c”ÿa»»

May know actually what cause this? Even I tried to added Content-type and charset in the script, it still show the same.

可能知道究竟是什么原因造成的?即使我尝试在脚本中添加内容类型和字符集,它仍然显示相同的内容。

I used Mail::Factory("mail");

我使用邮件:工厂(“邮件”);

2 个解决方案

#1


87  

You can add header "Content-Type: text/html; charset=UTF-8" to your message body.

您可以添加标题“内容类型:文本/html;给你的消息正文。

$headers = "Content-Type: text/html; charset=UTF-8";

If you use native mail() function $headers array will be the 4th parameter mail($to, $subject, $message, $headers)

如果使用本机mail()函数,$header数组将是第四个参数邮件($to, $subject, $message, $header)

If you user PEAR Mail::factory() code will be:

如果您使用PEAR邮件:::factory()代码为:

$smtp = Mail::factory('smtp', $params);

$mail = $smtp->send($to, $headers, $body);

#2


27  

I'm using rather specified charset (ISO-8859-2) because not every mail system (for example: http://10minutemail.com) can read UTF-8 mails. If you need this:

我使用了相当指定的charset (ISO-8859-2),因为不是每个邮件系统(例如:http://10minutemail.com)都可以读取UTF-8邮件。如果你需要:

function utf8_to_latin2($str)
{
    return iconv ( 'utf-8', 'ISO-8859-2' , $str );
}
function my_mail($to,$s,$text,$form, $reply)
    {
        mail($to,utf8_to_latin2($s),utf8_to_latin2($text),
        "From: $form\r\n".
        "Reply-To: $reply\r\n".
        "X-Mailer: PHP/" . phpversion());
    }

I have made another mailer function, because apple device could not read well the previous version.

我做了另一个mailer函数,因为苹果设备不能很好地阅读前一个版本。

function utf8mail($to,$s,$body,$from_name="x",$from_a = "info@x.com", $reply="info@x.com")
{
    $s= "=?utf-8?b?".base64_encode($s)."?=";
    $headers = "MIME-Version: 1.0\r\n";
    $headers.= "From: =?utf-8?b?".base64_encode($from_name)."?= <".$from_a.">\r\n";
    $headers.= "Content-Type: text/plain;charset=utf-8\r\n";
    $headers.= "Reply-To: $reply\r\n";  
    $headers.= "X-Mailer: PHP/" . phpversion();
    mail($to, $s, $body, $headers);
}

#1


87  

You can add header "Content-Type: text/html; charset=UTF-8" to your message body.

您可以添加标题“内容类型:文本/html;给你的消息正文。

$headers = "Content-Type: text/html; charset=UTF-8";

If you use native mail() function $headers array will be the 4th parameter mail($to, $subject, $message, $headers)

如果使用本机mail()函数,$header数组将是第四个参数邮件($to, $subject, $message, $header)

If you user PEAR Mail::factory() code will be:

如果您使用PEAR邮件:::factory()代码为:

$smtp = Mail::factory('smtp', $params);

$mail = $smtp->send($to, $headers, $body);

#2


27  

I'm using rather specified charset (ISO-8859-2) because not every mail system (for example: http://10minutemail.com) can read UTF-8 mails. If you need this:

我使用了相当指定的charset (ISO-8859-2),因为不是每个邮件系统(例如:http://10minutemail.com)都可以读取UTF-8邮件。如果你需要:

function utf8_to_latin2($str)
{
    return iconv ( 'utf-8', 'ISO-8859-2' , $str );
}
function my_mail($to,$s,$text,$form, $reply)
    {
        mail($to,utf8_to_latin2($s),utf8_to_latin2($text),
        "From: $form\r\n".
        "Reply-To: $reply\r\n".
        "X-Mailer: PHP/" . phpversion());
    }

I have made another mailer function, because apple device could not read well the previous version.

我做了另一个mailer函数,因为苹果设备不能很好地阅读前一个版本。

function utf8mail($to,$s,$body,$from_name="x",$from_a = "info@x.com", $reply="info@x.com")
{
    $s= "=?utf-8?b?".base64_encode($s)."?=";
    $headers = "MIME-Version: 1.0\r\n";
    $headers.= "From: =?utf-8?b?".base64_encode($from_name)."?= <".$from_a.">\r\n";
    $headers.= "Content-Type: text/plain;charset=utf-8\r\n";
    $headers.= "Reply-To: $reply\r\n";  
    $headers.= "X-Mailer: PHP/" . phpversion();
    mail($to, $s, $body, $headers);
}