使用非英文字符发送电子邮件时,为什么有些字符显示为问号?

发布于 2024-08-12 11:55:25 字数 613 浏览 4 评论 0原文

我尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半透明的墙 2024-08-19 11:55:25

这绝对是 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.

夏末 2024-08-19 11:55:25

诸如 Swiftmailer 之类的邮件包装器可能会对您有所帮助。

A mail wrapper such as Swiftmailer might help you.

濫情▎り 2024-08-19 11:55:25

关键是要使用UTF-8字符集。

添加内容类型:text/html; charset=UTF-8、MIME-Version 1.0Content-Transfer-Encoding:quoted-printable 添加到邮件标头,如下所示

$headers = 'From: [email protected]' . "\r\n" .
           'Reply-To: [email protected]' . "\r\n" .
           'Content-Type: text/html; charset=UTF-8' . "\r\n" .
           'MIME-Version: 1.0' . "\r\n" .
           'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
           'X-Mailer: PHP/' . phpversion(); // Why would you want to send this header?

:使用 HTML 而不是文本,您还需要将 META 标记添加到 (X)HTML 邮件的 HEAD 中:

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />

The key is to use the UTF-8 character set.

Add Content-Type: text/html; charset=UTF-8, MIME-Version 1.0 and Content-Transfer-Encoding: quoted-printable to the mail headers, like this:

$headers = 'From: [email protected]' . "\r\n" .
           'Reply-To: [email protected]' . "\r\n" .
           'Content-Type: text/html; charset=UTF-8' . "\r\n" .
           'MIME-Version: 1.0' . "\r\n" .
           'Content-Transfer-Encoding: quoted-printable' . "\r\n" .
           'X-Mailer: PHP/' . phpversion(); // Why would you want to send this header?

If you would be using HTML instead of text, you’d also need to add a META tag to the HEAD of your (X)HTML mail:

<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
物价感观 2024-08-19 11:55:25

您可能想查看 PEAR 的 MAIL_MIME

You might want to check out PEAR's MAIL_MIME

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文