I would like to hold a cache of emails to a group, and verify that the cache is not missing any uids periodically. First thing I tried was this method using imaplib
which the search comes back with all UID's of the Mailbox. Then you verify all ids have a cached counterpart.
con = imaplib.IMAP4_SSL("imap.gmail.com")
con.login(user, password)
>>> con.select('INBOX')
('OK', [b'15613'])
>>> con.search(None, "TO", "mail_group")
('OK', [b'13267 13277 13285 13286 13290 13306 15591 15612 15613"]
I switched to imap_tools which also has a similar query but a nicer API.
con = imap_tools.MailBox('imap.gmail.com').login(user, password, initial_folder='INBOX')
con.folder.status('INBOX')
{'MESSAGES': 15615, 'RECENT': 0, 'UIDNEXT': 66119, 'UIDVALIDITY': 1, 'UNSEEN': 51}
>>> con.search(AND(to='mail_group'))
['15534', '15557', '15558', '15565', '15566', '15567', '15571', '15573', '15576', '15579', '15580', '15582', '15584', '15588', '15589', '15591', '15612', '15613']
Using this search I then fetch ids that are not on disk. The account is an audit account and does not delete any message so I assumed that these ids will not change.
The problem I'm seeing using imap_tools is when you fetch a UID that you got in the search operation, the msg.uid
does not match it.
>>> msg = list(con.fetch(uid))[0]
>>> msg.uid
'66268'
>>> uid
15765
I'm not sure how 66268 connects with 15765. So why is 66268 the imap_tools
UID for this message and how can you reconcile with these two different ids? Am I approaching this the wrong way?
UPDATE:
For imap-tools>=0.45.0 Added new method: uids