10

In our internal webmail system, we'd like to attach notes and contacts to certain mails. In order to do this, we have to keep track of every mail on our IMAP server.

Unfortunately the IMAP standard doesn't enforce the uniqueness of the UID of a mail in a mailbox (just in subfolders). Is there any tool/IMAP server which generates UIDs which are truly unique? Or is there any other way how we can identify each mail? (the Message-ID header field is not unique as some mails do not contain such a field).

Additional resources: Unique ID in IMAP protocol - Limilabs.com

HopelessN00b
  • 53,385
  • 32
  • 133
  • 208
SecStone
  • 197
  • 1
  • 2
  • 7

2 Answers2

10

A tuple of (folder name, folder UIDVALIDITY, message UID) distinguishes a message from all others in an IMAP account. For any given message, that tuple can change between sessions, so you might want to track its Message-ID as a backup in case this happens.

Yes, it really is that annoying.

ʇsәɹoɈ
  • 231
  • 2
  • 4
  • But [RFC-2822](https://tools.ietf.org/html/rfc2822#section-3.6.4) says that Message-ID SHOULD exist in a message which means that there can be no Message-ID at all – Unicorn Apr 28 '18 at 05:35
  • 1
    That's right, @Romeno. IMAP is flawed and Message-ID only helps sometimes. – ʇsәɹoɈ Apr 28 '18 at 18:06
6

I don't know what you mean when you say UIDs are not unique in a mailbox, just in subfolders. By subfolders, do you mean anything other than INBOX? In any case, it sounds wrong. UIDs are in fact unique within each folder and it doesn't matter it it's INBOX or any other folder.

What can happen is that UIDs can change between sessions. Generally this happens if the IMAP server loses its indexes and has to rebuild them or if the mail is migrated from one IMAP server to another, etc... If this happens then you will of course lose all of the associations between emails and extra data (notes or contacts).

You can rely on this: a UID will never suddenly refer to a different message. Either it will remain valid and refer to the same message it always referred to, or else it will become invalid.

You might want to check out how your particular IMAP server generates and maintains UIDs. Different IMAP servers make different promises about the persistency of UID values. Even within one IMAP server, it may vary depending on which mailbox format is in use. For example, with Dovecot, expect less fragile UIDs with mdbox than with mbox or Maildir because the metadata are more tightly integrated with the actual emails with mdbox.

I suggest using UIDs to refer to messages on the IMAP server, with Message-ID as a backup. Message-ID is not as good because duplicates are possible and (for most IMAP servers) searching by Message-ID might be slower, but it's better than losing track of the messages completely.

Celada
  • 6,060
  • 1
  • 20
  • 17