Unique Email Identifiers with IMAP

末鹿安然 提交于 2021-02-19 08:58:10

问题


I am creating an GMail email interface in Django/Python. I want to create a skeleton of all emails, containing subject and date, a kind of overview-level index, so that even if an email is deleted, I have an indicator it existed and when it was sent.

My problem is, considering that IMAP UIDs change, what can I use as a unique email identifier? Even if I create the skeleton of each email with a unique ID in my database, if I associate a email UID and IMAP folder name to it, the UID can change when other emails are deleted.

What would be the best solution to create a unique identifier for an email? I was thinking of using a (sent date, subject) combination, but I was looking for something simpler.

Can somebody help?

Thank you


回答1:


My understanding is that the UID won't change... so should be fine to use that as a link between IMAP/your DB.

From RFC3501 (updates RFC822) - http://www.limilabs.com/mail/rfc/3501

2.3.1.1. Unique Identifier (UID) Message Attribute

A 32-bit value assigned to each message, which when used with the
unique identifier validity value (see below) forms a 64-bit value
that MUST NOT refer to any other message in the mailbox or any
subsequent mailbox with the same name forever. 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.

The unique identifier of a message MUST NOT change during the
session, and SHOULD NOT change between sessions. Any change of
unique identifiers between sessions MUST be detectable using the
UIDVALIDITY mechanism discussed below.




回答2:


What I've found is that every folder has it's own UID seed, which means that you will have repetetive UIDs in different folders for different messages. RFC822 specifies another identifier, Message-ID header of an email, which is usually autogenerated (eg. Gmail) which you can use to fix your UID after the message has been moved. IMAP supports querying for certain fields, like UID,MessageID.

Using this combined key you can always track every message in the system.

You can generate your own MessageID if you are doing the sending yourself. Here is how to generate it:

http://www.jwz.org/doc/mid.html

More reading:

http://en.wikipedia.org/wiki/Message-ID



来源:https://stackoverflow.com/questions/13593869/unique-email-identifiers-with-imap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!