Foundations of Python Network Programming

(WallPaper) #1
Chapter 15 ■ IMap

287

Summary


IMAP is a robust protocol for accessing e-mail messages stored on a remote server. Many IMAP libraries exist for
Python; imaplib is built into the Python Standard Library, but it requires you to do all sorts of low-level response
parsing by yourself. A far better choice is IMAPClient by Menno Smits, which you can install from the Python
Package Index.
On an IMAP server, your e-mail messages are grouped into folders, some of which will come predefined by your
particular IMAP provider and some of which you can create yourself. An IMAP client can create folders, delete folders,
insert new messages into a folder, and move existing messages between folders.
Once a folder has been selected, which is the rough IMAP equivalent of a “change directory” command on a
file system, messages can be listed and fetched very flexibly. Instead of having to download every message in its
entirety (though, of course, that is an option), the client can ask for particular information from a message, like a
few headers and its message structure, in order to build a display or summary into which the user can click, pulling
message parts and attachments down from the server on demand.
The client can also set flags on each message—some of which are also meaningful to the server—and it can delete
messages by setting the \Delete flag and then performing an expunge operation.
Finally, IMAP offers sophisticated search functionality so that common user operations can be supported without
requiring the e-mail data to be downloaded to the local machine.
In the next chapter we will leave the topic of e-mail and consider a quite different type of communication:
delivering shell commands to a remote server and receiving their output in response.

Free download pdf