2.8.1.7. Delete old emails from your mailbox
Attention!
With a large mailbox volume, the script may not have time to delete messages in one pass and may need to be run again.The script allows you to delete messages for a certain time interval, which can be specified relative to the current day or fixed dates.
<?php $Before = 'BEFORE "' . $Before . '"'; $After = 'SINCE "' . $After . '"'; $mbox = imap_open("{" . $Host . "/novalidate-cert}INBOX", $User, $Password) or die("Can't connect: " . imap_last_error()); try { throw new Exception("Error is occurred"); } } foreach ($emails as $email_id) { '/([^: ]+): (.+?(?:\r\n\s(?:.+?))*)\r\n/m', $matches ); } } catch (Exception $e) { echo $e->getMessage(), "\n"; }
The data required for the script to work can be specified both in the script itself and in the command line at startup:
Host
— address and port IMAPserver.User
— title mailbox.Password
— password mailbox.Before
— letters earlier than the specified date will be deleted. The date can be specified by replacing-1 year
for a specific date, in the format YYYY-MM-DD (2010-02-23) or subtracting a certain amount of time from the current date in the format-1 year
or-1 month
or-1 day
.After
— letters after the specified date will be deleted. The date can be specified by replacing1970-01-01
for a specific date, in the format YYYY-MM-DD (2010-02-22) or subtracting a certain amount of time from the current date in the format-1 year
or-1 month
or-1 day
.
Running the script in console or task cron executed by a command of the form:
/usr/local/bin/php /home/example/file.php mail.adm.tools:143 admin@example.com "password" "2022-03-24" "2015-04-28"