简体   繁体   English

使用PHP的电子邮件标头的UTF-8字符编码

[英]UTF-8 character encoding for email headers with PHP

I'm trying to encode a → (Right arrow, → or unicode 2192 hex) into an email subject line. 我正在尝试将→(右箭头, →或unicode 2192十六进制)编码为电子邮件主题行。

When I use php's mb_encode_mimeheader() I get a different value to when I do the same thing with Thunderbird or Gmail. 当我使用php的mb_encode_mimeheader()时,当我使用Thunderbird或Gmail做同样的事情时,我会得到不同的值。 But when the php-generated email arrives, the character is not properly displayed. 但是当php生成的电子邮件到达时,角色没有正确显示。 Also, PHP's mb_decode_mimeheader() works on the output from PHP, but not to decode content from the other email sources. 另外,PHP的mb_decode_mimeheader()适用于PHP的输出,但不能解码来自其他电子邮件源的内容。

By way of a hex dump, I've worked out that a UTF-8 representation of the arrow is 通过十六进制转储,我已经确定了箭头的UTF-8表示

<?php
$rarr = "\xe2\x86\x92";

mb_encode_mimeheader($rarr, 'UTF-8'); //     =?UTF-8?B?w6LChsKS?=
// whereas Tbird and Gmail produce:          =?UTF-8?B?4oaS?=
// and more manually:
'=?UTF-8?B?' . base64_encode($rarr).'?='; // =?UTF-8?B?4oaS?=

PHP's encoding comes out in Thunderbird and Gmail as: â PHP的编码在Thunderbird和Gmail中出现为:â

I am completely confused by PHP's behaviour as it does not appear to be producing standard results. 我对PHP的行为感到困惑,因为它似乎没有产生标准结果。

How can I get PHP to encode a UTF-8 email header value so that it will be properly decoded by mail clients? 如何让PHP编码UTF-8电子邮件头值,以便邮件客户端正确解码?

Seems there is a bug that ignores the second parameter, I get the correct result when I add internal encoding: 似乎有一个忽略第二个参数的错误,当我添加内部编码时,我得到了正确的结果:

<?php
$rarr = "\xe2\x86\x92";
mb_internal_encoding( "UTF-8");
echo mb_encode_mimeheader($rarr, 'UTF-8'); //=?UTF-8?B?4oaS?=

But

<?php
$rarr = "\xe2\x86\x92";

mb_encode_mimeheader($rarr, 'UTF-8'); //=?UTF-8?B?w6LChsKS?=

Just setting internal encoding is enough: 只需设置内部编码即可:

<?php
$rarr = "\xe2\x86\x92";
mb_internal_encoding( "UTF-8");
echo mb_encode_mimeheader($rarr); //=?UTF-8?B?4oaS?=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM