Foundations of Python Network Programming

(WallPaper) #1
Chapter 15 ■ IMap

281

The results of this fetch() call are printed to the screen once an e-mail folder has been selected:

2703 2010-09-28 21:32:13 19129 bytes \Seen
From: Brandon Craig Rhodes
Subject: Digested Articles


2704 2010-09-28 23:03:45 15354 bytes
Subject: Re: [venv] Building a virtual environment for offline testing
From: "W. Craig Trader"


2705 2010-09-29 08:11:38 10694 bytes
Subject: Re: [venv] Building a virtual environment for offline testing
From: Hugo Lopes Tavares


Folder INBOX - type a message UID, or "q" to quit:


As you can see, the fact that several items of interest can be supplied to the IMAP fetch() command allows you
to build fairly sophisticated message summaries with only a single roundtrip to the server!
Once the user has selected a particular message, a technique that I have not discussed so far is used: fetch() is
asked to return the BODYSTRUCTURE of the message, which is the key to seeing a MIME message’s parts without having
to download its entire text. Instead of making you pull several megabytes over the network just to list a large message’s
attachments, BODYSTRUCTURE simply lists its MIME sections as a recursive data structure.
Simple MIME parts are returned as a tuple:


('TEXT', 'PLAIN', ('CHARSET', 'US-ASCII'), None, None, '7BIT', 2279, 48)


The elements of this tuple, which are detailed in section 7.4.2 of RFC 3501, are as follows (starting from item
index zero, of course):



  1. MIME type

  2. MIME subtype

  3. Body parameters, presented as a tuple (name, value, name, value, ...) where each
    parameter name is followed by its value

  4. Content ID

  5. Content description

  6. Content encoding

  7. Content size in bytes

  8. For textual MIME types, this gives the content length in lines


When the IMAP server sees that a message is multipart, or when it examines one of the parts of the message that
it discovers is itself multipart (see Chapter 12 for more information about how MIME messages can nest other MIME
messages inside them), then the tuple it returns will begin with a list of substructures, which are each a tuple laid out
just like the outer structure. Then it will finish with some information about the multipart container that bound those
sections together:


([(...), (...)], "MIXED", ('BOUNDARY', '=-=-='), None, None)

Free download pdf