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 是日期/时间,超过该时间的邮件就不需要了。有没有办法跳过所有邮件的检索并逐个检查每封邮件的日期?如果没有,我可以反转 $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 回答