18.4. mailbox — Manipulate mailboxes in various formats¶
This module defines two classes, Mailbox and Message, for
accessing and manipulating on-disk mailboxes and the messages they contain.
Mailbox offers a dictionary-like mapping from keys to messages.
Message extends the email.message module’s
Message class with format-specific state and behavior.
Supported mailbox formats are
Maildir, mbox, MH, Babyl, and MMDF.
See also
- Module
email Represent and manipulate messages.
18.4.1. Mailbox objects¶
-
class
mailbox.Mailbox¶ A mailbox, which may be inspected and modified.
The
Mailboxclass defines an interface and is not intended to be instantiated. Instead, format-specific subclasses should inherit fromMailboxand your code should instantiate a particular subclass.The
Mailboxinterface is dictionary-like, with small keys corresponding to messages. Keys are issued by theMailboxinstance with which they will be used and are only meaningful to thatMailboxinstance. A key continues to identify a message even if the corresponding message is modified, such as by replacing it with another message.Messages may be added to a
Mailboxinstance using the set-like methodadd()and removed using adelstatement or the set-like methodsremove()anddiscard().Mailboxinterface semantics differ from dictionary semantics in some noteworthy ways. Each time a message is requested, a new representation (typically aMessageinstance) is generated based upon the current state of the mailbox. Similarly, when a message is added to aMailboxinstance, the provided message representation’s contents are copied. In neither case is a reference to the message representation kept by theMailboxinstance.The default
Mailboxiterator iterates over message representations, not keys as the default dictionary iterator does. Moreover, modification of a mailbox during iteration is safe and well-defined. Messages added to the mailbox after an iterator is created will not be seen by the iterator. Messages removed from the mailbox before the iterator yields them will be silently skipped, though using a key from an iterator may result in aKeyErrorexception if the corresponding message is subsequently removed.Warning
Be very cautious when modifying mailboxes that might be simultaneously changed by some other process. The safest mailbox format to use for such tasks is Maildir; try to avoid using single-file formats such as mbox for concurrent writing. If you’re modifying a mailbox, you must lock it by calling the
lock()andunlock()methods before reading any messages in the file or making any changes by adding or deleting a message. Failing to lock the mailbox runs the risk of losing messages or corrupting the entire mailbox.Mailboxinstances have the following methods:-
add(message)¶ Add message to the mailbox and return the key that has been assigned to it.
Parameter message may be a
Messageinstance, anemail.message.Messageinstance, a string, or a file-like object (which should be open in text mode). If message is an instance of the appropriate format-specificMessagesubclass (e.g., if it’s anmboxMessageinstance and this is anmboxinstance), its format-specific information is used. Otherwise, reasonable defaults for format-specific information are used.
-
remove(key)¶ -
__delitem__(key)¶ -
discard(key)¶ Delete the message corresponding to key from the mailbox.
If no such message exists, a
KeyErrorexception is raised if the method was called asremove()or__delitem__()but no exception is raised if the method was called asdiscard(). The behavior ofdiscard()may be preferred if the underlying mailbox format supports concurrent modification by other processes.
-
__setitem__(key, message)¶ Replace the message corresponding to key with message. Raise a
KeyErrorexception if no message already corresponds to key.As with
add(), parameter message may be aMessageinstance, anemail.message.Messageinstance, a string, or a file-like object (which should be open in text mode). If message is an instance of the appropriate format-specificMessagesubclass (e.g., if it’s anmboxMessageinstance and this is anmboxinstance), its format-specific information is used. Otherwise, the format-specific information of the message that currently corresponds to key is left unchanged.
-
iterkeys()¶ -
keys()¶ Return an iterator over all keys if called as
iterkeys()or return a list of keys if called askeys().
-
itervalues()¶ -
__iter__()¶ -
values()¶ Return an iterator over representations of all messages if called as
itervalues()or__iter__()or return a list of such representations if called asvalues(). The messages are represented as instances of the appropriate format-specificMessagesubclass unless a custom message factory was specified when theMailboxinstance was initialized.Note
The behavior of
__iter__()is unlike that of dictionaries, which iterate over keys.
-
iteritems()¶ -
items()¶ Return an iterator over (key, message) pairs, where key is a key and message is a message representation, if called as
iteritems()or return a list of such pairs if called asitems(). The messages are represented as instances of the appropriate format-specificMessagesubclass unless a custom message factory was specified when theMailboxinstance was initialized.
-
get(key, default=None)¶ -
__getitem__(key)¶ Return a representation of the message corresponding to key. If no such message exists, default is returned if the method was called as
get()and aKeyErrorexception is raised if the method was called as__getitem__(). The message is represented as an instance of the appropriate format-specificMessagesubclass unless a custom message factory was specified when theMailboxinstance was initialized.
-
get_message(key)¶ Return a representation of the message corresponding to key as an instance of the appropriate format-specific
Messagesubclass, or raise aKeyErrorexception if no such message exists.
-
get_string(key)¶ Return a string representation of the message corresponding to key, or raise a
KeyErrorexception if no such message exists.
-
get_file(key)¶ Return a file-like representation of the message corresponding to key, or raise a
KeyErrorexception if no such message exists. The file-like object behaves as if open in binary mode. This file should be closed once it is no longer needed.Note
Unlike other representations of messages, file-like representations are not necessarily independent of the
Mailboxinstance that created them or of the underlying mailbox. More specific documentation is provided by each subclass.
-
__len__()¶ Return a count of messages in the mailbox.
-
clear()¶ Delete all messages from the mailbox.
-
pop(key[, default])¶ Return a representation of the message corresponding to key and delete the message. If no such message exists, return default if it was supplied or else raise a
KeyErrorexception. The message is represented as an instance of the appropriate format-specificMessagesubclass unless a custom message factory was specified when theMailboxinstance was initialized.
-
popitem()¶ Return an arbitrary (key, message) pair, where key is a key and message is a message representation, and delete the corresponding message. If the mailbox is empty, raise a
KeyErrorexception. The message is represented as an instance of the appropriate format-specificMessagesubclass unless a custom message factory was specified when theMailboxinstance was initialized.
-
update(arg)
-
