Manage cookies that are used for advertising, such as ad personalization, remarketing, and ad effectiveness analysis.
2.8.1.7. Delete old messages from mailbox
Attention!
- This script is a powerful tool for managing mailboxes and should be used with caution.
- If mailbox is large, the script may not be able to delete all messages in one pass and may need to be run again.
The script requires PHP version 8.3 or lower to run.
Script for deleting messages within a specified time interval:
<?php
if (php_sapi_name() !== 'cli') {
die("Access Denied: This script can only be run from the command line.\n");
}
$options = getopt('', [
'hostname::',
'port::',
'ssl::',
'username:',
'password:',
'folders::',
'from-date::',
'to-date:',
]);
$hostname = isset($options['hostname']) ? $options['hostname'] : 'mail.adm.tools';
$port = isset($options['port']) ? $options['port'] : 143;
$username = $options['username'] ?? die("Error: 'username' parameter is required.\n");
$password = $options['password'] ?? die("Error: 'password' parameter is required.\n");
$ssl = isset($options['ssl']) ? $options['ssl'] : (($port == 993) ? 'y' : 'n');
$folders = isset($options['folders']) ? explode(',', $options['folders']) : ['INBOX'];
$fromDate = isset($options['from-date']) ? $options['from-date'] : '1-Jan-1970';
if (!isset($options['to-date'])) {
die("Error: 'to-date' parameter is required.\n");
}
$toDate = $options['to-date'];
$connectionString = '{' . $hostname . ':' . $port . (($ssl == 'y') ? '/imap/ssl' : '/imap') . '}';
echo "Connecting to server: $connectionString\n";
echo "Searching emails in folders: " . implode(', ', $folders) . "\n";
foreach ($folders as $folder) {
$fullFolderName = (stripos($folder, 'INBOX') === 0) ? $folder : 'INBOX.' . $folder;
$imapStream = imap_open($connectionString . $fullFolderName, $username, $password);
if (!$imapStream) {
echo "Error: Could not open folder $fullFolderName\n";
continue;
}
$searchCriteria = 'SINCE "' . $fromDate . '" BEFORE "' . $toDate . '"';
$emails = imap_search($imapStream, $searchCriteria);
if ($emails) {
echo "Found " . count($emails) . " emails in folder $fullFolderName\n";
foreach ($emails as $emailNumber) {
$deleteStatus = imap_delete($imapStream, $emailNumber);
if ($deleteStatus) {
echo "Successfully deleted email with ID $emailNumber in folder $fullFolderName\n";
} else {
echo "Error deleting email with ID $emailNumber in folder $fullFolderName\n";
}
}
imap_expunge($imapStream);
} else {
echo "No emails found in folder $fullFolderName matching criteria\n";
}
imap_close($imapStream);
}
echo "Done.\n";
Command line arguments when running the script:
hostname— IMAP server address (by defaultmail.adm.tools).port— server port (by default143).ssl— forced use of SSL (y— use,n— do not use).username(required parameter) — mailbox address.password(required parameter) — mailbox password.folders— folders for searching and deleting messages (by default, theINBOXfolder).from-date— from what date in theDay-Month-Yearformat should messages be deleted (by default1-Jan-1970).to-date(required parameter) — to which date in theDay-Month-Yearformat should messages be deleted.
Example of the command to run the script in the console or cron:
/usr/local/bin/php /home/example/file.php --hostname="mail.example.com" --port=993 --ssl=y --username="your-email@example.com" --password="your-email-password" --folders="INBOX,Sent" --from-date="1-Jan-2025" --to-date="1-Jan-2025"
(1)