如何从 PHP 中的 IMAP 检索 Unicode 字符?

How can I retrieve Unicode characters from IMAP in PHP?

我正在尝试使用 PHP 解析一封部分用英语写成、部分用日语写成的电子邮件。这是我目前的代码:

if ($mbox = imap_open($mailbox, $username, $password)) {

    $inbox = imap_check($mbox);
    $total = (int) $inbox->Nmsgs;
    $min = max(1, $total - 10);

    // fetch 10 most recent emails
    if ($total > 0 && ($emails = imap_fetch_overview($mbox, "{$min}:{$total}"))) {

        foreach ($emails as $email) {

            $body = imap_body($mbox, $email->uid, FT_UID);

            // for testing purposes
            echo $body;

            // parsing logic here

        }
    }
}

正如您所看到的,我只是回显了电子邮件的明文正文,只是为了测试一些东西,这是我在 运行 这个脚本时得到的回显响应的一部分:

Your Japanese word of the day is: ç”·ã®å­

但是,根据电子邮件中的实际内容,我应该看到以下内容:

Your Japanese word of the day is: 男の子

很明显,翻译中丢失了一些东西(双关语)。我正在使用一些简单的字符串操作来尝试解析日文字符,然后将它们插入到 MySQL 数据库中。但是,它目前正在插入那些官话字符。我错过了什么?

编辑:我正在使用 PHP 版本 5.4.45

所以我不太确定如何在 PHP 5 中让它工作。我决定尝试将我的服务器升级到 PHP 7,突然间所有代码都开始工作了。