imaplib ... understanding the result from a fetch of RFC822s
Jp Calderone
exarkun at divmod.com
Thu Nov 18 01:06:52 EST 2004
On Wed, 17 Nov 2004 21:26:35 +0100, Max M <maxm at mxm.dk> wrote:
>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?
>
Parsing IMAP4 is rather involved. imaplib does a minimal job of it, and leaves many of the details up to you. For a more complete parser, see Twisted's IMAP4 support: http://www.twistedmatrix.com/
Jp
More information about the Python-list
mailing list