imaplib ... understanding the result from a fetch of RFC822s

Max M maxm at mxm.dk
Wed Nov 17 15:26:35 EST 2004


I am using the fetch command from the imaplib to fetch messages. I get a 
result, but I am a bit uncertain as to how I should interpret it.

The result is described at http://pydoc.org/2.3/imaplib.html as::

(typ, [data, ...]) = <instance>.fetch(message_set, message_parts)

In RFC 2060 it says: "The data items to be fetched can be either a 
single atom or a parenthesized list."

So I do a fetch like:

     mailconn.uid('fetch', '1:*', '(RFC822)')

As a result I receive the following results (from 2 different servers):

# mailserver 1
messages = [
     ('1 (UID 2 RFC822 {616}', "Received: from SNIP..."),
     ')',
     ('2 (UID 4 RFC822 {626}', "Received: from SNIP..."),
     ')',
]

# mailserver 2

messages = [
     ('1 (RFC822 {1155}', "Return-path: SNIP..."),
     ' UID 1)',
     ('2 (RFC822 {977}', "Return-path: SNIP..."),
     ' UID 2)',
     ('3 (RFC822 {1016}', "Return-path: SNIP..."),
     ' UID 3)',
     ('4 (RFC822 {1153}', "Return-path: SNIP..."),
     ' UID 4)',
     ('5 (RFC822 {732}', 'Mime-Version: SNIP...'),
     ' UID 5)',
]

It's just a long list which seems to have the structure:

list = [
     (envelope start, rfc288-message), envelope-end,
     (envelope start, rfc288-message), envelope-end,
     (envelope start, rfc288-message), envelope-end,
]

To me this is an odd format. It's sort of a parenthesized list, but not 
really.

I guess that I can iterate it like:

for ((envelopeStart, msg), envelopeEnd) in range(0, len(messages), 2):
     # do stuff

But I feel a bit uncertain that it won't break in some edge cases.

Does anybody have a clue as to why imaplib returns results like that?


-- 

hilsen/regards Max M, Denmark

http://www.mxm.dk/
IT's Mad Science



More information about the Python-list mailing list