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

M

Max M

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
 
M

Max M

Max said:
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
 
D

Donn Cave

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, (e-mail address removed)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top