I'm making a bounce handler, and I want to delete all of the bounced emails. I'm trying to do it with imap_delete()
, but the emails are not deleted. What could be the problem?
<?php
// Settings
require_once(dirname(__FILE__) . '/settings.php');
// Bounce
$bounce = array();
$inbox = imap_open('{imap.example.com:143/novalidate-cert}INBOX', 'mail-bounce@example.com', 'SECRET') or die('Cannot connect to mailbox: ' . imap_last_error());
$emails = imap_search($inbox, 'ALL');
if($emails) {
foreach($emails as $id) {
preg_match('/<(.*?)>: host/s', imap_fetchbody($inbox, $id, 1), $matches);
$bounce[] = $matches[1];
imap_delete($inbox, $id);
}
}
imap_close($inbox);
if(!empty($bounce)) {
$STH = $DBH->prepare("UPDATE newsletter SET status=-1 WHERE email='" . implode(" OR email='", $bounce) . "'");
$STH->execute();
}
?>
imap_last_error()
doesn't return any errors.
imap_delete()
marks the email for deletion. But it's not actually deleted until you call imap_expunge()
, or until you call imap_close()
with the optional CL_EXPUNGE
parameter.