Search code examples
phpimap

IMAP Search - Date Range


I am currently working on a bespoke system where i need to loop through an IMAP email account where the emails im looking for need lay within a date range.

As it stands, i have a working IMAP search function which is getting all emails with a certain string in the subject, the part which i cannot seem to find any documentation for, is the BETWEEN date range. I have seen that you can use the BEFORE ON & SINCE criteria but this doesn't seem to allow me to create a range as such.

$inbox = imap_open($hostname,$username,$password) or die('Cannot connect to mailbox: ' . imap_last_error());
$emails = imap_search($inbox,'SUBJECT "Undelivered Mail Returned to Sender" ON "'.$date.'"');

Im not asking for a complete spoonfeed but a point in the right direction would be appreciated.

Thanks a lot, Lewis


Solution

  • For those interested: After staring at the documentation for imap_search for about 10 minutes, i decided to try using both SINCE and BEFORE together. I managed to get the following code to do exactly what was required.

    $emails = imap_search($inbox,'SUBJECT "Undelivered Mail Returned to Sender" SINCE "'.$dateMonth.'" BEFORE "'.$date.'"');