Elorfin
Elorfin

Reputation: 23

imap_search SINCE criterion never returns results

I am trying to access Gmail accounts to import Sent and Received Mails in a PHP application. After each import, I store the current date in order to not import all mail each time.

// $this->dateLastSynchro is a DateTime object
$searchCriteria = 'NEW UNDELETED SINCE "' . $this->dateLastSynchro->format('r') . '" ';
$return = imap_search($this->mailBoxHandle, $searchCriteria);

The first import works fine (when I use "ALL UNDELETED"), but when I use SINCE, I never have results (even if I have sent or received new mails).

I have tried to use the format Y-m-d for date, but I have a PHP Notice when I do this (it says something like Unrecognized criterion).

Thanks for help.

EDIT

Solution suggested by Max works fine but I can't accept answer because it's a comment.

Upvotes: 2

Views: 3450

Answers (2)

Max
Max

Reputation: 10985

Perhaps you should store the last UID you got. UIDs are guaranteed to increase as messages are added to the mailbox.

Upvotes: 0

TML
TML

Reputation: 12966

The below works fine for me:

$imap    = imap_open('{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX', "[email protected]", "qqqqqqqqqqqqqqqqqq");
$search  = imap_search($conn, 'ALL UNDELETED SINCE "28-OCT-12"', SE_UID);

(Note that PHP's imap API only uses the mail_criteria() function in the c-client library, and thus is using the IMAP2-style "SEARCH" command rather than IMAP4, due to resistance from the c-client maintainer(s) to provide a generic API for parsing SEARCH Commands into a SEARCHPGM.)

Upvotes: 1

Related Questions