2013-04-06 13 views
1

在Windows 8上運行Apache 2.2與PHP 5.3試圖獲得PHP類ImapMailbox下載附件,但每次獲取郵件()時,每個附件中的附件值都爲空。PHP class ImapMailbox不下載附件

所有其他的電子郵件信息已正確下載。

我已經瀏覽了課程代碼,但無法確定問題出在哪裏。

這裏是我當前的代碼:

$mailbox = new ImapMailbox('{testsite.com:110/pop3/novalidate-cert}INBOX', '[email protected]', 'MyPaSs', ATTACH_DIR, 'utf-8'); 

$mails = array(); 

foreach($mailbox->searchMailbox('SUBJECT "test attach" SINCE "' . date('m/d/Y', strtotime('-1 week')) . '"') as $mailId) { 
     $mail = $mailbox->getMail($mailId); 
     $mails[] = $mail; 
} 

在getMail()傾銷$ VAR的數據後,它似乎有在winmail.dat的格式的附件。代碼無法到達這些,因爲由於空的'ifid'值而沒有分配attachmentId值。解碼winmail.dat附件可以完成,但前提是它們被檢測到並寫入文件。

任何想法如何在ImapMailbox代碼中爲此創建解決方法?

+0

我相信你實際上必須通過字節讀取附件中的文件並將它們寫入一個新文件.. – 2013-04-06 23:55:21

回答

1

這是我寫的修復這個問題。

在initMailPart()方法開始時,添加以下內容:

static $altAttachmentId = 0; 

在IF塊的結尾if($this->attachmentsDir) {添加以下其中閉合}托架是:

} elseif (!empty($params['fileName']) || !empty($params['filename']) || !empty($params['name'])) { // Process attachments that are not inline. 
          // Check if need to decode TNEF (Winmail.dat) file. 
          if ($partStructure->ifsubtype && $partStructure->subtype == 'MS-TNEF') { 
           require_once 'path_to_your/tnef_decoder.php'; 

           $Tnef = new tnef_decoder; 

           $un_tnef = $Tnef->decompress($data); 


           $attached_files = array(); 

           foreach ($un_tnef as $f) { 
            if (!empty($f['name']) && !empty($f['stream'])) { 
             $attachment = new IncomingMailAttachment(); 

             $attachment->id = $altAttachmentId; 
             $attachment->name = $f['name']; 
             $attachment->filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . preg_replace('~[\\\\/]~', '', $f['name']); 

             $mail->addAttachment($attachment); 

             if (file_exists($attachment->filePath) && md5($f['stream']) != md5_file($attachment->filePath)) { 
              $attachment->filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . preg_replace('~[\\\\/]~', '', $mail->id . '_' . $altAttachmentId . '_' . $f['name']); 
             } 

             file_put_contents($attachment->filePath, $f['stream']); 

             $altAttachmentId++; 
            } 
           } 
          } else { 
           if (!empty($params['filename'])) { 
            $fileName = $params['filename']; // Account for random camel-case mistake on element. 
           } elseif (!empty($params['fileName'])) { 
            $fileName = $params['fileName']; 
           } else { 
            $fileName = $params['name']; 
           } 

           $attachment = new IncomingMailAttachment(); 
           $attachment->id = $altAttachmentId; 
           $attachment->name = $fileName; 
           $attachment->filePath = $this->attachmentsDir . DIRECTORY_SEPARATOR . preg_replace('~[\\\\/]~', '', $mail->id . '_' . $altAttachmentId . '_' . $fileName); 

           $mail->addAttachment($attachment); 

           file_put_contents($attachment->filePath, $data); 

           $altAttachmentId++; 
          } 
         } 

請注意,您必須包含Roundcube Webmail包中的tnef_decoder.php文件才能使TNEF解碼工作。我爲TNEF解決方案here獲得靈感。

此修改將處理Winmail.dat文件中的所有TNEF編碼文件以及未內聯的任何其他附件。在大文件上觀察內存使用情況。

如果它們不完全相同,它也不會覆蓋同名的現有文件。