Clear emails using PHP

I have a PHP script that cleans up a mailbox every day. The problem is that all deleted emails are moved to the trash and garbage can never be emptied.

I used the PHP IMAP commands to process the mailbox.

$this->_mbox = imap_open(
    $this->_data['server'],
    $this->_data['user'],
    $this->_data['pass'],
    OP_SILENT
)

...
imap_delete($this->_mbox, $index);

...
imap_expunge($this->_mbox);

How to empty the basket?

EDIT:

imap_delete () completely deletes letters (also from the trash). Now I'm just trying to find a way to read the basket folder. I tried to open the directory in the same way that I open the inbox without success.

+3
source share
1 answer

Maybe the problem is $index?

If you want to erase all Trash mailboxes, try this instead:

<?php
$conn = @imap_open("\{$server/$serverType}Trash", $user, $pass) 
   or die("Connection to folder failed");

// delete email(s)
@imap_delete($conn,'1:*');   // to clear out an entire mailbox.
@imap_expunge($conn);
echo "Trash is empty.";
?>

Hope this helps! Credits go to jacky

+1
source

All Articles