通过 PHP 发送的电子邮件中的中文字符不显示

发布于 2024-08-27 15:58:39 字数 2375 浏览 4 评论 0原文

我从我的测试服务器通过 PHP 发送带有中文字符的邮件,并且它完美地发送出去。编码是UTF-8。当我将相同的 PHP 文件上传到另一台服务器并尝试从那里发送电子邮件时,它在一个邮件客户端(实际上是基于网络的邮件,GMail)中看起来 90% 没问题,但在另一个邮件客户端(Apple Mail)中它是所有乱码 - 即使我尝试更改邮件客户端中的编码。

我被困在这里,因为一切在一台服务器上运行良好,但在另一台服务器上则不然。我不知道从哪里开始寻找解决方案。更令人费解的是,在生产服务器上,电子邮件看起来还不错(奇怪的是,某些字符没有显示),但在其他邮件应用程序中,它看起来像垃圾。

知道我可以从哪里开始寻找解决这个问题的方法吗?

PHP

$books = json_decode ($_POST['books']);

$body = '
<body bgcolor="#999999"><center>
<table width="700" border="0" cellspacing="2" cellpadding="10" bgcolor="#FFFFFF">
<tr>
    <td>
    你好 ' . $_POST['name'] . ',<br/><br/>以下是你从学习网站,给孩子讲故事课程,所要求的书籍名单<br/><br/><hr noshade="noshade" />
    </td>
</tr>
<tr>
<td>';

$iLen = count($books);
for ($i = 0; $i < $iLen; $i = $i + 1) {
    $book = $books[$i];

    $body .= '<b>' . $book->title . '</b><br/>' . $book->author . '<br/><br/>';
    $body .= '简介: ' . $book->synopsis . '<br/>';
    $body .= '年龄层: ' . $book->age . '<br/>';
    $body .= '场景: ' . $book->setting . '<br/>';
    $body .= '目的: ' . $book->purpose . '<br/>';
    $body .= '索书号: ' . $book->call . '<br/><br/>';
    $body .= '出版商: ' . $book->publisher . '<br/><br/><hr noshade="noshade" />';

}

$body .= '
</td>
</tr>
<tr>
    <td>
    亲切问侯,<br/>Name
    </td>
</tr>
</table>
</center>
</body>
';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: Name <[email protected]>' . "\r\n";

$ok = mail ($_POST['email'], '讲故事给孩子听:您所要求的故事精选书单', $body, $headers);

结果

ä∏ å¥∏ ryan,

ä»¥ä¸—æ˘¯ä∏ ä»⁄å–œä’ ç∏Œç«˙,ç»˙å–©å–∆è®”æ∑亗è¯≤稗,æ≈•èœ™æ±≠ç˚≥䒜籟å∆ŸåŸ

瑲瑲æ√Œæ˛≈å¤˚ç√±ä∏ 
麜å∑—å¸∞é˝·å°π, å±±å§∫ 

Synopsis: 粗å∂¯ç√±ç˚≥å°∂å∑‰å®ˇå®ˇæ•⁄ä’√牨å®∞ç—¬ç≈’ç˚≥戒åπ∂å‚‘å∑‰åœ√åœ√说å®∞æ˛≈å¤˚ç√±å®∞ã•≠
Age Group: 4 - 6 å”™
Setting: ç≤¤ä∏„
Purpose: ä»·å•πè§≠å‚‘ä¿∞è¿˝äº”å–∆ç˚≥æ≥ƒæ∞∑
Call no: JP MAC

Publisher: 麜å∑—å¸∞é˝·å°π, å±±å§∫. 瑲瑲æ√Œæ˛≈å¤˚ç√±ä∏ .丅海 : 尌咴å≥¿ç«¥åΩºç≈√ç¤≤, 2005.

I send mail via PHP with Chinese characters from my testing server, and it sends it out perfectly. The encoding is UTF-8. When I upload the same PHP file to another server and try to send e-mail from there, it will look 90% fine in one mail client (web-based mail actually, GMail), but in another mail client (Apple Mail) it's all gibberish - even when I try changing the encoding in the mail client.

