php 如何发送 UTF-8 电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7266935/
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
How to send UTF-8 email?
提问by deepWebMie
When I send out the email, the email does not show characters other than english. It does show like below:
当我发送电子邮件时,电子邮件不显示英文以外的字符。它确实显示如下:
????”?????
????”????
May know actually what cause this? Even I tried to added Content-type and charset in the script, it still show the same.
可知道究竟是什么原因造成的?即使我尝试在脚本中添加 Content-type 和字符集,它仍然显示相同。
I used Mail::Factory("mail");
我用了 Mail::Factory("mail");
回答by s.webbandit
You can add header "Content-Type: text/html; charset=UTF-8" to your message body.
您可以在邮件正文中添加标题“Content-Type: text/html; charset=UTF-8”。
$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()
函数 $headers 数组将是第 4 个参数
mail($to, $subject, $message, $headers)
If you user PEAR Mail::factory() code will be:
如果您使用 PEAR Mail::factory() 代码将是:
$smtp = Mail::factory('smtp', $params);
$mail = $smtp->send($to, $headers, $body);
回答by deepWebMie
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:
我使用的是相当指定的字符集 (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.
我做了另一个邮件功能,因为苹果设备无法很好地读取以前的版本。
function utf8mail($to,$s,$body,$from_name="x",$from_a = "[email protected]", $reply="[email protected]")
{
$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);
}
回答by vml vml
If not HTML
, then UTF-8
is not recommended. koi8-r
and windows-1251
only without problems. So use html mail.
如果不是HTML
,那么UTF-8
不推荐。koi8-r
而windows-1251
只有没有问题。所以使用html邮件。
$headers['Content-Type']='text/html; charset=UTF-8';
$body='<html><head><meta charset="UTF-8"><title>ESP Notufy - ESP Сообщения</title></head><body>'.$text.'</body></html>';
$mail_object=& Mail::factory('smtp',
array ('host' => $host,
'auth' => true,
'username' => $username,
'password' => $password));
$mail_object->send($recipents, $headers, $body);
}