Checking reading and deleting mail

Before you can do anything with messages, you must choose a mailbox. The mailbox INBOX is always available. To select a mailbox, call select([mailbox[, readonly]]) . The parameter mailbox is the mailbox name, which defaults to INBOX. If readonly is present and true, then modifications to the mailbox are forbidden. The return value includes the number of messages in the mailbox. For example:

>>> imap.select("INBOX") ('OK', ['2'])

When finished with a mailbox, call close to close it.

The method search(charset,criteria...) searches the current mailbox for messages satisfying one or more criteria. The parameter charset, if not None, specifies a particular character set to use. One or more values can be passed as criteria; these are concatenated into one search string. A list of matching message indexes is returned. Note that text (other than keywords) in criteria should be quoted. For example, the following code checks for messages from the president (none today), and then checks for messages whose subject contains "Howdy!" (and finds message number 2):

>>> imap.search(None,"ALL","FROM",'"[email protected]"') ('OK', [None])

>>> imap.search(None,"ALL","SUBJECT",'"Howdy!"') ('OK', ['2'])

To retrieve a message, call fetch(messages,parts). Here, messages is a string listing messages, such as "2", or "2,7", or "3:5" (for messages 3 through 5). The parameter parts should be a parenthesized list of what parts of the message(s) to retrieve — for instance, FULL for the entire message, BODY for just the body. For example:

>>> imap.fetch("2","(BODY[text])")

('OK', [('2 (BODY[text] {13}', 'Howdy cowboy!'), ')', '2 (FLAGS (\\SEEN))'])

To change a message's status, call store(messages,command,flags). Here, command is the command to perform, such as " + FLAGS" or "-FLAGS". The parameter flags is a list of flags to set or remove. For example, the following line of code deletes message 2:

>>> imap.store("2","+FLAGS",["\Deleted"]) ('OK', ['2 (FLAGS (\\SEEN \\DELETED))'])

The method expunge permanently removes all messages marked as deleted by a \Deleted flag. Such messages are automatically expunged when you close the current mailbox.

The method copy(messages,newmailbox) copies a set of messages to the mailbox named newmailbox.

The method check does a mailbox "checkpoint" operation; what this means depends on the server.

You normally operate on messages by index number. However, messages also have a unique identifier, or uid. To use uids to name messages, call the method uid (commandname, [args...]). This carries out the command commandname using uids instead of message indices.

0 0

Post a comment