2

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 から開始した場合、メールが正しい順序で届くかどうかだけです。

4

1 に答える 1

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();
于 2012-12-27T12:33:15.190 に答える