Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.
The IMAP protocol is a fairly finicky and complex beast, but the
good news is that you don’t have to know much of it to search and fetch
mail messages. imaplib-compliant examples are readily available
online, and one of the more common operations you’ll want to do
is search for messages. There are various ways that you can construct an
IMAP query. An example of how you’d search for messages from a
particular user is conn.search(None, '(FROM "me")'), where
None is an optional parameter for the character set and '(FROM
"me")' is a search command to find messages that you’ve sent
yourself (Gmail recognizes “me” as the authenticated user). A command to
search for messages containing “foo” in the subject would be
'(SUBJECT "foo")', and there are many additional possibilities that you can read
about in Section 6.4.4 of RFC 3501, which
defines the IMAP specification. imaplib returns a search
response as a tuple that consists of a status code and a string of
space-separated message IDs wrapped in a list, such as ('OK',
['506 527 566']). You can parse out these ID values to fetch
RFC
822-compliant mail messages, but alas, there’s additional work
involved to parse the content of the mail messages into a usable form.
Fortunately, with some minimal adaptation, we can reuse the code from
Example 3-3, which used the
email module to parse messages into a more readily usable
form, to take care of the uninteresting email-parsing cruft that’s
necessary to get usable text from each message. Example 7-12 illustrates.