PHP/IMAP : 内容邮件 MIME 转换

PHP/IMAP : Content mail MIME convert

我看过很多关于这个主题的帖子,但是 none 已经解决了我的问题。请考虑这是我第一次使用 IMAP,我想我错过了一些东西。我想将邮箱中收到的邮件转换为 "kind of command"。一切正常,除了邮件内容,用 MIME 编码(我认为是),我不知道如何在 UTF-8 上获取它。请注意我是法国人,所以我使用像 éèâçôì 等字符。请参阅下面的代码:

if($imap = @imap_open($server , $username, $password)) {
    $num = imap_num_msg($imap);

    while($num > 0) {
        $sujet      = imap_mime_header_decode(cropSubject($headers->subject));
        $time       = $headers->udate;
        $fromMail   = $headers->from[0]->mailbox.'@'.$headers->from[0]->host;
        $fromUser   = $headers->from[0]->mailbox;
        $fromHost   = $headers->from[0]->host;
        $content    = nl2br(imap_fetchbody($imap, $num, '1'));

        echo $content;
        echo "<hr>";
    }
}

其实我只是想显示所有收到的邮件。例如,我得到这样的值:

“你好,

Ceci est un test de cr=C3=A9ation directionement par mail.

Cordialement.

而不是:

“你好,

Ceci est un test de création directionement par mail.

Cordialement.

我看到很多线程建议同时使用 imap_fetchstructure() 和 imap_fetchbody(),但我认为这不会解决我的问题。使用 imap_utf8() 不起作用。

我也尝试制作自己的函数,将这些东西转换为正确的字符..但我认为这不是执行此操作的好方法。

有什么想法吗?

EDIT : quoted_printable_decode() 工作正常,只是它用我无法处理的“=\r\n”切断了长行。我在 PHP 文档中看到了一个 hack,但这似乎不起作用。下面的代码。

$content = str_replace("=\r\n", '', quoted_printable_decode($content));

echo $content;

知道我应该如何让它工作吗?

我使用@Max 解决方案 + 我自己的代码来完成这项工作。见下文。

    $content = quoted_printable_decode($content);
    $c = explode("\r\n", $content);

    foreach($c as $key => $value) {
        if(substr($value, strlen($value) - 6, strlen($value)) == "<br />" && $value != "<br />" && substr($value, strlen($value) - 7, strlen($value)) != "=<br />") {
            $c[$key] = substr($value, 0, strlen($value) - 6);
        } elseif(strlen($value) >= 7 && substr($value, strlen($value) - 7, strlen($value)) == "=<br />") {
            $c[$key] = substr($value, 0, strlen($value) - 7);
        } elseif($value == "<br />") {
            $c[$key] = "\r\n\r\n";
        }
    }

    $content = implode("", $c);

请注意,这用于将值插入数据库,而不是在页面上显示它们。仅仅为了炫耀电子邮件是行不通的。