0

I am trying to get all the UID's of the INBOX folder in a gmail account using imaplib (account is new for testing purposes). It worked fine at first (i.e. when I had only sent mails to the account), but after moving some mails to trash, my code now returns both a wrong amount of UIDs (8, with 7 emails in the inbox folder), while also missing a UID.

con = imaplib.IMAP4_SSL(imap_server, imap_port)
con.login(user, password)
con.select("INBOX")
result, numbers = con.uid('search', None, 'ALL')
uids = numbers[0].split()

In my specific case, printing "uids" gives the following list

[b'1', b'3', b'4', b'7', b'8', b'9', b'10', b'11']

Manually checking the mails by writing a list [b'1', b'2'..., b'11'] and fetching the email subjects, I find out that there are unique mails for the following UIDs:

[b'1', b'2' OR b'3', b'4', b'5', b'6', b'7', ONE OF b'8' to b'11']

b'1' and b'2' returns the same mail, b'6' is missing, and b'8' through b'11 returns the same mail

Searching around here before asking this, I came across this question: Download attachments from gmail. The answer had this bit of code that fetches id for all mails of a folder:

resp, items = m.search(None, "ALL") # you could filter using the IMAP rules here (check http://www.example-code.com/csharp/imap-search-critera.asp)
items = items[0].split() # getting the mails id

Similar to my code, just not using the uid method. However, in my case, this makes the list:

[b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8']

So also not correct. Another answer I found Imaplib with GMail offset uids writes:

It seems like M.uid simply specifies that the return value will be UID's, so it is still necessary to specify that the parameters sent will be UID's and not message ID's. This fixes it:

rv, data = M.uid("search", None, 'UID', '29540:*')

And in a comment:

rv, data = M.uid("search", None, '(UID 29540:*)')

But both return an error for me: UID command error: BAD [b'Could not parse command']

What am I doing wrong, and/or is there a better way?

Zsigma
  • 1
  • 1
    Have you checked the flags of these "extra" messages? They may be marked `\Deleted`, which usually means you should ignore them. Your question as it stands doesn't really indicate why you think the UIDs are wrong, and you don't show your code for fetching your manual UID list. If you mix UIDs with `fetch` (and not uid fetch) you will get very confusing results. – Max Mar 10 '20 at 14:57
  • For now I can answer the second part of the comment (I am very new to IMAP and will have to find out how to get the flags): I did not use any code to generate the manual UID list, I just typed it in (I couldn't think of a better way to check if the search was missing UIDs or it was a problem somewhere else in the program, and at least it helped isolate the problem) – Zsigma Mar 10 '20 at 17:16
  • I think you can just use con.uid('search', 'ALL'). This command doens't take the charset parameter, unlike the con.search() version. You might actually be sending UID SEARCH NIL ALL, or UID SEARCH "" ALL, due to the extra None. Turning protocol logging by setting .debug on the connection object to a fairly large value might help you see what is sending. – Max Mar 10 '20 at 17:39
  • I tried using con.uid('search', 'ALL'), but it returns the same values as with `None` as the charset. I am not sure what a fairly large value for .debug is, but from the docs values > 3 traces all commands, so I used 4. From the debug output it seems the commands are successful (ie search for 'ALL' in the inbox), but they still return the wrong values. I would post the debug output, but it seems you cannot format comments, and I am not sure it is the right place either. I am new to both stack overflow and programming, so if you have suggestions on how to move forward I would be grateful. – Zsigma Mar 12 '20 at 13:10
  • Updating the question with as much information as possible and your exact code is more useful than providing questions. – Max Mar 12 '20 at 13:35

0 Answers0