2012-12-21 62 views
2

我使用Zend_Mail_Storage_Imap庫從IMAP檢索電子郵件。使用Zend從IMAP檢索郵件到特定日期

$mail = new Zend_Mail_Storage_Imap(array('connection details')); 
foreach($mail as $message) 
{ 
    if($message->date > $myDesiredDate) 
    { 
    //do stuff 
    }else{ 
    continue; 
    } 

此代碼檢索所有郵件中檢索到的第一個最老的郵件。變量$ myDesiredDate是日期/時間,郵件超出了這些不需要。有沒有辦法跳過所有郵件的檢索並逐一檢查每個郵件的日期?如果不是,我可以將$ mail對象取消以獲取最上面的最新電子郵件地址嗎?

更新:我現在修改了一下代碼,從最新的郵件開始並檢查當前郵件的日期時間。當我遇到一封電子郵件時,我不想解析電子郵件,我打破了這個循環。

//time upto which I want to fetch emails (in seconds from current time) 
    $time = 3600; 
    $mail = new Zend_Mail_Storage_Imap(array('connection details')); 
    //get total number of messages 
    $total = $mail->countMessages() 

    //loop through the mails, starting from the latest mail 
    while($total>0) 
    { 
     $mailTime = strtotime(substr($mail->getMessage($total)->date,0,strlen($mail->getMessage($total)->date)-6)); 

     //check if the email was received before the time limit 
     if($mailTime < (time()-$time)) 
     break; 
     else 
     //do my thing 

     $total--; 
    } 

    //close mail connection 

$mail->close(); 

我唯一關心的是,如果我從郵件數到0開始,我是否會按正確的順序收到郵件?

回答

1

因爲我的代碼工作得很好,所以我將其作爲答案(快速和骯髒)。我現在從最新的郵件開始,並檢查當前郵件的日期時間。當我遇到一封電子郵件時,我不想解析電子郵件,我打破了這個循環。

//time upto which I want to fetch emails (in seconds from current time) 
    $time = 3600; 
    $mail = new Zend_Mail_Storage_Imap(array('connection details')); 
    //get total number of messages 
    $total = $mail->countMessages() 

    //loop through the mails, starting from the latest mail 
    while($total>0) 
    { 
     $mailTime = strtotime(substr($mail->getMessage($total)->date,0,strlen($mail->getMessage($total)->date)-6)); 

     //check if the email was received before the time limit 
     if($mailTime < (time()-$time)) 
     break; 
     else 
     //do my thing 

     $total--; 
    } 

    //close mail connection 

$mail->close(); 
相關問題