We have a PHP script that opens a mailbox with imap_open
, checks it for new messages, posts them onto our intranet, and then imap_delete
s the message and imap_expunge
s the mailbox.
Generally this works okay, but sometimes the script fails to evict the message from the mailbox. This usually seems to be when the mail has a .xls
attachment, though there exists the possibility that that's a red herring.
I've tried reworking things several times to get this to work properly, but without any success. Most of the time imap_delete
does exactly what we want it to. Is there any obvious reason why imap_delete
/ expunge
would fail to delete from a mailbox?
Or can anyone suggest a better way of deleting a mail that is being "difficult"?
ETA:
$check = imap_mailboxmsginfo($mailbox);
echo "Messages before delete: " . $check->Nmsgs . "<br />\n";
imap_delete($mailbox, $i);
$check = imap_mailboxmsginfo($mailbox);
echo "Messages after delete: " . $check->Nmsgs . "<br />\n";
imap_expunge($mailbox);
$check = imap_mailboxmsginfo($mailbox);
echo "Messages after expunge: " . $check->Nmsgs . "<br />\n";
Results in:
Messages before delete: 1
Messages after delete: 1
Messages after expunge: 1
Both imap_delete and imap_expunge are giving a return value of true. imap_errors() is empty at all points during and after the process. Nothing looks out of the ordinary in the access log.
imap_fetch_overview after the imap_delete call does indicate that the delete flag has been correctly set. The imap_fetch_overview call after imap_expunge() returns just the same thing, delete flag still set. So I guess the expunge seems to be not expunging, in the case of certain mails... The results of imap_fetch_overview both times:
array(1) { [0]=> object(stdClass)#16 (14) { ["subject"]=> string(23) "(Subject)" ["from"]=> string(40) "(Sender)" ["to"]=> string(24) "undisclosed-recipients: " ["date"]=> string(31) "Sun, 30 Dec 2012 23:36:34 +0000" ["message_id"]=> string(50) "" ["size"]=> int(89752) ["uid"]=> int(1) ["msgno"]=> int(1) ["recent"]=> int(1) ["flagged"]=> int(0) ["answered"]=> int(0) ["deleted"]=> int(1) ["seen"]=> int(1) ["draft"]=> int(0) } }