Zend_Mail:moveMessage() 异常

3
根据文档,当移动/删除电子邮件时,您应该使用getNumberByUniqueId()方法来检索消息ID。然而,在处理文件夹时,我似乎仍然会遇到异常:“在响应中未找到单个ID”。所使用的代码大致如下:
try {
  $emailAddresses = array ();
  $invalidAddresses = array ();
  $errors = array();

  $emailValidator = new Zend_Validate_EmailAddress ();
  $trimFilter = new Zend_Filter_StringTrim ();

  $mail = new Zend_Mail_Storage_Imap (...);  
  $mail->selectFolder ( '/Unsubscribe' );

  echo 'There are ' . $mail->countMessages () . " messages to process\n";

  foreach ( $mail as $messageId => $message ) {
    try {
      $mail->noop ();
      $recipientMatch = $trimFilter->filter ( str_ireplace ( 'REMOVE ', '', $message->subject ) );

      if (! substr_count ( $recipientMatch, '@' )) {
        // Try the sender if the subject line doesn't have an address in
        $matches = array ();
        $from = $message->getHeader ( 'from' );
        preg_match ( '/<(.+@.+)>/', $from, $matches );

        if (sizeof ( $matches ) == 2) {
          $recipientMatch = $matches [1];
        } else {
          $recipientMatch = $from;
        }
      }

      if (! $emailValidator->isValid ( $recipientMatch )) {
        $invalidAddresses [] = $recipientMatch;
        continue;
      }

      $emailAddresses [] = $recipientMatch;
      $messageUniqueId = $mail->getUniqueId ( $messageId );
      $mail->moveMessage ( $mail->getNumberByUniqueId ( $messageUniqueId ), '/Unsubscribe/done' );
    } catch ( Exception $e ) {
      $errors[] = array($recipientMatch , $e);
    }
  }
} catch ( Zend_Mail_Storage_Exception $e ) {
  echo "There was a problem processing the mail account\n", $e->getMessage ();
} catch ( Exception $e ) {
  echo "There was an unmatched exception\n", $e->getMessage ();
}

任何想法为什么会抛出异常(Zend Framework v1.10.8)?

异常的堆栈跟踪如下:
#0 /usr/share/php/libzend-framework-php/Zend/Mail/Storage/Imap.php(163): Zend_Mail_Protocol_Imap->fetch(Array, 4)
#1 /usr/share/php/libzend-framework-php/Zend/Mail/Storage/Abstract.php(307): Zend_Mail_Storage_Imap->getMessage(4)
#2 /tmp/unsubscribe.php(29): Zend_Mail_Storage_Abstract->current()
#3 /tmp/dummy.php(1): include('/tmp/unsubscribe.php')
#4 {main}
< p > Zend_Mail_Protocol_Imap->fetch() 使用的数组内容是: < /p >
Array(
   0 => 'FLAGS', 
   1 => 'RFC822.HEADER'
)

你能否提供由异常生成的堆栈跟踪信息? - Mark
@Mark:我已经为这个异常添加了一个跟踪。 - cEz
我在想这是否与Exchange有关,因为我使用Python时遇到了类似的问题。 - cEz
2个回答

3
你不应该使用这种循环。使用这个循环后,一旦您删除了一个单独的消息,$messageId就过时了。在下一个循环中,$messageId将“指向”错误的消息或者甚至没有消息(如Index-Out-Of-Bounce)。这就是为什么您收到错误消息的原因。
解决方法:首先将所有相关的唯一ID收集到一个数组中,然后遍历此数组并调用moveMessage - 使用从getNumberByUniqueId获取的最新ID!
我希望我没有错,但我认为moveMessage($id, $folder) 应该是 moveMessage($index, $folder)。这更准确。如果我错了请纠正我。

1

正如之前提到的那样...如果你遇到了错误:"未在响应中找到单个ID",这意味着在清除过程中消息序列发生了变化,因此当你保存所有消息的所有ID并尝试迭代时,会出现此错误,因为一些消息已被删除,请检查:http://framework.zend.com/issues/browse/ZF-5655

你可以使用 "while" 在每次迭代中获取实际ID集合,像这样:

while($next_id = $gmail->getUniqueId()) {
  // move message to label folder "My_archive_folder"
  $gmail->moveMessage($next_id, 'My_archive_folder');
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接