I am trying to access a IMAP webmail account in order to delete old messages using php. The script I have so far is:
<?php
$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);
?>
The code is now correct. But the web hosting service does not allow scripted access to their IMAP server from localhost
You need a semicolon on this line:
imap_expunge($mbox);
when PHP can't be parsed it returns a 500 error.