2012-01-20 58 views
0

我有一個訪問指定電子郵件並獲取郵件的腳本。 $temp->getContent()回聲以下..電子郵件解碼在zend郵件中不起作用

----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網站的文檔不是很好。需要一些幫助!!

+0

你能提供您嘗試未能成功解析消息的完整和未經編輯的原始版本?這聽起來像它不是一個格式正確的MIME消息,但如果情況是這樣的話,你應該得到一個異常。 – Charles

+0

這是否傾向於幫助:http://stackoverflow.com/questions/834152/why-doesnt-this-mail-message-decode-correctly –

+0

@Charles其有點機密的信息,所以我不能分享編碼部分..當它被解碼時,我得到一個html輸出,我完全看到了我的Gmail。 –

回答

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

    } 
} 
+0

單獨獲取郵件零件我用getPath直接做這個工作..但在某些情況下它不是base64其引用的打印編碼..我無法找到「內容傳輸編碼」類型使用base64解碼或引用打印解碼..請建議.... –

+0

我不瞭解你。您的頭文件中包含Content-Transfer-Encoding:base64。我爲你編輯了標題。 –

+0

我沒有看到編輯..但getHeaders函數沒有返回Content-Transfer-Encoding ..我想我必須首先getPart和getHeaders ..我檢查並讓你知道.. –

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

}