09.11.2016 Views

Foundations of Python Network Programming 978-1-4302-3004-5

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

CHAPTER 15 ■ IMAP<br />

An IMAP message has two dates: the internal Date header specified by the sender, which is called its<br />

“send date,” and the date at which it actually arrived at the IMAP server. (The former could obviously be<br />

a forgery; the latter is as reliable as the IMAP server and its clock.) So there are two sets <strong>of</strong> criteria for<br />

dates, depending on which date you want to query by:<br />

BEFORE 01-Jan-1970<br />

ON 01-Jan-1970<br />

SINCE 01-Jan-1970<br />

SENTBEFORE 01-Jan-1970<br />

SENTON 01-Jan-1970<br />

SENTSINCE 01-Jan-1970<br />

Finally, there are two search operations that refer to the text <strong>of</strong> the message itself—these are the big<br />

workhorses that support full-text search <strong>of</strong> the kind your users are probably expecting when they type<br />

into a search field in an e-mail client:<br />

BODY string: The message body must contain the string.<br />

TEXT string: The entire message, either body or header, must contain the string somewhere.<br />

See the documentation for the particular IMAP server you are using to learn whether it returns any<br />

“near miss” matches, like those supported by modern search engines, or only exact matches for the<br />

words you provide.<br />

If your strings contain any characters that IMAP might consider special, try surrounding them with<br />

double-quotes, and then backslash-quote any double-quotes within the strings themselves:<br />

>>> c.search(r'TEXT "Quoth the raven, \"Nevermore.\""')<br />

[2652L]<br />

Note that by using an r'...' string here, I avoided having to double up the backslashes to get single<br />

backslashes through to IMAP.<br />

Manipulating Folders and Messages<br />

Creating or deleting folders is done quite simply in IMAP, by providing the name <strong>of</strong> the folder:<br />

c.create_folder('Personal')<br />

c.delete_folder('Work')<br />

Some IMAP servers or configurations may not permit these operations, or may have restrictions on<br />

naming; be sure to have error checking in place when calling them.<br />

There are two operations that can create new e-mail messages in your IMAP account besides the<br />

“normal” means <strong>of</strong> waiting for people to send them to you.<br />

First, you can copy an existing message from its home folder over into another folder. Start by using<br />

select_folder() to visit the folder where the messages live, and then run the copy method like this:<br />

c.select_folder('INBOX')<br />

c.copy([2653L, 2654L], 'TODO')<br />

Finally, it is possible to add a message to a mailbox with IMAP. You do not need to send the message<br />

first with SMTP; IMAP is all that is needed. Adding a message is a simple process, though there are a<br />

couple <strong>of</strong> things to be aware <strong>of</strong>.<br />

The primary concern is line endings. Many Unix machines use a single ASCII line feed character<br />

(0x0a, or '\n' in <strong>Python</strong>) to designate the end <strong>of</strong> a line <strong>of</strong> text. Windows machines use two characters:<br />

CR-LF, a carriage return (0x0D, or '\r' in <strong>Python</strong>) followed by a line feed. Older Macs use just the<br />

carriage return.<br />

260

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!