Search code examples
phpimap

imap_delete() not working?


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', '[email protected]', '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.


Solution

  • 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.