使用Zend从IMAP检索特定日期的邮件

时间:2012-12-21 08:20:50

标签: zend-framework object imap

我正在使用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,我是否会以正确的顺序收到邮件?

1 个答案:

答案 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();