Created
September 16, 2016 10:27
-
-
Save heri16/ef87ae7c313f3396d13acd28642d7af4 to your computer and use it in GitHub Desktop.
Delete expired email from IMAP server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$server_spec = "{****-****.accessdomain.com:993/imap/ssl}"; | |
$mbox = imap_open($server_spec, $argv[1], $argv[2], OP_HALFOPEN) | |
or die("can't connect: " . imap_last_error()); | |
echo "=================== Connected ===================\n"; | |
echo "Getting list of mailboxes...\n"; | |
$list = imap_getmailboxes($mbox, $server_spec, "*"); | |
if (is_array($list)) { | |
foreach ($list as $val) { | |
//echo imap_utf7_decode($val->name) . "\n"; | |
imap_reopen($mbox, $val->name, CL_EXPUNGE) | |
or die("can't reopen: " . imap_last_error()); | |
echo "Getting next mailbox info...\n"; | |
$MC = imap_check($mbox); | |
echo "=================== Mailbox ===================\n"; | |
echo "Name: " . $MC->Mailbox . "\n"; | |
$deldate = DateTime::createFromFormat(DateTime::RFC2822 . '+', $MC->Date) | |
or die("can't get current system date: " . DateTime::getLastErrors()); | |
$deldate->modify('-1 month'); | |
echo "System Date: ". $MC->Date . "\n"; | |
echo "Total messages: ". $MC->Nmsgs . "\n"; | |
if ($MC->Nmsgs == 0) { continue; } | |
echo "Fetching message list...\n"; | |
$result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}"); | |
if (is_array($result)) { | |
echo("Filtering message list...\n"); | |
foreach ($result as $n => $overview) { | |
if ($overview->deleted) { continue; } | |
echo '(' . $n . ') ' . $overview->date . ' => ' . $overview->subject; | |
$msgdate = DateTime::createFromFormat(DateTime::RFC2822 . '+', $overview->date); | |
if ($msgdate !== false && $msgdate < $deldate) { | |
imap_delete($mbox,$overview->msgno); | |
echo " [Mark Del]"; | |
} | |
echo "\n"; | |
} | |
} else { | |
echo "imap_fetch_overview failed: " . imap_last_error() . "\n"; | |
} | |
echo "Expunging all messages marked for deletion... "; | |
echo (imap_expunge($mbox) ? "OK" : "FAILED") . "\n"; | |
} | |
} else { | |
echo "imap_getmailboxes failed: " . imap_last_error() . "\n"; | |
} | |
imap_close($mbox); | |
echo "=================== Completed ===================\n"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment