Php 如果循环中断,如何从imap中删除消息
我正在使用php从exchange邮箱读取邮件。下面只是一个简单的脚本,不是一个真实的例子 在某些时候,“for”循环有时会中断,我正在修复这些问题 如果有10条消息,且最后一条消息上的循环中断,则应删除的其他9条消息将不会删除,因为删除的代码中断未到达 是否有一个解决办法,即使代码中断,我仍然可以删除已被删除处理的正确电子邮件Php 如果循环中断,如何从imap中删除消息,php,exchange-server,imap,Php,Exchange Server,Imap,我正在使用php从exchange邮箱读取邮件。下面只是一个简单的脚本,不是一个真实的例子 在某些时候,“for”循环有时会中断,我正在修复这些问题 如果有10条消息,且最后一条消息上的循环中断,则应删除的其他9条消息将不会删除,因为删除的代码中断未到达 是否有一个解决办法,即使代码中断,我仍然可以删除已被删除处理的正确电子邮件 //checking how many messages are in the mailbox $total = imap_num_msg($inbox
//checking how many messages are in the mailbox
$total = imap_num_msg($inbox);
//if there are any messages then process them
if ( $total > 0){
echo "\nAnalysing Mailbox\n";
for ($x=$total; $x>0; $x--){
//doing some work here
delete_processed_message($x);
}
echo "Please wait, expunging old messages\n";
imap_expunge($inbox);
echo "Please wait, disconnecting from mail box\n";
imap_close($inbox);
非常感谢。另一种选择是将您的
delete\u processed\u消息($x)
包装在一个文件中(可以找到官方文档)。这样,即使它抛出一个异常(这可能是它中断的原因),它也会继续处理其余的消息
代码应该是这样的
...
for ($x=$total; $x>0; $x--){
//doing some work here
try {
delete_processed_message($x);
}
catch (Exception $e) {
//Here you can log it to a file, or simply echo it
echo 'Caught exception: ', $e->getMessage(), "\n";
}
}
...
正如中所解释的,在for循环中使用try-catch可以确保完成