2012-03-30 154 views
3

我正在嘗試構建一個小型網絡郵件應用程序。當我閱讀收件箱中的所有電子郵件時,如果它包含附件,我想爲每封郵件展示。這是有效的,但問題是需要很長時間才能做到這一點,1Mb電子郵件附件大約需要0.5秒。與收件箱中具有大附加文件的所有電子郵件相乘:| 我的問題是:如何檢查電子郵件是否附上沒有加載整個電子郵件?那可能嗎 ? 貝婁是我現在使用的代碼:php imap檢查電子郵件是否有附件

function existAttachment($part) 
{ 
    if (isset($part->parts)) 
    { 
    foreach ($part->parts as $partOfPart) 
    { 
    $this->existAttachment($partOfPart); 
    } 
    } 
    else 
    { 
    if (isset($part->disposition)) 
    { 
    if ($part->disposition == 'attachment') 
    { 
    echo '<p>' . $part->dparameters[0]->value . '</p>'; 
    // here you can create a link to the file whose name is $part->dparameters[0]->value to download it 
    return true; 
    } 
    } 
    } 
    return false; 
} 

function hasAttachments($msgno) 
{ 
    $struct = imap_fetchstructure($this->_connection,$msgno,FT_UID); 
    $existAttachments = $this->existAttachment($struct); 

    return $existAttachments; 
} 

回答

1

imap_fetchstructure並以分析它獲取整個電子郵件內容。可悲的是,沒有其他方式來檢查附件。

也許你可以使用來自imap_headerinfo的消息大小信息來獲得預測,如果消息將具有附件。

另一種方法是在後臺定期獲取電子郵件,並將它們的內容和UID存儲起來,以便以後在數據庫中查找。無論如何,你需要這樣做,當你想要搜索特定的消息。 (您不想在搜索「晚餐」時掃描imap帳戶)

0

要檢查電子郵件是否有附件,請使用$ structure-> parts [0] - >部分。

$inbox = imap_open($mailserver,$username, $password, null, 1, ['DISABLE_AUTHENTICATOR' => 'PLAIN']) or die(var_dump(imap_errors())); 

$unreadEmails = imap_search($inbox, 'UNSEEN'); 

$email_number = $unreadEmails[0]; 

$structure = imap_fetchstructure($inbox, $email_number); 

if(isset($structure->parts[0]->parts)) 
{ 
    // has attachment 
}else{ 
    // no attachment 
} 
+0

下載整個電子郵件,這是我想要避免的 – 2017-03-17 06:41:44