I need to remove older emails automatically from webmail. How can I do it? Anyone have an idea?
Asked
Active
Viewed 698 times
1 Answers
0
You can use IMAP client libraries from different languages:
here are some examples:
- email - PHP delete old messages from IMAP mail account - Stack Overflow
- python - imap deleting messages - Stack Overflow
I already used PHP to do this task.
<?php // Example from the first link
$del = new DateTime();
$del->modify('-1 month');
$mbox = imap_open("{imap.test.com:993/imap/ssl}INBOX", "username", "password")
or die("can't connect: " . imap_last_error());
$MC = imap_check($mbox);
// Fetch an overview for all messages in INBOX
$result = imap_fetch_overview($mbox,"1:{$MC->Nmsgs}",0);
foreach ($result as $overview) {
$date = $overview->date;
$date = DateTime::createFromFormat('D, d M Y H:i:s O', $date);
if($date<$del) {
imap_delete($mbox,$overview->msgno);
}
}
imap_expunge($mbox);
imap_close($mbox);
?>

Chocolord
- 493
- 3
- 12