Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IMAP: Search for messages with UID greater than X (or generally, after my last search)

I'm writing a script to analyze my mailbox and want to periodically check for new messages. The search criteria would be: give me the UIDs for all emails with UID greater than X, where X is the UID of the last email I processed.

Or, more generally, I'm looking for a way to only see messages since my last search.

Note that I'm not looking for seen/unseen messages; the script opens the mailbox as read-only, and I'd like it to not interfere with my flags, etc.

I know I can specify a date in the IMAP search, but the granularity of that seems to be by day, so not exactly what I need.

I'm starting with Gmail as the IMAP server, but would like to support generic IMAP servers in the future.

Is there way to search for emails with UID greater than X? Or another means of specify all messages since message X?

like image 978
Parand Avatar asked Feb 05 '12 06:02

Parand


2 Answers

You can use IMAP SEARCH for UIDs. Assuming your most recently fetched UID is 1999, I think you would do:

SEARCH UID 2000:*

like image 92
SimonMayer Avatar answered Oct 21 '22 10:10

SimonMayer


Why not use IMAP IDLE for this?

with IMAP IDLE, the server warns you every time a new message arrives

like image 26
537mfb Avatar answered Oct 21 '22 10:10

537mfb