I'm stuck here, because everything works fine on one server but not another. I'm not sure where to start looking for solutions. What's even more puzzling is that on the production server, the e-mail looks somewhat ok (strange case of some characters not showing), but in other mail apps it looks like garbage.

Any idea where I can start looking to solve this?

PHP

$books = json_decode ($_POST['books']);

$body = '
<body bgcolor="#999999"><center>
<table width="700" border="0" cellspacing="2" cellpadding="10" bgcolor="#FFFFFF">
<tr>
    <td>
    你好 ' . $_POST['name'] . ',<br/><br/>以下是你从学习网站,给孩子讲故事课程,所要求的书籍名单<br/><br/><hr noshade="noshade" />
    </td>
</tr>
<tr>
<td>';

$iLen = count($books);
for ($i = 0; $i < $iLen; $i = $i + 1) {
    $book = $books[$i];

    $body .= '<b>' . $book->title . '</b><br/>' . $book->author . '<br/><br/>';
    $body .= '简介: ' . $book->synopsis . '<br/>';
    $body .= '年龄层: ' . $book->age . '<br/>';
    $body .= '场景: ' . $book->setting . '<br/>';
    $body .= '目的: ' . $book->purpose . '<br/>';
    $body .= '索书号: ' . $book->call . '<br/><br/>';
    $body .= '出版商: ' . $book->publisher . '<br/><br/><hr noshade="noshade" />';

}

$body .= '
</td>
</tr>
<tr>
    <td>
    亲切问侯,<br/>Name
    </td>
</tr>
</table>
</center>
</body>
';

$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";
$headers .= 'From: Name <[email protected]>' . "\r\n";

$ok = mail ($_POST['email'], '讲故事给孩子听:您所要求的故事精选书单', $body, $headers);

Result

ä∏ å¥∏ ryan,

ä»¥ä¸—æ˘¯ä∏ ä»⁄å–œä’ ç∏Œç«˙,ç»˙å–©å–∆è®”æ∑亗è¯≤稗,æ≈•èœ™æ±≠ç˚≥䒜籟å∆ŸåŸ

瑲瑲æ√Œæ˛≈å¤˚ç√±ä∏ 
麜å∑—å¸∞é˝·å°π, å±±å§∫ 

Synopsis: 粗å∂¯ç√±ç˚≥å°∂å∑‰å®ˇå®ˇæ•⁄ä’√牨å®∞ç—¬ç≈’ç˚≥戒åπ∂å‚‘å∑‰åœ√åœ√说å®∞æ˛≈å¤˚ç√±å®∞ã•≠
Age Group: 4 - 6 å”™
Setting: ç≤¤ä∏„
Purpose: ä»·å•πè§≠å‚‘ä¿∞è¿˝äº”å–∆ç˚≥æ≥ƒæ∞∑
Call no: JP MAC

Publisher: 麜å∑—å¸∞é˝·å°π, å±±å§∫. 瑲瑲æ√Œæ˛≈å¤˚ç√±ä∏ .丅海 : 尌咴å≥¿ç«¥åΩºç≈√ç¤≤, 2005.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

故事灯 2024-09-03 15:58:39

也许你应该以 MIME 格式对你的消息进行编码

maybe you should encode your message in MIME format

要走干脆点 2024-09-03 15:58:39

php.ini中“default_charset”的值都是utf8吗?

这可能会导致您所看到的问题。看来您依赖 php 解释器来知道脚本是用 utf-8 编写的,因为您将脚本中的文字 utf-8 字符传递到电子邮件,并且标头中的字符集设置为 utf-8 (这很好)。

Are the values of "default_charset" in the php.ini both utf8?

That might be able to cause the problem you're seeing. It seems you are relying on the php interpreter to know that the script is written in utf-8, as your passing literal utf-8 characters in the script to the email, and the charset is in the headers is set to utf-8 (which is good).

月光色 2024-09-03 15:58:39

您的电子邮件是否包含纯文本和 html 的多部分内容?还是只有html?
如果是前者,请将纯文本的内容类型也设置为utf-8。

如果是单独的html,你可以写一个完整的html,里面指定content-type。

Is you email multi-part of with plain text and html? Or only html?
If it is the former, set the content type of the plain text to utf-8 too.

If it is html alone, you might write a full html with content-type specified inside.

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