【问题标题】:Email decoding doesn't work in zend mail电子邮件解码在 zend 邮件中不起作用
【发布时间】:2012-02-14 20:33:35
【问题描述】:

我有一个脚本可以访问指定的电子邮件并获取邮件。 $temp->getContent() 回显以下内容..

----boundary_2710_edfb8b44-71c8-49ff-a8cb-88c83382c4ee 
Content-Type: multipart/alternative; 
boundary=--boundary_2709_dde0dd0e-ba35-4469-949d-5392aec65750 --boundary_2709_dde0dd0e-ba35-4469-949d-5392aec65750 
Content-Type: text/html; charset=utf-8 
Content-Transfer-Encoding: base64 
PGZvcm0gbWV0aG9k.........this part is base64 encoded and it works fine if i copy and decode it separately.......AgICAgICAgICAgDQoNCjwvZm9ybT4= 
----boundary_2709_dde0dd0e-ba35-4469-949d-5392aec65750-- ----boundary_2710_edfb8b44-71c8-49ff-a8cb-88c83382c4ee 
Content-Type: multipart/mixed; boundary=--boundary_2711_eca4cfc3-fc62-43d6-b9fb-e5295abbfbe8 ----boundary_2711_eca4cfc3-fc62-43d6-b9fb-e5295abbfbe8 Content-Type: application/pdf; 
name=redBusTicket.pdf 
Content-Transfer-Encoding: base64 
Content-Disposition: attachment Content-ID: JVBERi0xLjIgCiXi48/TIAoxIDAgb2JqIAo8PCAKL1R5cGUgL0NhdGFsb2cgCi9QYWdlcyAy IDAgUiAKL1BhZ2VNb2RlIC9Vc2VOb25lIAovVmlld2VyUHJlZ

在此内容之间有 base64 编码部分,如果我单独复制和解码它,它可以正常工作。邮件里还有附件。我怎样才能得到附件。以下是我的代码。当我直接使用 base64_decode 时,我没有输出.. 只是一个空白页..

$storage = new Zend_Mail_Storage_Imap($imap);
$temp = $storage->getMessage($_GET['mailid']);
echo base64_decode($temp->getContent());

zend 网站上的文档不是很好。需要帮助!!

【问题讨论】:

  • 您能否提供您尝试解析失败的消息的完整且未经编辑的原始版本?听起来这不是格式正确的 MIME 消息,但如果是这种情况,您应该得到一个异常。
  • @Charles 有点机密信息,所以我不能分享编码部分。当它被解码后,我得到一个 html 输出,我可以在我的 gmail 中看到它。
  • @Sudhir 那不是我要找的人..我还是发现了问题..我必须使用 getPart() 单独获取邮件部分

标签: php zend-framework zend-mail


【解决方案1】:

它对我很有用:

foreach ($mail as $message) {

    $content = null;      
    foreach (new RecursiveIteratorIterator($message) as $part) {
        if (strtok($part->contentType, ';') == 'text/plain') {
            $content = $part;
            break;
        }
    }

    if ($content) {
        echo "\n encode: " . $content->contentTransferEncoding;        
        echo "\n date: " . $message->date;
        echo "\n subject: \n" . iconv_mime_decode($message->subject, 0, 'UTF-8');
        echo "\n plain text part: \n" . mb_convert_encoding(base64_decode($content), 'UTF-8', 'KOI8-R');
    }

}

【讨论】:

    【解决方案2】:

    我有这样的东西可以从电子邮件中获取 base_64 内容。试着过滤掉你不需要的东西。

    if ($email->isMultipart() && $partsCount){
    
        for($i = 1; $i < $email->countParts() +1; $i++) {
            $part = $email->getPart($i);
            $headers = $part->getHeaders();
            if (
                array_key_exists('content-description', $headers)
                 || array_key_exists('content-disposition', $headers)
    
            ){
                if (array_key_exists('content-description', $headers)) {
                    $att = $part->getContent();
                    $filepath = utf8_encode(DATA_PATH . '/' . $part->getHeader('content-description'));
                    if (is_file($filepath)) {
                        unlink($filepath); // deletes previous files with same name
                    }
                    $file = fopen($filepath, "w");
                    fwrite($file, base64_decode($att));
                    fclose($file);
                    $attachments[] = $filepath;
                } 
            }
    
        }
    }
    

    【讨论】:

    • 我直接使用 getPath 来完成这项工作。但在某些情况下,它不是 base64 引用的可打印编码。我无法找到“内容传输编码”类型来使用 base64 解码或引用的可打印编码解码..请建议....
    • 我不懂你。您的标头中有 Content-Transfer-Encoding: base64 。我已经为你编辑了标题。
    • 我没有看到编辑。但是 getHeaders 函数没有返回 Content-Transfer-Encoding.. 我想我必须先 getPart 和 getHeaders.. 我会检查并让你知道。 .
    猜你喜欢
    • 1970-01-01
    • 2014-07-05
    • 2014-09-08
    • 2017-01-25
    • 1970-01-01
    • 2014-02-28
    • 2011-05-18
    • 2016-12-14
    • 2015-11-03
    相关资源
    最近更新 更多