I am trying to fetch messages from my IMAP server. This is a short version of my code:
>>> import imaplib
>>> m = imaplib.IMAP4_SSL(host='my.host.com')
>>> m.login('username', 'password')
>>> m.select('INBOX')
The imaplib module in python3.4 is compatible to the RFC 2060 Standard. There it says:
The FETCH command retrieves data associated with a message in the mailbox. The data items to be fetched can be either a single atom or a parenthesized list.
Now I get some different results, which I cannot understand:
>>> m.fetch('1613', '(ALL)')
imaplib.error: FETCH command error: BAD [b'Invalid Fetch attribute ALL']
>>> m.fetch('1613', 'ALL')
('OK', [b'1613 (FLAGS (\\Seen) INTERNALDATE' ... etc ...
>>> m.fetch('1613', '(FLAGS)')
('OK', [b'1613 (FLAGS (\\Seen))'])
>>> m.fetch('1613', 'FLAGS')
('OK', [b'1613 (FLAGS (\\Seen))'])
>>> m.fetch('1613', '(FLAGS ENVELOPE)')
('OK', ... etc ...
>>> m.fetch('1613', '(FLAGS ALL)')
imaplib.error: FETCH command error: BAD [b'Invalid Fetch attribute ALL']
Can someone explain to me why I cannot use the ALL
item with the parentheses?
The reason why I am asking is because I am using the IMAPClient module, which automatically adds the parentheses to the parameters, and therefore the ALL
item won't work.
ALL isn't a fetch item, it's a macro. A bit a special case in the syntax. There are two others, FAST and FULL. They're like fetch items but with some extra restrictions, one of which you've run into.
I suggest that you just use the equivalent fetch items and you'll get the result you want without having to deal with the special restrictions. From RFC3501:
ALL
Macro equivalent to: (FLAGS INTERNALDATE RFC822.SIZE ENVELOPE)
FAST
Macro equivalent to: (FLAGS INTERNALDATE RFC822.SIZE)
FULL
Macro equivalent to: (FLAGS INTERNALDATE RFC822.SIZE ENVELOPE BODY)