laravel-imap icon indicating copy to clipboard operation
laravel-imap copied to clipboard

Could not clean my mailbox

Open Yooootsuba opened this issue 3 years ago • 1 comments

I just implemented a function that trying to clean mailbox.

And it always delete a few mails, not all.

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $client = new ClientManager();

        $client = $client->make([
    		'host'          => 'imap.gmail.com',
    		'port'          => 993,
    		'encryption'    => 'ssl',
    		'validate_cert' => true,
    		'username'      => env('MAIL_USERNAME'),
    		'password'      => env('MAIL_PASSWORD'),
    		'protocol'      => 'imap'
        ]);

        $client->connect();

        $folder = $client->getFolderByName('INBOX');

        foreach ($folder->messages()->all()->get() as $message) {
            $message->delete();
        }
    }

Yooootsuba avatar Jun 02 '22 09:06 Yooootsuba

Hi @Yooootsuba , you have to call $client->expunge() at the end. Otherwise those changes aren't committed on the server and therefor the messages won't get deleted. Alternative, you could call $message->delete(true) instead.

Best regards & happy coding,

Webklex avatar Aug 09 '22 06:08 Webklex