0

I'm trying to revive a python 2 code that I found on github to clean out my icloud mails. My code logs in, selects INBOX and fetches status, IDs and UIDs successfully using imaplib.IMAP4_ssl as email_connection.

It, then, should flag emails in Apple's icloud Mail for deletion:

email_connection.uid("STORE", email_uid, "+FLAGS", "(\\Deleted)") 

but this is where it throws an

imaplib.error: UID command error: BAD [b'Parse Error (took 0 ms)']

The original code used int(email_uid) at the same spot which I changed as it threw

TypeError: can't concat int to bytes

email_uid is now passed as a str which produces the UID command error. Online research gave a lot of info that was gmail-specific. Anyone with experience of using imaplib on Apple Mail?

Thanks!

Here is the debug log starting a few lines before the break:

    06:34.78 < b'* 198 FETCH (UID 47520)'
  06:34.78  matched b'\\* (?P<data>\\d+) (?P<type>[A-Z-]+)( (?P<data2>.*))?' => (b'198', b'FETCH', b' (UID 47520)', b'(UID 47520)')
  06:34.78 untagged_responses[FETCH] 0 += ["b'198 (UID 47520)'"]
  06:34.78 < b'IAFG4 OK FETCH completed (took 52 ms)'
  06:34.78  matched b'(?P<tag>IAFG\\d+) (?P<type>[A-Z]+) (?P<data>.*)' => (b'IAFG4', b'OK', b'FETCH completed (took 52 ms)')
  06:34.78 untagged_responses[FETCH] => [b'198 (UID 47520)']
  06:34.78 > b'IAFG5 UID STORE  47520 +FLAGS (\\Deleted)'
  06:34.89 < b'IAFG5 BAD Parse Error (took 0 ms)'
  06:34.89  matched b'(?P<tag>IAFG\\d+) (?P<type>[A-Z]+) (?P<data>.*)' => (b'IAFG5', b'BAD', b'Parse Error (took 0 ms)')
  06:34.89 BAD response: b'Parse Error (took 0 ms)'
  • 1
    Can you turn on full protocol logging (I believe debug=5? to the IMAP4_ssl constructor) and paste the log where it fails? – Max Oct 05 '22 at 14:42
  • @Max: Done! Added it to the question. – user2822564 Oct 05 '22 at 15:19
  • 1
    It looks like there may be some stray sort of white space before your UID, or perhaps some Unicode garbage. Can you check and maybe hex dump your email_uid variable? – Max Oct 05 '22 at 15:31
  • email_uid.strip() solved the problem - I guess, whitespace was the problem. the hexdump gave me `00000000: 31 39 38 20 28 55 49 44 20 34 37 35 32 30 29 198 (UID 47520)`. How did you spot this? thanks a lot! – user2822564 Oct 05 '22 at 18:53
  • Great thing about fixedwidth fonts is it makes it easy to pick out that it looked "weird" :) – Max Oct 05 '22 at 20:17

1 Answers1

0

The problem is the double space.

IMAP is strict about where space is required and permitted. Space is only permitted where required, and required only where a delimiter is necessary to avoid misunderstandings. A single space character is enough to avoid misunderstandings, and therefore the second space is an error.

arnt
  • 8,949
  • 5
  • 24
  • 32