By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,136 Members | 2,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,136 IT Pros & Developers. It's quick & easy.

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

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a
In article <41*********************@dread12.news.tele.dk>,
Max M <ma**@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..."),
')',
] .... 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, do**@u.washington.edu
Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.