Zend_Mail_Storage_Imap ライブラリを使用して、IMAP から電子メールを取得しています。
$mail = new Zend_Mail_Storage_Imap(array('connection details'));
foreach($mail as $message)
{
if($message->date > $myDesiredDate)
{
//do stuff
}else{
continue;
}
このコードは、最も古いメールを最初に取得して、すべてのメールを取得します。変数 $myDesiredDate は日付/時刻であり、それ以降のメールは必要ありません。すべてのメールの取得をスキップして、各メールの日付を 1 つずつ確認する方法はありますか? そうでない場合、$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 から開始した場合、メールが正しい順序で届くかどうかだけです。