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

Discussion in 'Python' started by Max M, Nov 17, 2004.

  1. Max M

    Max M Guest

    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
     
    Max M, Nov 17, 2004
    #1
    1. Advertising

  2. Max M

    Max M Guest

    Max M wrote:

    > I guess that I can iterate it like:
    >
    > for ((envelopeStart, msg), envelopeEnd) in range(0, len(messages), 2):
    > # do stuff


    I guess not. I really meant:

    for i in range(0, len(results), 2):
    ((envelopeStart, msg), envelopeEnd) = (results[0], results[1])

    --

    hilsen/regards Max M, Denmark

    http://www.mxm.dk/
    IT's Mad Science
     
    Max M, Nov 17, 2004
    #2
    1. Advertising

  3. Max M

    Donn Cave Guest

    In article <419bb3a7$0$259$>,
    Max M <> 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..."),
    > ')',
    > ]

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


    It has to parse the response that far, in order to read the
    whole thing. That '{616}' is as you probably surmised the
    length of the following text, spanning more than a single line,
    so imaplib needs that number. The intent is not to provide
    you with a fully parsed IMAP4 response, you're just getting
    the data and whatever parsing was needed along the way. In
    a perversely ideal sense, it might have been better to put
    the pieces back together and just give you the response as
    one string.

    Donn Cave,
     
    Donn Cave, Nov 18, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jp Calderone
    Replies:
    1
    Views:
    334
    Max M
    Nov 19, 2004
  2. Antoon Pardon
    Replies:
    5
    Views:
    393
    Donn Cave
    Nov 30, 2006
  3. Rich Healey

    imaplib fetch message flags

    Rich Healey, Mar 2, 2009, in forum: Python
    Replies:
    0
    Views:
    417
    Rich Healey
    Mar 2, 2009
  4. Gabriel Genellina

    Re: imaplib fetch message flags

    Gabriel Genellina, Mar 2, 2009, in forum: Python
    Replies:
    1
    Views:
    1,169
    Lawrence D'Oliveiro
    Mar 2, 2009
  5. Michael Tan
    Replies:
    32
    Views:
    997
    Ara.T.Howard
    Jul 21, 2005
Loading...

Share This Page