电子邮件解码在zend邮件中不起作用

时间:2012-01-20 08:36:22

标签: php zend-framework zend-mail

我有一个访问指定电子邮件并提取邮件的脚本。 $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网站上的文档不是很好。需要一些帮助!!

2 个答案:

答案 0 :(得分:3)

对我有用:

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');
    }

}

答案 1 :(得分: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;
            } 
        }

    }
}