Webklex / laravel-imap

Laravel IMAP is an easy way to integrate both the native php-imap module and an extended custom imap protocol into your Laravel app.
https://www.php-imap.com
MIT License
641 stars 182 forks source link

Could not clean my mailbox #439

Closed Yooootsuba closed 2 years ago

Yooootsuba commented 2 years ago

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();
        }
    }
Webklex commented 2 years ago

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,