PHP中的imap_open()不处理UTF-8


imap_open() in PHP is not handling UTF-8

我正在使用PHP imap函数读取接收UTF-8编码的纯文本电子邮件(由另一个服务器生成)的邮箱。重音字符将被问号(?)替换。下面是我的代码,下面是修复它的两次尝试。如何解决这个问题?我无法控制生成消息的服务器,但它们声称它们是UTF-8编码的。mb_detect_encoding表示imap_body函数返回ASCII字符串,但我发现mb_detect_encoding在过去有些错误。

$connection = imap_open( '{localhost:993/ssl/novalidate-cert}INBOX', 'xxxxxxx', 'xxxxxxx', 0, 1 );
$result = imap_search( $connection, 'UNSEEN' );
if ( $result )
    {
    foreach ( $result as $msgno )
        {
        $body = imap_body( $connection, $msgno );
        // ... (code to process the message) ...
        imap_mail_move( $connection, "$msgno:$msgno", 'INBOX.processed' );
        }
    imap_expunge( $connection );
    imap_close( $connection );
        }
    }

我尝试了以下转换为UTF-8,即使消息已经是UTF-8:

$current_encoding = mb_detect_encoding( $body, 'auto' ); // Returns "ASCII"
$body = mb_convert_encoding( $body, $current_encoding, 'UTF-8' );

我也试过:

$body = mb_convert_encoding( $body, 'UTF-8', 'UTF-8' );

获取邮件正文后

$body = imap_body( $connection, $msgno );

使用下面的代码解码消息体

$body = utf8_decode(imap_utf8($body));

对$subject也使用相同的逻辑…

详细信息请参考imap_utf8

解决方案是这样的:我找到了生成电子邮件的人,他们做了以下标题更改:

Content-type: text/html; charset=UTF-8
Content-transfer-encoding: quoted-printable

这就解决了。我的PHP程序现在可以看到UTF-8字符。