根据文档,您应use the getNumberByUniqueId() method在移动/删除电子邮件时检索邮件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}
Zend_Mail_Protocol_Imap->fetch()
中使用的数组的内容是:
Array(
0 => 'FLAGS',
1 => 'RFC822.HEADER'
)
答案 0 :(得分:3)
你不要使用这种循环。使用此循环时,只要删除单个邮件,$ messageId就会过时。在下一个周期中,$ messageId将“指向”错误的消息甚至没有消息(如Index-Out-Of-Bounce)。这就是你收到错误信息的原因。
解决方案:首先将所有相关的唯一ID收集到一个数组中,然后遍历此数组并调用moveMessage - 使用从getNumberByUniqueId获得的最新ID!
我希望我没错,但我认为moveMessage($ id,$ folder)应该是moveMessage($ index,$ folder)。这更准确。但如果这是错误的,请纠正我。
答案 1 :(得分:1)
如果您遇到错误:“在响应中找不到单个ID”意味着在删除过程中更改了消息序列,因此当您保存所有消息的所有ID时尝试迭代你有这个错误,因为之前删除了一些消息,请检查:http://framework.zend.com/issues/browse/ZF-5655
您可以使用“while”在每次迭代中获取ID的ACTUAL集合,如下所示:
while($next_id = $gmail->getUniqueId()) {
// move message to label folder "My_archive_folder"
$gmail->moveMessage($next_id, 'My_archive_folder');
}