1

I am working on PHP project that should fetch emails from IMAP server, and store them in local database. Same IMAP server can be used by other email clients, like outbox and so on.

The problem is how to know which messages I already fetched, and which I didn't? I am thinking to use search by datetime, but is it reliable(I would have cronjob, that would access user mail box every minute, and check for emails, but not sure if datetime can cause some issues, for example in case when at almost same time arrive short message and message with big attachment).

I was thinking about system tags, but user can modify them via email client, so I can rely on them, and don't want to modify them and confuse client.

Next I was thinking about custom tags, but not all IMAP servers support them(and our software need to be flexible as much as possible).

Any good idea how I could solve this problem?

Ivica
  • 795
  • 2
  • 8
  • 20

1 Answers1

3

Keep track of the currently highest synced UID of the folder you are syncing, and verify that the UIDVALIDITY value of the folder match.

Unique identifiers are assigned in a strictly ascending fashion in the mailbox; as each message is added to the mailbox it is assigned a higher UID than the message(s) which were added previously. Unlike message sequence numbers, unique identifiers are not necessarily contiguous.

Community
  • 1
  • 1
Anders Lindahl
  • 41,582
  • 9
  • 89
  • 93
  • http://stackoverflow.com/questions/1084780/getting-only-new-mail-from-an-imap-server I found another similar post, and I got idea whet to do if UIDVALIDITY doesn't match. Thanks! – Ivica Apr 14 '12 at 06:33
  • There is a problem. PHP imap_search function doesn't suport searching messages by UID, seems that I can use only dates:( – Ivica Apr 16 '12 at 11:03
  • Instead of imap_search, you probably have to use imap_fetch_overview. See this answer: https://stackoverflow.com/questions/39402349/php-imap-search-uid-search-returns-false – Benedikt Aug 27 '21 at 06:09