使用非英文字符发送电子邮件时,为什么有些字符显示为问号?
我尝试使用 PHP 的 mail
功能发送包含非英文字符的文本电子邮件。但相反,我的信息中出现了看起来很有趣的垃圾字符。我该如何修复它?
我使用这段代码:
function _mail($to, $subject, $content)
{
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $content, $headers);
}
有些字符以问号的形式出现......
I tried to send a text email with non-English characters using PHPs mail
function. But instead my message went with funny looking garbage characters. How do I fix it?
I use this piece of code:
function _mail($to, $subject, $content)
{
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $content, $headers);
}
Some of the characters came out as question marks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这绝对是 Joel 文章 每个软件开发人员绝对必须了解 Unicode 的绝对最低限度的情况字符集(没有任何借口!)。
必须先了解字符编码的作用,才能成功解决这个问题。
This is definitely a case for Joel's article The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!).
You must understand the role of character encodings before you can successfully solve this problem.
诸如 Swiftmailer 之类的邮件包装器可能会对您有所帮助。
A mail wrapper such as Swiftmailer might help you.
关键是要使用UTF-8字符集。
添加内容类型:text/html; charset=UTF-8、
MIME-Version 1.0
和Content-Transfer-Encoding:quoted-printable
添加到邮件标头,如下所示:使用 HTML 而不是文本,您还需要将
META
标记添加到 (X)HTML 邮件的HEAD
中:The key is to use the UTF-8 character set.
Add
Content-Type: text/html; charset=UTF-8
,MIME-Version 1.0
andContent-Transfer-Encoding: quoted-printable
to the mail headers, like this:If you would be using HTML instead of text, you’d also need to add a
META
tag to theHEAD
of your (X)HTML mail:您可能想查看 PEAR 的 MAIL_MIME
You might want to check out PEAR's MAIL_MIME