10

I try to get an unique id that's not change during operation. I think UID is not good. So I think 'Message-ID' is the right thing, But I don't know how to get it. I know just imap.fetch(uid, 'XXXX'), Anyone has a solution?.

vernomcrp
  • 3,459
  • 11
  • 34
  • 44

4 Answers4

10

From IMAP documentation itself:

IMAP4 message numbers change as the mailbox changes; in particular, after an EXPUNGE command performs deletions the remaining messages are renumbered. So it is highly advisable to use UIDs instead, with the UID command.

Discussion at SO : About IMAP UID with imaplib

IMAP4.fetch(message_set, 'UID')

Fetch is the best way to get the UID of the message

And to get message ID you could do some thing like this, Although not all messages may have a message id.

server.select(imap_folder)
# List all messages
typ, data = server.search(None, 'ALL') 
# iterate through messages
for num in data[0].split():
    typ, data = server.fetch(num, '(BODY[HEADER.FIELDS (MESSAGE-ID)])')
    # parse data to get message id
Community
  • 1
  • 1
pyfunc
  • 65,343
  • 15
  • 148
  • 136
  • 1
    @pyfunc: But the accepted answer to the question you linked to states that `Note: UID is changed but the Message-Id will not be changed during any operation on that mail)`. – Manoj Govindan Sep 23 '10 at 05:48
  • Yeah, The IMAP lib documentation contradicts that discussion – pyfunc Sep 23 '10 at 05:58
  • 1
    The UID won't change, but the question linked to talks about moving a message to another mailbox. After the move, the UID will be different -- it's really a new message in a different location. – Thomas Wouters Sep 23 '10 at 07:36
  • 1
    @pyfunc : Can u plz show me any mail which has no Message-Id ? – Avadhesh Sep 23 '10 at 09:14
  • @pyfunc: It is not good to execute Fetch command inside the loop , it will reduce the speed. u just fetch the header of all mails in 1 go. – Avadhesh Sep 23 '10 at 09:57
  • @pyfunc: I think its great that how you get message-id, but I don't think UID will not change when do operation just like 'move to other mailbox' . Anyway Thank you all :) – vernomcrp Sep 26 '10 at 20:47
  • @Avadhesh Using Message-Id is not unproblematic. The RFC requires each message to have a Message-Id, but spammers (and PHP programmers, which seems to be more or less indistinguishable from the first group in practice) don't read the RFCs. If the Message-Id is not generated properly when the message is sent, it is *likely* to be generated en route, but this is a precarious assumption. Also, there was historically a problem with a particular mail server (cough *Microsoft* cough) which did not manage to generate unique Message-id:s. In the context of a single IMAP mailbox, definitely use UID. – tripleee Jun 22 '16 at 05:21
6

You can try this python code to fetch the header information of all the mails.

import imaplib
import email

obj = imaplib.IMAP4_SSL('imap.gmail.com', 993)
obj.login('username', 'password')
obj.select('folder_name')
resp,data = obj.uid('FETCH', '1:*' , '(RFC822.HEADER)')
messages = [data[i][1].strip() + "\r\nSize:" + data[i][0].split()[4] + "\r\nUID:" + data[i][0].split()[2]  for i in xrange(0, len(data), 2)]
for msg in messages:
    msg_str = email.message_from_string(msg)
    message_id = msg_str.get('Message-ID')
Vahid Chakoshy
  • 1,497
  • 15
  • 27
Avadhesh
  • 4,519
  • 5
  • 33
  • 44
4

There is much easier method for this...

typ, data = obj.fetch(num, '(BODY[HEADER.FIELDS (MESSAGE-ID)])')
msg_str = email.message_from_string(data[0][1])
message_id = msg_str.get('Message-ID')
print message_id

Hope this helps!

Shreejibawa
  • 1,860
  • 1
  • 25
  • 35
2
result, data = imapconnection.uid('search', None, "ALL") # search and return uids instead
latest_email_uid = data[0].split()[-1]
result, data = imapconnection.uid('fetch', latest_email_uid, '(RFC822)')
raw_email = data[0][1]
Sgali
  • 362
  • 2
  • 12
  • 5
    It would be helpful if you added some explanation to your post. In programming for any language or system, the *logic* behind it is the most important, and the actual *code* is secondary to the logic - understanding the logic helps people learn how to write this code themselves later, and tweak it to fit their needs. – CodeMouse92 Sep 24 '15 at 19:37
  • Where is `imapconnection` defined? Is this an `imaplib.IMAP4_SSL` object? – Joel Jul 18 '18 at 12:51