为什么此邮件消息无法正确解码?

Óla*_*age 3 php email mime zend-framework zend-mail

我有这个代码.它来自Zend Reading Mail示例.

$message = $mail->getMessage(1);

// output first text/plain part
$foundPart = null;
foreach (new RecursiveIteratorIterator($mail->getMessage(1)) as $part) {
    try {
        if (strtok($part->contentType, ';') == 'text/plain') {
            $foundPart = $part;
            break;
        }
    } catch (Zend_Mail_Exception $e) {
        // ignore
    }
}
if (!$foundPart) {
    echo 'no plain text part found';
} else {
    echo $foundPart->getContent();
}
Run Code Online (Sandbox Code Playgroud)

我能得到的是消息,它运作正常.但是尝试将消息解码为可读的东西是行不通的.我已经尝试过Zend_Mime,imap_mime和iconv而没有运气.

这是我得到的一个例子 $foundPart->getContent();

Hall = F3 heim = FAr

它应该说"Hallóheimúr"

我想要的只是一些图书馆,我可以在实践中"按下按钮,接收培根".我的意思是,我只想将库指向POP3电子邮箱,并以可读的形式(没有任何编码问题)和附件获取电子邮件.

imap_mime_header_decode()给我一个包含相同数据的数组.
iconv_ mime_ decode()是一样的

有没有人知道为什么会发生这种情况或某些我可以抽象出来的库(PHP/Python或Perl)

And*_*rew 13

在学习如何使用Zend_Mail阅读电子邮件时,我遇到了一些类似的问题.您将需要添加Zend_Mail未实现的其他逻辑,例如解码编码的电子邮件和转换字符集.这是我在找到纯文本部分后正在做的事情:

$content = $foundPart->getContent();

switch ($foundPart->contentTransferEncoding) {
    case 'base64':
        $content = base64_decode($content);
        break;
    case 'quoted-printable':
        $content = quoted_printable_decode($content);
        break;
}

//find the charset
preg_match('/charset="(.+)"$/', $foundPart->contentType, $matches);
$charset = $matches[1];

if ($charset == 'iso-8859-1') {
    $content = utf8_encode($content); //convert to utf8
}
Run Code Online (Sandbox Code Playgroud)

  • 你是男人!这应该是公认的答案,因为它涵盖了base64和quoted-printable编码(在我的例子中,它是后者). (2认同)