I'm currently testing a script that will go through an e-mail account deleting 20 of the oldest e-mails per page load. (Number is just for testing purposes) However. I've noticed a couple of issues with what I'm doing:
1 - It seems to sometimes ignore e-mails. For example. I had 12 e-mails that could have been deleted. Yet it only deleted 6, When I ran the script again, it deleted 3, then down to 2, and finally I had to refresh again to delete the final one. Does anyone know why this may be? When the limit is 20. So would loop through 20 times anyway.
2 - The main issue is that although it removes from the Inbox. When I go to All Mail. Every single e-mail is still in there.
The code is as follows incase anyone can spot why I experience these difficulties. (although removed the imap_open call.
$numMessages = imap_num_msg($imap);
//for ($i = $numMessages; $i > ($numMessages - 20); $i--) {
for($i=1; $i<=20; $i++)
{
$header = imap_header($imap, $i);
$fromInfo = $header->from[0];
$replyInfo = $header->reply_to[0];
$details = array(
"fromAddr" => (isset($fromInfo->mailbox) && isset($fromInfo->host))
? $fromInfo->mailbox . "@" . $fromInfo->host : "",
"fromName" => (isset($fromInfo->personal))
? $fromInfo->personal : "",
"replyAddr" => (isset($replyInfo->mailbox) && isset($replyInfo->host))
? $replyInfo->mailbox . "@" . $replyInfo->host : "",
"replyName" => (isset($replyTo->personal))
? $replyto->personal : "",
"subject" => (isset($header->subject))
? $header->subject : "",
"udate" => (isset($header->udate))
? $header->udate : ""
);
$uid = imap_uid($imap, $i);
echo "<ul>";
echo "<li><strong>From:</strong>" . $details["fromName"];
echo " " . $details["fromAddr"] . "</li>";
echo "<li><strong>Subject:</strong> " . $details["subject"] . "</li>";
echo '<li><a href="mail.php?folder=' . $folder . '&uid=' . $uid . '&func=read">Read</a>';
echo " | ";
echo '<a href="mail.php?folder=' . $folder . '&uid=' . $uid . '&func=delete">Delete</a>';
echo " | ";
echo date("F j, Y, g:i a",$details['udate']) . '</li>';
echo "</ul>";
imap_delete($imap, $uid, FT_UID);
}
imap_expunge($imap);
imap_close($imap);
?>
Thank you
Since Google mails can be in multiple "folders" (labels) imap_delete just removes the corresponding label.
First you have to move the mails to the Trash folder. From there you can imap_delete them.
Source: Deleting IMAP messages