blob: ff8cfea40376b1e7b09edee8858f6e8cb21f373c [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`mailbox` --- Manipulate mailboxes in various formats
2==========================================================
3
4.. module:: mailbox
5 :synopsis: Manipulate mailboxes in various formats
6.. moduleauthor:: Gregory K. Johnson <gkj@gregorykjohnson.com>
7.. sectionauthor:: Gregory K. Johnson <gkj@gregorykjohnson.com>
8
9
10This module defines two classes, :class:`Mailbox` and :class:`Message`, for
11accessing and manipulating on-disk mailboxes and the messages they contain.
12:class:`Mailbox` offers a dictionary-like mapping from keys to messages.
13:class:`Message` extends the :mod:`email.Message` module's :class:`Message`
14class with format-specific state and behavior. Supported mailbox formats are
15Maildir, mbox, MH, Babyl, and MMDF.
16
17
18.. seealso::
19
20 Module :mod:`email`
21 Represent and manipulate messages.
22
23
24.. _mailbox-objects:
25
26:class:`Mailbox` objects
27------------------------
28
Georg Brandl116aa622007-08-15 14:28:22 +000029.. class:: Mailbox
30
31 A mailbox, which may be inspected and modified.
32
Benjamin Petersone41251e2008-04-25 01:59:09 +000033 The :class:`Mailbox` class defines an interface and is not intended to be
34 instantiated. Instead, format-specific subclasses should inherit from
35 :class:`Mailbox` and your code should instantiate a particular subclass.
Georg Brandl116aa622007-08-15 14:28:22 +000036
Benjamin Petersone41251e2008-04-25 01:59:09 +000037 The :class:`Mailbox` interface is dictionary-like, with small keys
38 corresponding to messages. Keys are issued by the :class:`Mailbox` instance
39 with which they will be used and are only meaningful to that :class:`Mailbox`
40 instance. A key continues to identify a message even if the corresponding
41 message is modified, such as by replacing it with another message.
Georg Brandl116aa622007-08-15 14:28:22 +000042
Benjamin Petersone41251e2008-04-25 01:59:09 +000043 Messages may be added to a :class:`Mailbox` instance using the set-like
44 method :meth:`add` and removed using a ``del`` statement or the set-like
45 methods :meth:`remove` and :meth:`discard`.
Georg Brandl116aa622007-08-15 14:28:22 +000046
Benjamin Petersone41251e2008-04-25 01:59:09 +000047 :class:`Mailbox` interface semantics differ from dictionary semantics in some
48 noteworthy ways. Each time a message is requested, a new representation
49 (typically a :class:`Message` instance) is generated based upon the current
50 state of the mailbox. Similarly, when a message is added to a
51 :class:`Mailbox` instance, the provided message representation's contents are
52 copied. In neither case is a reference to the message representation kept by
53 the :class:`Mailbox` instance.
Georg Brandl116aa622007-08-15 14:28:22 +000054
Benjamin Petersone41251e2008-04-25 01:59:09 +000055 The default :class:`Mailbox` iterator iterates over message representations,
56 not keys as the default dictionary iterator does. Moreover, modification of a
57 mailbox during iteration is safe and well-defined. Messages added to the
58 mailbox after an iterator is created will not be seen by the
59 iterator. Messages removed from the mailbox before the iterator yields them
60 will be silently skipped, though using a key from an iterator may result in a
61 :exc:`KeyError` exception if the corresponding message is subsequently
62 removed.
Georg Brandl116aa622007-08-15 14:28:22 +000063
Benjamin Petersone41251e2008-04-25 01:59:09 +000064 .. warning::
Georg Brandl116aa622007-08-15 14:28:22 +000065
Benjamin Petersone41251e2008-04-25 01:59:09 +000066 Be very cautious when modifying mailboxes that might be simultaneously
67 changed by some other process. The safest mailbox format to use for such
68 tasks is Maildir; try to avoid using single-file formats such as mbox for
69 concurrent writing. If you're modifying a mailbox, you *must* lock it by
70 calling the :meth:`lock` and :meth:`unlock` methods *before* reading any
71 messages in the file or making any changes by adding or deleting a
72 message. Failing to lock the mailbox runs the risk of losing messages or
73 corrupting the entire mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +000074
Benjamin Petersone41251e2008-04-25 01:59:09 +000075 :class:`Mailbox` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000076
77
Benjamin Petersone41251e2008-04-25 01:59:09 +000078 .. method:: add(message)
Georg Brandl116aa622007-08-15 14:28:22 +000079
Benjamin Petersone41251e2008-04-25 01:59:09 +000080 Add *message* to the mailbox and return the key that has been assigned to
81 it.
Georg Brandl116aa622007-08-15 14:28:22 +000082
Benjamin Petersone41251e2008-04-25 01:59:09 +000083 Parameter *message* may be a :class:`Message` instance, an
R. David Murrayb7deff12011-01-30 06:21:28 +000084 :class:`email.Message.Message` instance, a string, a byte string, or a
85 file-like object (which should be open in binary mode). If *message* is
86 an instance of the
Benjamin Petersone41251e2008-04-25 01:59:09 +000087 appropriate format-specific :class:`Message` subclass (e.g., if it's an
88 :class:`mboxMessage` instance and this is an :class:`mbox` instance), its
89 format-specific information is used. Otherwise, reasonable defaults for
90 format-specific information are used.
Georg Brandl116aa622007-08-15 14:28:22 +000091
R. David Murrayb7deff12011-01-30 06:21:28 +000092 .. versionchanged:: 3.2 support for binary input
93
Georg Brandl116aa622007-08-15 14:28:22 +000094
Benjamin Petersone41251e2008-04-25 01:59:09 +000095 .. method:: remove(key)
96 __delitem__(key)
97 discard(key)
Georg Brandl116aa622007-08-15 14:28:22 +000098
Benjamin Petersone41251e2008-04-25 01:59:09 +000099 Delete the message corresponding to *key* from the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000100
Benjamin Petersone41251e2008-04-25 01:59:09 +0000101 If no such message exists, a :exc:`KeyError` exception is raised if the
102 method was called as :meth:`remove` or :meth:`__delitem__` but no
103 exception is raised if the method was called as :meth:`discard`. The
104 behavior of :meth:`discard` may be preferred if the underlying mailbox
105 format supports concurrent modification by other processes.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107
Benjamin Petersone41251e2008-04-25 01:59:09 +0000108 .. method:: __setitem__(key, message)
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Benjamin Petersone41251e2008-04-25 01:59:09 +0000110 Replace the message corresponding to *key* with *message*. Raise a
111 :exc:`KeyError` exception if no message already corresponds to *key*.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
Benjamin Petersone41251e2008-04-25 01:59:09 +0000113 As with :meth:`add`, parameter *message* may be a :class:`Message`
R. David Murrayb7deff12011-01-30 06:21:28 +0000114 instance, an :class:`email.Message.Message` instance, a string, a byte
115 string, or a file-like object (which should be open in binary mode). If
116 *message* is an
Benjamin Petersone41251e2008-04-25 01:59:09 +0000117 instance of the appropriate format-specific :class:`Message` subclass
118 (e.g., if it's an :class:`mboxMessage` instance and this is an
119 :class:`mbox` instance), its format-specific information is
120 used. Otherwise, the format-specific information of the message that
121 currently corresponds to *key* is left unchanged.
Georg Brandl116aa622007-08-15 14:28:22 +0000122
123
Benjamin Petersone41251e2008-04-25 01:59:09 +0000124 .. method:: iterkeys()
125 keys()
Georg Brandl116aa622007-08-15 14:28:22 +0000126
Benjamin Petersone41251e2008-04-25 01:59:09 +0000127 Return an iterator over all keys if called as :meth:`iterkeys` or return a
128 list of keys if called as :meth:`keys`.
Georg Brandl116aa622007-08-15 14:28:22 +0000129
130
Benjamin Petersone41251e2008-04-25 01:59:09 +0000131 .. method:: itervalues()
132 __iter__()
133 values()
Georg Brandl116aa622007-08-15 14:28:22 +0000134
Benjamin Petersone41251e2008-04-25 01:59:09 +0000135 Return an iterator over representations of all messages if called as
136 :meth:`itervalues` or :meth:`__iter__` or return a list of such
137 representations if called as :meth:`values`. The messages are represented
138 as instances of the appropriate format-specific :class:`Message` subclass
139 unless a custom message factory was specified when the :class:`Mailbox`
140 instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000141
Benjamin Petersone41251e2008-04-25 01:59:09 +0000142 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000143
Benjamin Petersone41251e2008-04-25 01:59:09 +0000144 The behavior of :meth:`__iter__` is unlike that of dictionaries, which
145 iterate over keys.
Georg Brandl116aa622007-08-15 14:28:22 +0000146
147
Benjamin Petersone41251e2008-04-25 01:59:09 +0000148 .. method:: iteritems()
149 items()
Georg Brandl116aa622007-08-15 14:28:22 +0000150
Benjamin Petersone41251e2008-04-25 01:59:09 +0000151 Return an iterator over (*key*, *message*) pairs, where *key* is a key and
152 *message* is a message representation, if called as :meth:`iteritems` or
153 return a list of such pairs if called as :meth:`items`. The messages are
154 represented as instances of the appropriate format-specific
155 :class:`Message` subclass unless a custom message factory was specified
156 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000157
158
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000159 .. method:: get(key, default=None)
Benjamin Petersone41251e2008-04-25 01:59:09 +0000160 __getitem__(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000161
Benjamin Petersone41251e2008-04-25 01:59:09 +0000162 Return a representation of the message corresponding to *key*. If no such
163 message exists, *default* is returned if the method was called as
164 :meth:`get` and a :exc:`KeyError` exception is raised if the method was
165 called as :meth:`__getitem__`. The message is represented as an instance
166 of the appropriate format-specific :class:`Message` subclass unless a
167 custom message factory was specified when the :class:`Mailbox` instance
168 was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000169
170
Benjamin Petersone41251e2008-04-25 01:59:09 +0000171 .. method:: get_message(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000172
Benjamin Petersone41251e2008-04-25 01:59:09 +0000173 Return a representation of the message corresponding to *key* as an
174 instance of the appropriate format-specific :class:`Message` subclass, or
175 raise a :exc:`KeyError` exception if no such message exists.
Georg Brandl116aa622007-08-15 14:28:22 +0000176
177
R. David Murrayb7deff12011-01-30 06:21:28 +0000178 .. method:: get_bytes(key)
179
180 Return a byte representation of the message corresponding to *key*, or
181 raise a :exc:`KeyError` exception if no such message exists.
182
183 .. versionadded:: 3.2
184
185
Benjamin Petersone41251e2008-04-25 01:59:09 +0000186 .. method:: get_string(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000187
Benjamin Petersone41251e2008-04-25 01:59:09 +0000188 Return a string representation of the message corresponding to *key*, or
R. David Murrayb7deff12011-01-30 06:21:28 +0000189 raise a :exc:`KeyError` exception if no such message exists. The
190 message is processed through :class:`email.message.Message` to
191 convert it to a 7bit clean representation.
Georg Brandl116aa622007-08-15 14:28:22 +0000192
193
Benjamin Petersone41251e2008-04-25 01:59:09 +0000194 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000195
Benjamin Petersone41251e2008-04-25 01:59:09 +0000196 Return a file-like representation of the message corresponding to *key*,
Georg Brandl6ce29fa2010-10-30 14:33:28 +0000197 or raise a :exc:`KeyError` exception if no such message exists. The
198 file-like object behaves as if open in binary mode. This file should be
Benjamin Petersone41251e2008-04-25 01:59:09 +0000199 closed once it is no longer needed.
Georg Brandl116aa622007-08-15 14:28:22 +0000200
R. David Murrayb7deff12011-01-30 06:21:28 +0000201 .. versionchanged:: 3.2
202 The file object really is a binary file; previously it was incorrectly
203 returned in text mode. Also, the file-like object now supports the
204 context manager protocol: you can use a :keyword:`with` statement to
205 automatically close it.
Georg Brandl6ce29fa2010-10-30 14:33:28 +0000206
Benjamin Petersone41251e2008-04-25 01:59:09 +0000207 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000208
Benjamin Petersone41251e2008-04-25 01:59:09 +0000209 Unlike other representations of messages, file-like representations are
210 not necessarily independent of the :class:`Mailbox` instance that
Georg Brandl6ce29fa2010-10-30 14:33:28 +0000211 created them or of the underlying mailbox. More specific documentation
Benjamin Petersone41251e2008-04-25 01:59:09 +0000212 is provided by each subclass.
Georg Brandl116aa622007-08-15 14:28:22 +0000213
214
Benjamin Petersone41251e2008-04-25 01:59:09 +0000215 .. method:: __contains__(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000216
Benjamin Petersone41251e2008-04-25 01:59:09 +0000217 Return ``True`` if *key* corresponds to a message, ``False`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000218
219
Benjamin Petersone41251e2008-04-25 01:59:09 +0000220 .. method:: __len__()
Georg Brandl116aa622007-08-15 14:28:22 +0000221
Benjamin Petersone41251e2008-04-25 01:59:09 +0000222 Return a count of messages in the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000223
224
Benjamin Petersone41251e2008-04-25 01:59:09 +0000225 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +0000226
Benjamin Petersone41251e2008-04-25 01:59:09 +0000227 Delete all messages from the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000230 .. method:: pop(key, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000231
Benjamin Petersone41251e2008-04-25 01:59:09 +0000232 Return a representation of the message corresponding to *key* and delete
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000233 the message. If no such message exists, return *default*. The message is
Benjamin Petersone41251e2008-04-25 01:59:09 +0000234 represented as an instance of the appropriate format-specific
235 :class:`Message` subclass unless a custom message factory was specified
236 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000237
238
Benjamin Petersone41251e2008-04-25 01:59:09 +0000239 .. method:: popitem()
Georg Brandl116aa622007-08-15 14:28:22 +0000240
Benjamin Petersone41251e2008-04-25 01:59:09 +0000241 Return an arbitrary (*key*, *message*) pair, where *key* is a key and
242 *message* is a message representation, and delete the corresponding
243 message. If the mailbox is empty, raise a :exc:`KeyError` exception. The
244 message is represented as an instance of the appropriate format-specific
245 :class:`Message` subclass unless a custom message factory was specified
246 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000247
248
Benjamin Petersone41251e2008-04-25 01:59:09 +0000249 .. method:: update(arg)
Georg Brandl116aa622007-08-15 14:28:22 +0000250
Benjamin Petersone41251e2008-04-25 01:59:09 +0000251 Parameter *arg* should be a *key*-to-*message* mapping or an iterable of
252 (*key*, *message*) pairs. Updates the mailbox so that, for each given
253 *key* and *message*, the message corresponding to *key* is set to
254 *message* as if by using :meth:`__setitem__`. As with :meth:`__setitem__`,
255 each *key* must already correspond to a message in the mailbox or else a
256 :exc:`KeyError` exception will be raised, so in general it is incorrect
257 for *arg* to be a :class:`Mailbox` instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000258
Benjamin Petersone41251e2008-04-25 01:59:09 +0000259 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000260
Benjamin Petersone41251e2008-04-25 01:59:09 +0000261 Unlike with dictionaries, keyword arguments are not supported.
Georg Brandl116aa622007-08-15 14:28:22 +0000262
263
Benjamin Petersone41251e2008-04-25 01:59:09 +0000264 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000265
Benjamin Petersone41251e2008-04-25 01:59:09 +0000266 Write any pending changes to the filesystem. For some :class:`Mailbox`
267 subclasses, changes are always written immediately and :meth:`flush` does
268 nothing, but you should still make a habit of calling this method.
Georg Brandl116aa622007-08-15 14:28:22 +0000269
270
Benjamin Petersone41251e2008-04-25 01:59:09 +0000271 .. method:: lock()
Georg Brandl116aa622007-08-15 14:28:22 +0000272
Benjamin Petersone41251e2008-04-25 01:59:09 +0000273 Acquire an exclusive advisory lock on the mailbox so that other processes
274 know not to modify it. An :exc:`ExternalClashError` is raised if the lock
275 is not available. The particular locking mechanisms used depend upon the
276 mailbox format. You should *always* lock the mailbox before making any
277 modifications to its contents.
Georg Brandl116aa622007-08-15 14:28:22 +0000278
279
Benjamin Petersone41251e2008-04-25 01:59:09 +0000280 .. method:: unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000281
Benjamin Petersone41251e2008-04-25 01:59:09 +0000282 Release the lock on the mailbox, if any.
Georg Brandl116aa622007-08-15 14:28:22 +0000283
284
Benjamin Petersone41251e2008-04-25 01:59:09 +0000285 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000286
Benjamin Petersone41251e2008-04-25 01:59:09 +0000287 Flush the mailbox, unlock it if necessary, and close any open files. For
288 some :class:`Mailbox` subclasses, this method does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000289
290
291.. _mailbox-maildir:
292
293:class:`Maildir`
294^^^^^^^^^^^^^^^^
295
296
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000297.. class:: Maildir(dirname, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000298
299 A subclass of :class:`Mailbox` for mailboxes in Maildir format. Parameter
300 *factory* is a callable object that accepts a file-like message representation
301 (which behaves as if opened in binary mode) and returns a custom representation.
302 If *factory* is ``None``, :class:`MaildirMessage` is used as the default message
303 representation. If *create* is ``True``, the mailbox is created if it does not
304 exist.
305
Georg Brandlaa5b4112008-05-11 20:51:18 +0000306 It is for historical reasons that *dirname* is named as such rather than *path*.
Georg Brandl116aa622007-08-15 14:28:22 +0000307
Benjamin Petersone41251e2008-04-25 01:59:09 +0000308 Maildir is a directory-based mailbox format invented for the qmail mail
309 transfer agent and now widely supported by other programs. Messages in a
310 Maildir mailbox are stored in separate files within a common directory
311 structure. This design allows Maildir mailboxes to be accessed and modified
312 by multiple unrelated programs without data corruption, so file locking is
313 unnecessary.
Georg Brandl116aa622007-08-15 14:28:22 +0000314
Benjamin Petersone41251e2008-04-25 01:59:09 +0000315 Maildir mailboxes contain three subdirectories, namely: :file:`tmp`,
316 :file:`new`, and :file:`cur`. Messages are created momentarily in the
317 :file:`tmp` subdirectory and then moved to the :file:`new` subdirectory to
318 finalize delivery. A mail user agent may subsequently move the message to the
319 :file:`cur` subdirectory and store information about the state of the message
320 in a special "info" section appended to its file name.
Georg Brandl116aa622007-08-15 14:28:22 +0000321
Benjamin Petersone41251e2008-04-25 01:59:09 +0000322 Folders of the style introduced by the Courier mail transfer agent are also
323 supported. Any subdirectory of the main mailbox is considered a folder if
324 ``'.'`` is the first character in its name. Folder names are represented by
325 :class:`Maildir` without the leading ``'.'``. Each folder is itself a Maildir
326 mailbox but should not contain other folders. Instead, a logical nesting is
327 indicated using ``'.'`` to delimit levels, e.g., "Archived.2005.07".
Georg Brandl116aa622007-08-15 14:28:22 +0000328
Benjamin Petersone41251e2008-04-25 01:59:09 +0000329 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000330
Benjamin Petersone41251e2008-04-25 01:59:09 +0000331 The Maildir specification requires the use of a colon (``':'``) in certain
332 message file names. However, some operating systems do not permit this
333 character in file names, If you wish to use a Maildir-like format on such
334 an operating system, you should specify another character to use
335 instead. The exclamation point (``'!'``) is a popular choice. For
336 example::
Georg Brandl116aa622007-08-15 14:28:22 +0000337
Benjamin Petersone41251e2008-04-25 01:59:09 +0000338 import mailbox
339 mailbox.Maildir.colon = '!'
Georg Brandl116aa622007-08-15 14:28:22 +0000340
Benjamin Petersone41251e2008-04-25 01:59:09 +0000341 The :attr:`colon` attribute may also be set on a per-instance basis.
Georg Brandl116aa622007-08-15 14:28:22 +0000342
Benjamin Petersone41251e2008-04-25 01:59:09 +0000343 :class:`Maildir` instances have all of the methods of :class:`Mailbox` in
344 addition to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000345
346
Benjamin Petersone41251e2008-04-25 01:59:09 +0000347 .. method:: list_folders()
Georg Brandl116aa622007-08-15 14:28:22 +0000348
Benjamin Petersone41251e2008-04-25 01:59:09 +0000349 Return a list of the names of all folders.
Georg Brandl116aa622007-08-15 14:28:22 +0000350
351
Ezio Melotti2d352d02009-09-15 18:51:37 +0000352 .. method:: get_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000353
Benjamin Petersone41251e2008-04-25 01:59:09 +0000354 Return a :class:`Maildir` instance representing the folder whose name is
355 *folder*. A :exc:`NoSuchMailboxError` exception is raised if the folder
356 does not exist.
Georg Brandl116aa622007-08-15 14:28:22 +0000357
358
Benjamin Petersone41251e2008-04-25 01:59:09 +0000359 .. method:: add_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000360
Benjamin Petersone41251e2008-04-25 01:59:09 +0000361 Create a folder whose name is *folder* and return a :class:`Maildir`
362 instance representing it.
Georg Brandl116aa622007-08-15 14:28:22 +0000363
364
Benjamin Petersone41251e2008-04-25 01:59:09 +0000365 .. method:: remove_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000366
Benjamin Petersone41251e2008-04-25 01:59:09 +0000367 Delete the folder whose name is *folder*. If the folder contains any
368 messages, a :exc:`NotEmptyError` exception will be raised and the folder
369 will not be deleted.
Georg Brandl116aa622007-08-15 14:28:22 +0000370
371
Benjamin Petersone41251e2008-04-25 01:59:09 +0000372 .. method:: clean()
Georg Brandl116aa622007-08-15 14:28:22 +0000373
Benjamin Petersone41251e2008-04-25 01:59:09 +0000374 Delete temporary files from the mailbox that have not been accessed in the
375 last 36 hours. The Maildir specification says that mail-reading programs
376 should do this occasionally.
Georg Brandl116aa622007-08-15 14:28:22 +0000377
Benjamin Petersone41251e2008-04-25 01:59:09 +0000378 Some :class:`Mailbox` methods implemented by :class:`Maildir` deserve special
379 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000380
381
Benjamin Petersone41251e2008-04-25 01:59:09 +0000382 .. method:: add(message)
383 __setitem__(key, message)
384 update(arg)
Georg Brandl116aa622007-08-15 14:28:22 +0000385
Benjamin Petersone41251e2008-04-25 01:59:09 +0000386 .. warning::
Georg Brandl116aa622007-08-15 14:28:22 +0000387
Benjamin Petersone41251e2008-04-25 01:59:09 +0000388 These methods generate unique file names based upon the current process
389 ID. When using multiple threads, undetected name clashes may occur and
390 cause corruption of the mailbox unless threads are coordinated to avoid
391 using these methods to manipulate the same mailbox simultaneously.
Georg Brandl116aa622007-08-15 14:28:22 +0000392
393
Benjamin Petersone41251e2008-04-25 01:59:09 +0000394 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000395
Benjamin Petersone41251e2008-04-25 01:59:09 +0000396 All changes to Maildir mailboxes are immediately applied, so this method
397 does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000398
399
Benjamin Petersone41251e2008-04-25 01:59:09 +0000400 .. method:: lock()
401 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000402
Benjamin Petersone41251e2008-04-25 01:59:09 +0000403 Maildir mailboxes do not support (or require) locking, so these methods do
404 nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000405
406
Benjamin Petersone41251e2008-04-25 01:59:09 +0000407 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000408
Benjamin Petersone41251e2008-04-25 01:59:09 +0000409 :class:`Maildir` instances do not keep any open files and the underlying
410 mailboxes do not support locking, so this method does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000411
412
Benjamin Petersone41251e2008-04-25 01:59:09 +0000413 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000414
Benjamin Petersone41251e2008-04-25 01:59:09 +0000415 Depending upon the host platform, it may not be possible to modify or
416 remove the underlying message while the returned file remains open.
Georg Brandl116aa622007-08-15 14:28:22 +0000417
418
419.. seealso::
420
421 `maildir man page from qmail <http://www.qmail.org/man/man5/maildir.html>`_
422 The original specification of the format.
423
424 `Using maildir format <http://cr.yp.to/proto/maildir.html>`_
425 Notes on Maildir by its inventor. Includes an updated name-creation scheme and
426 details on "info" semantics.
427
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000428 `maildir man page from Courier <http://www.courier-mta.org/maildir.html>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000429 Another specification of the format. Describes a common extension for supporting
430 folders.
431
432
433.. _mailbox-mbox:
434
435:class:`mbox`
436^^^^^^^^^^^^^
437
438
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000439.. class:: mbox(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000440
441 A subclass of :class:`Mailbox` for mailboxes in mbox format. Parameter *factory*
442 is a callable object that accepts a file-like message representation (which
443 behaves as if opened in binary mode) and returns a custom representation. If
444 *factory* is ``None``, :class:`mboxMessage` is used as the default message
445 representation. If *create* is ``True``, the mailbox is created if it does not
446 exist.
447
Benjamin Petersone41251e2008-04-25 01:59:09 +0000448 The mbox format is the classic format for storing mail on Unix systems. All
449 messages in an mbox mailbox are stored in a single file with the beginning of
450 each message indicated by a line whose first five characters are "From ".
Georg Brandl116aa622007-08-15 14:28:22 +0000451
Benjamin Petersone41251e2008-04-25 01:59:09 +0000452 Several variations of the mbox format exist to address perceived shortcomings in
453 the original. In the interest of compatibility, :class:`mbox` implements the
454 original format, which is sometimes referred to as :dfn:`mboxo`. This means that
455 the :mailheader:`Content-Length` header, if present, is ignored and that any
456 occurrences of "From " at the beginning of a line in a message body are
457 transformed to ">From " when storing the message, although occurrences of ">From
458 " are not transformed to "From " when reading the message.
Georg Brandl116aa622007-08-15 14:28:22 +0000459
Benjamin Petersone41251e2008-04-25 01:59:09 +0000460 Some :class:`Mailbox` methods implemented by :class:`mbox` deserve special
461 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000462
463
Benjamin Petersone41251e2008-04-25 01:59:09 +0000464 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000465
Benjamin Petersone41251e2008-04-25 01:59:09 +0000466 Using the file after calling :meth:`flush` or :meth:`close` on the
467 :class:`mbox` instance may yield unpredictable results or raise an
468 exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000469
470
Benjamin Petersone41251e2008-04-25 01:59:09 +0000471 .. method:: lock()
472 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000473
Benjamin Petersone41251e2008-04-25 01:59:09 +0000474 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000475 :c:func:`flock` and :c:func:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000476
477
478.. seealso::
479
480 `mbox man page from qmail <http://www.qmail.org/man/man5/mbox.html>`_
481 A specification of the format and its variations.
482
483 `mbox man page from tin <http://www.tin.org/bin/man.cgi?section=5&topic=mbox>`_
484 Another specification of the format, with details on locking.
485
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000486 `Configuring Netscape Mail on Unix: Why The Content-Length Format is Bad <http://www.jwz.org/doc/content-length.html>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000487 An argument for using the original mbox format rather than a variation.
488
489 `"mbox" is a family of several mutually incompatible mailbox formats <http://homepages.tesco.net./~J.deBoynePollard/FGA/mail-mbox-formats.html>`_
490 A history of mbox variations.
491
492
493.. _mailbox-mh:
494
495:class:`MH`
496^^^^^^^^^^^
497
498
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000499.. class:: MH(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000500
501 A subclass of :class:`Mailbox` for mailboxes in MH format. Parameter *factory*
502 is a callable object that accepts a file-like message representation (which
503 behaves as if opened in binary mode) and returns a custom representation. If
504 *factory* is ``None``, :class:`MHMessage` is used as the default message
505 representation. If *create* is ``True``, the mailbox is created if it does not
506 exist.
507
Benjamin Petersone41251e2008-04-25 01:59:09 +0000508 MH is a directory-based mailbox format invented for the MH Message Handling
509 System, a mail user agent. Each message in an MH mailbox resides in its own
510 file. An MH mailbox may contain other MH mailboxes (called :dfn:`folders`) in
511 addition to messages. Folders may be nested indefinitely. MH mailboxes also
512 support :dfn:`sequences`, which are named lists used to logically group
513 messages without moving them to sub-folders. Sequences are defined in a file
514 called :file:`.mh_sequences` in each folder.
Georg Brandl116aa622007-08-15 14:28:22 +0000515
Benjamin Petersone41251e2008-04-25 01:59:09 +0000516 The :class:`MH` class manipulates MH mailboxes, but it does not attempt to
517 emulate all of :program:`mh`'s behaviors. In particular, it does not modify
518 and is not affected by the :file:`context` or :file:`.mh_profile` files that
519 are used by :program:`mh` to store its state and configuration.
Georg Brandl116aa622007-08-15 14:28:22 +0000520
Benjamin Petersone41251e2008-04-25 01:59:09 +0000521 :class:`MH` instances have all of the methods of :class:`Mailbox` in addition
522 to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000523
524
Benjamin Petersone41251e2008-04-25 01:59:09 +0000525 .. method:: list_folders()
Georg Brandl116aa622007-08-15 14:28:22 +0000526
Benjamin Petersone41251e2008-04-25 01:59:09 +0000527 Return a list of the names of all folders.
Georg Brandl116aa622007-08-15 14:28:22 +0000528
529
Benjamin Petersone41251e2008-04-25 01:59:09 +0000530 .. method:: get_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000531
Benjamin Petersone41251e2008-04-25 01:59:09 +0000532 Return an :class:`MH` instance representing the folder whose name is
533 *folder*. A :exc:`NoSuchMailboxError` exception is raised if the folder
534 does not exist.
Georg Brandl116aa622007-08-15 14:28:22 +0000535
536
Benjamin Petersone41251e2008-04-25 01:59:09 +0000537 .. method:: add_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000538
Benjamin Petersone41251e2008-04-25 01:59:09 +0000539 Create a folder whose name is *folder* and return an :class:`MH` instance
540 representing it.
Georg Brandl116aa622007-08-15 14:28:22 +0000541
542
Benjamin Petersone41251e2008-04-25 01:59:09 +0000543 .. method:: remove_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000544
Benjamin Petersone41251e2008-04-25 01:59:09 +0000545 Delete the folder whose name is *folder*. If the folder contains any
546 messages, a :exc:`NotEmptyError` exception will be raised and the folder
547 will not be deleted.
Georg Brandl116aa622007-08-15 14:28:22 +0000548
549
Benjamin Petersone41251e2008-04-25 01:59:09 +0000550 .. method:: get_sequences()
Georg Brandl116aa622007-08-15 14:28:22 +0000551
Benjamin Petersone41251e2008-04-25 01:59:09 +0000552 Return a dictionary of sequence names mapped to key lists. If there are no
553 sequences, the empty dictionary is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000554
555
Benjamin Petersone41251e2008-04-25 01:59:09 +0000556 .. method:: set_sequences(sequences)
Georg Brandl116aa622007-08-15 14:28:22 +0000557
Benjamin Petersone41251e2008-04-25 01:59:09 +0000558 Re-define the sequences that exist in the mailbox based upon *sequences*,
559 a dictionary of names mapped to key lists, like returned by
560 :meth:`get_sequences`.
Georg Brandl116aa622007-08-15 14:28:22 +0000561
562
Benjamin Petersone41251e2008-04-25 01:59:09 +0000563 .. method:: pack()
Georg Brandl116aa622007-08-15 14:28:22 +0000564
Benjamin Petersone41251e2008-04-25 01:59:09 +0000565 Rename messages in the mailbox as necessary to eliminate gaps in
566 numbering. Entries in the sequences list are updated correspondingly.
Georg Brandl116aa622007-08-15 14:28:22 +0000567
Benjamin Petersone41251e2008-04-25 01:59:09 +0000568 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000569
Benjamin Petersone41251e2008-04-25 01:59:09 +0000570 Already-issued keys are invalidated by this operation and should not be
571 subsequently used.
Georg Brandl116aa622007-08-15 14:28:22 +0000572
Benjamin Petersone41251e2008-04-25 01:59:09 +0000573 Some :class:`Mailbox` methods implemented by :class:`MH` deserve special
574 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000575
576
Benjamin Petersone41251e2008-04-25 01:59:09 +0000577 .. method:: remove(key)
578 __delitem__(key)
579 discard(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000580
Benjamin Petersone41251e2008-04-25 01:59:09 +0000581 These methods immediately delete the message. The MH convention of marking
582 a message for deletion by prepending a comma to its name is not used.
Georg Brandl116aa622007-08-15 14:28:22 +0000583
584
Benjamin Petersone41251e2008-04-25 01:59:09 +0000585 .. method:: lock()
586 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000587
Benjamin Petersone41251e2008-04-25 01:59:09 +0000588 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000589 :c:func:`flock` and :c:func:`lockf` system calls. For MH mailboxes, locking
Benjamin Petersone41251e2008-04-25 01:59:09 +0000590 the mailbox means locking the :file:`.mh_sequences` file and, only for the
591 duration of any operations that affect them, locking individual message
592 files.
Georg Brandl116aa622007-08-15 14:28:22 +0000593
594
Benjamin Petersone41251e2008-04-25 01:59:09 +0000595 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000596
Benjamin Petersone41251e2008-04-25 01:59:09 +0000597 Depending upon the host platform, it may not be possible to remove the
598 underlying message while the returned file remains open.
Georg Brandl116aa622007-08-15 14:28:22 +0000599
600
Benjamin Petersone41251e2008-04-25 01:59:09 +0000601 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000602
Benjamin Petersone41251e2008-04-25 01:59:09 +0000603 All changes to MH mailboxes are immediately applied, so this method does
604 nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000605
606
Benjamin Petersone41251e2008-04-25 01:59:09 +0000607 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000608
Benjamin Petersone41251e2008-04-25 01:59:09 +0000609 :class:`MH` instances do not keep any open files, so this method is
610 equivalent to :meth:`unlock`.
Georg Brandl116aa622007-08-15 14:28:22 +0000611
612
613.. seealso::
614
615 `nmh - Message Handling System <http://www.nongnu.org/nmh/>`_
616 Home page of :program:`nmh`, an updated version of the original :program:`mh`.
617
Georg Brandl495f7b52009-10-27 15:28:25 +0000618 `MH & nmh: Email for Users & Programmers <http://rand-mh.sourceforge.net/book/>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000619 A GPL-licensed book on :program:`mh` and :program:`nmh`, with some information
620 on the mailbox format.
621
622
623.. _mailbox-babyl:
624
625:class:`Babyl`
626^^^^^^^^^^^^^^
627
628
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000629.. class:: Babyl(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000630
631 A subclass of :class:`Mailbox` for mailboxes in Babyl format. Parameter
632 *factory* is a callable object that accepts a file-like message representation
633 (which behaves as if opened in binary mode) and returns a custom representation.
634 If *factory* is ``None``, :class:`BabylMessage` is used as the default message
635 representation. If *create* is ``True``, the mailbox is created if it does not
636 exist.
637
Benjamin Petersone41251e2008-04-25 01:59:09 +0000638 Babyl is a single-file mailbox format used by the Rmail mail user agent
639 included with Emacs. The beginning of a message is indicated by a line
640 containing the two characters Control-Underscore (``'\037'``) and Control-L
641 (``'\014'``). The end of a message is indicated by the start of the next
642 message or, in the case of the last message, a line containing a
643 Control-Underscore (``'\037'``) character.
Georg Brandl116aa622007-08-15 14:28:22 +0000644
Benjamin Petersone41251e2008-04-25 01:59:09 +0000645 Messages in a Babyl mailbox have two sets of headers, original headers and
646 so-called visible headers. Visible headers are typically a subset of the
647 original headers that have been reformatted or abridged to be more
648 attractive. Each message in a Babyl mailbox also has an accompanying list of
649 :dfn:`labels`, or short strings that record extra information about the
650 message, and a list of all user-defined labels found in the mailbox is kept
651 in the Babyl options section.
Georg Brandl116aa622007-08-15 14:28:22 +0000652
Benjamin Petersone41251e2008-04-25 01:59:09 +0000653 :class:`Babyl` instances have all of the methods of :class:`Mailbox` in
654 addition to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000655
656
Benjamin Petersone41251e2008-04-25 01:59:09 +0000657 .. method:: get_labels()
Georg Brandl116aa622007-08-15 14:28:22 +0000658
Benjamin Petersone41251e2008-04-25 01:59:09 +0000659 Return a list of the names of all user-defined labels used in the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000660
Benjamin Petersone41251e2008-04-25 01:59:09 +0000661 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000662
Benjamin Petersone41251e2008-04-25 01:59:09 +0000663 The actual messages are inspected to determine which labels exist in
664 the mailbox rather than consulting the list of labels in the Babyl
665 options section, but the Babyl section is updated whenever the mailbox
666 is modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000667
Benjamin Petersone41251e2008-04-25 01:59:09 +0000668 Some :class:`Mailbox` methods implemented by :class:`Babyl` deserve special
669 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000670
671
Benjamin Petersone41251e2008-04-25 01:59:09 +0000672 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000673
Benjamin Petersone41251e2008-04-25 01:59:09 +0000674 In Babyl mailboxes, the headers of a message are not stored contiguously
675 with the body of the message. To generate a file-like representation, the
676 headers and body are copied together into a :class:`StringIO` instance
677 (from the :mod:`StringIO` module), which has an API identical to that of a
678 file. As a result, the file-like object is truly independent of the
679 underlying mailbox but does not save memory compared to a string
680 representation.
Georg Brandl116aa622007-08-15 14:28:22 +0000681
682
Benjamin Petersone41251e2008-04-25 01:59:09 +0000683 .. method:: lock()
684 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000685
Benjamin Petersone41251e2008-04-25 01:59:09 +0000686 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000687 :c:func:`flock` and :c:func:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000688
689
690.. seealso::
691
692 `Format of Version 5 Babyl Files <http://quimby.gnus.org/notes/BABYL>`_
693 A specification of the Babyl format.
694
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000695 `Reading Mail with Rmail <http://www.gnu.org/software/emacs/manual/html_node/emacs/Rmail.html>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000696 The Rmail manual, with some information on Babyl semantics.
697
698
699.. _mailbox-mmdf:
700
701:class:`MMDF`
702^^^^^^^^^^^^^
703
704
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000705.. class:: MMDF(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000706
707 A subclass of :class:`Mailbox` for mailboxes in MMDF format. Parameter *factory*
708 is a callable object that accepts a file-like message representation (which
709 behaves as if opened in binary mode) and returns a custom representation. If
710 *factory* is ``None``, :class:`MMDFMessage` is used as the default message
711 representation. If *create* is ``True``, the mailbox is created if it does not
712 exist.
713
Benjamin Petersone41251e2008-04-25 01:59:09 +0000714 MMDF is a single-file mailbox format invented for the Multichannel Memorandum
715 Distribution Facility, a mail transfer agent. Each message is in the same
716 form as an mbox message but is bracketed before and after by lines containing
717 four Control-A (``'\001'``) characters. As with the mbox format, the
718 beginning of each message is indicated by a line whose first five characters
719 are "From ", but additional occurrences of "From " are not transformed to
720 ">From " when storing messages because the extra message separator lines
721 prevent mistaking such occurrences for the starts of subsequent messages.
Georg Brandl116aa622007-08-15 14:28:22 +0000722
Benjamin Petersone41251e2008-04-25 01:59:09 +0000723 Some :class:`Mailbox` methods implemented by :class:`MMDF` deserve special
724 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000725
726
Benjamin Petersone41251e2008-04-25 01:59:09 +0000727 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000728
Benjamin Petersone41251e2008-04-25 01:59:09 +0000729 Using the file after calling :meth:`flush` or :meth:`close` on the
730 :class:`MMDF` instance may yield unpredictable results or raise an
731 exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000732
733
Benjamin Petersone41251e2008-04-25 01:59:09 +0000734 .. method:: lock()
735 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000736
Benjamin Petersone41251e2008-04-25 01:59:09 +0000737 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000738 :c:func:`flock` and :c:func:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000739
740
741.. seealso::
742
743 `mmdf man page from tin <http://www.tin.org/bin/man.cgi?section=5&topic=mmdf>`_
744 A specification of MMDF format from the documentation of tin, a newsreader.
745
746 `MMDF <http://en.wikipedia.org/wiki/MMDF>`_
747 A Wikipedia article describing the Multichannel Memorandum Distribution
748 Facility.
749
750
751.. _mailbox-message-objects:
752
753:class:`Message` objects
754------------------------
755
756
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000757.. class:: Message(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000758
759 A subclass of the :mod:`email.Message` module's :class:`Message`. Subclasses of
760 :class:`mailbox.Message` add mailbox-format-specific state and behavior.
761
762 If *message* is omitted, the new instance is created in a default, empty state.
763 If *message* is an :class:`email.Message.Message` instance, its contents are
764 copied; furthermore, any format-specific information is converted insofar as
R. David Murrayb7deff12011-01-30 06:21:28 +0000765 possible if *message* is a :class:`Message` instance. If *message* is a string,
766 a byte string,
Georg Brandl116aa622007-08-15 14:28:22 +0000767 or a file, it should contain an :rfc:`2822`\ -compliant message, which is read
R. David Murrayb7deff12011-01-30 06:21:28 +0000768 and parsed. Files should be open in binary mode, but text mode files
769 are accepted for backward compatibility.
Georg Brandl116aa622007-08-15 14:28:22 +0000770
Benjamin Petersone41251e2008-04-25 01:59:09 +0000771 The format-specific state and behaviors offered by subclasses vary, but in
772 general it is only the properties that are not specific to a particular
773 mailbox that are supported (although presumably the properties are specific
774 to a particular mailbox format). For example, file offsets for single-file
775 mailbox formats and file names for directory-based mailbox formats are not
776 retained, because they are only applicable to the original mailbox. But state
777 such as whether a message has been read by the user or marked as important is
778 retained, because it applies to the message itself.
Georg Brandl116aa622007-08-15 14:28:22 +0000779
Benjamin Petersone41251e2008-04-25 01:59:09 +0000780 There is no requirement that :class:`Message` instances be used to represent
781 messages retrieved using :class:`Mailbox` instances. In some situations, the
782 time and memory required to generate :class:`Message` representations might
783 not not acceptable. For such situations, :class:`Mailbox` instances also
784 offer string and file-like representations, and a custom message factory may
785 be specified when a :class:`Mailbox` instance is initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000786
787
788.. _mailbox-maildirmessage:
789
790:class:`MaildirMessage`
791^^^^^^^^^^^^^^^^^^^^^^^
792
793
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000794.. class:: MaildirMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000795
796 A message with Maildir-specific behaviors. Parameter *message* has the same
797 meaning as with the :class:`Message` constructor.
798
Benjamin Petersone41251e2008-04-25 01:59:09 +0000799 Typically, a mail user agent application moves all of the messages in the
800 :file:`new` subdirectory to the :file:`cur` subdirectory after the first time
801 the user opens and closes the mailbox, recording that the messages are old
802 whether or not they've actually been read. Each message in :file:`cur` has an
803 "info" section added to its file name to store information about its state.
804 (Some mail readers may also add an "info" section to messages in
805 :file:`new`.) The "info" section may take one of two forms: it may contain
806 "2," followed by a list of standardized flags (e.g., "2,FR") or it may
807 contain "1," followed by so-called experimental information. Standard flags
808 for Maildir messages are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000809
Benjamin Petersone41251e2008-04-25 01:59:09 +0000810 +------+---------+--------------------------------+
811 | Flag | Meaning | Explanation |
812 +======+=========+================================+
813 | D | Draft | Under composition |
814 +------+---------+--------------------------------+
815 | F | Flagged | Marked as important |
816 +------+---------+--------------------------------+
817 | P | Passed | Forwarded, resent, or bounced |
818 +------+---------+--------------------------------+
819 | R | Replied | Replied to |
820 +------+---------+--------------------------------+
821 | S | Seen | Read |
822 +------+---------+--------------------------------+
823 | T | Trashed | Marked for subsequent deletion |
824 +------+---------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000825
Benjamin Petersone41251e2008-04-25 01:59:09 +0000826 :class:`MaildirMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000827
828
Benjamin Petersone41251e2008-04-25 01:59:09 +0000829 .. method:: get_subdir()
Georg Brandl116aa622007-08-15 14:28:22 +0000830
Benjamin Petersone41251e2008-04-25 01:59:09 +0000831 Return either "new" (if the message should be stored in the :file:`new`
832 subdirectory) or "cur" (if the message should be stored in the :file:`cur`
833 subdirectory).
Georg Brandl116aa622007-08-15 14:28:22 +0000834
Benjamin Petersone41251e2008-04-25 01:59:09 +0000835 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000836
Benjamin Petersone41251e2008-04-25 01:59:09 +0000837 A message is typically moved from :file:`new` to :file:`cur` after its
838 mailbox has been accessed, whether or not the message is has been
839 read. A message ``msg`` has been read if ``"S" in msg.get_flags()`` is
840 ``True``.
Georg Brandl116aa622007-08-15 14:28:22 +0000841
842
Benjamin Petersone41251e2008-04-25 01:59:09 +0000843 .. method:: set_subdir(subdir)
Georg Brandl116aa622007-08-15 14:28:22 +0000844
Benjamin Petersone41251e2008-04-25 01:59:09 +0000845 Set the subdirectory the message should be stored in. Parameter *subdir*
846 must be either "new" or "cur".
Georg Brandl116aa622007-08-15 14:28:22 +0000847
848
Benjamin Petersone41251e2008-04-25 01:59:09 +0000849 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +0000850
Benjamin Petersone41251e2008-04-25 01:59:09 +0000851 Return a string specifying the flags that are currently set. If the
852 message complies with the standard Maildir format, the result is the
853 concatenation in alphabetical order of zero or one occurrence of each of
854 ``'D'``, ``'F'``, ``'P'``, ``'R'``, ``'S'``, and ``'T'``. The empty string
855 is returned if no flags are set or if "info" contains experimental
856 semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000857
858
Benjamin Petersone41251e2008-04-25 01:59:09 +0000859 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +0000860
Benjamin Petersone41251e2008-04-25 01:59:09 +0000861 Set the flags specified by *flags* and unset all others.
Georg Brandl116aa622007-08-15 14:28:22 +0000862
863
Benjamin Petersone41251e2008-04-25 01:59:09 +0000864 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000865
Benjamin Petersone41251e2008-04-25 01:59:09 +0000866 Set the flag(s) specified by *flag* without changing other flags. To add
867 more than one flag at a time, *flag* may be a string of more than one
868 character. The current "info" is overwritten whether or not it contains
869 experimental information rather than flags.
Georg Brandl116aa622007-08-15 14:28:22 +0000870
871
Benjamin Petersone41251e2008-04-25 01:59:09 +0000872 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000873
Benjamin Petersone41251e2008-04-25 01:59:09 +0000874 Unset the flag(s) specified by *flag* without changing other flags. To
875 remove more than one flag at a time, *flag* maybe a string of more than
876 one character. If "info" contains experimental information rather than
877 flags, the current "info" is not modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000878
879
Benjamin Petersone41251e2008-04-25 01:59:09 +0000880 .. method:: get_date()
Georg Brandl116aa622007-08-15 14:28:22 +0000881
Benjamin Petersone41251e2008-04-25 01:59:09 +0000882 Return the delivery date of the message as a floating-point number
883 representing seconds since the epoch.
Georg Brandl116aa622007-08-15 14:28:22 +0000884
885
Benjamin Petersone41251e2008-04-25 01:59:09 +0000886 .. method:: set_date(date)
Georg Brandl116aa622007-08-15 14:28:22 +0000887
Benjamin Petersone41251e2008-04-25 01:59:09 +0000888 Set the delivery date of the message to *date*, a floating-point number
889 representing seconds since the epoch.
Georg Brandl116aa622007-08-15 14:28:22 +0000890
891
Benjamin Petersone41251e2008-04-25 01:59:09 +0000892 .. method:: get_info()
Georg Brandl116aa622007-08-15 14:28:22 +0000893
Benjamin Petersone41251e2008-04-25 01:59:09 +0000894 Return a string containing the "info" for a message. This is useful for
895 accessing and modifying "info" that is experimental (i.e., not a list of
896 flags).
Georg Brandl116aa622007-08-15 14:28:22 +0000897
898
Benjamin Petersone41251e2008-04-25 01:59:09 +0000899 .. method:: set_info(info)
Georg Brandl116aa622007-08-15 14:28:22 +0000900
Benjamin Petersone41251e2008-04-25 01:59:09 +0000901 Set "info" to *info*, which should be a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000902
903When a :class:`MaildirMessage` instance is created based upon an
904:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
905and :mailheader:`X-Status` headers are omitted and the following conversions
906take place:
907
908+--------------------+----------------------------------------------+
909| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
910| | state |
911+====================+==============================================+
912| "cur" subdirectory | O flag |
913+--------------------+----------------------------------------------+
914| F flag | F flag |
915+--------------------+----------------------------------------------+
916| R flag | A flag |
917+--------------------+----------------------------------------------+
918| S flag | R flag |
919+--------------------+----------------------------------------------+
920| T flag | D flag |
921+--------------------+----------------------------------------------+
922
923When a :class:`MaildirMessage` instance is created based upon an
924:class:`MHMessage` instance, the following conversions take place:
925
926+-------------------------------+--------------------------+
927| Resulting state | :class:`MHMessage` state |
928+===============================+==========================+
929| "cur" subdirectory | "unseen" sequence |
930+-------------------------------+--------------------------+
931| "cur" subdirectory and S flag | no "unseen" sequence |
932+-------------------------------+--------------------------+
933| F flag | "flagged" sequence |
934+-------------------------------+--------------------------+
935| R flag | "replied" sequence |
936+-------------------------------+--------------------------+
937
938When a :class:`MaildirMessage` instance is created based upon a
939:class:`BabylMessage` instance, the following conversions take place:
940
941+-------------------------------+-------------------------------+
942| Resulting state | :class:`BabylMessage` state |
943+===============================+===============================+
944| "cur" subdirectory | "unseen" label |
945+-------------------------------+-------------------------------+
946| "cur" subdirectory and S flag | no "unseen" label |
947+-------------------------------+-------------------------------+
948| P flag | "forwarded" or "resent" label |
949+-------------------------------+-------------------------------+
950| R flag | "answered" label |
951+-------------------------------+-------------------------------+
952| T flag | "deleted" label |
953+-------------------------------+-------------------------------+
954
955
956.. _mailbox-mboxmessage:
957
958:class:`mboxMessage`
959^^^^^^^^^^^^^^^^^^^^
960
961
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000962.. class:: mboxMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000963
964 A message with mbox-specific behaviors. Parameter *message* has the same meaning
965 as with the :class:`Message` constructor.
966
Benjamin Petersone41251e2008-04-25 01:59:09 +0000967 Messages in an mbox mailbox are stored together in a single file. The
968 sender's envelope address and the time of delivery are typically stored in a
969 line beginning with "From " that is used to indicate the start of a message,
970 though there is considerable variation in the exact format of this data among
971 mbox implementations. Flags that indicate the state of the message, such as
972 whether it has been read or marked as important, are typically stored in
973 :mailheader:`Status` and :mailheader:`X-Status` headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000974
Benjamin Petersone41251e2008-04-25 01:59:09 +0000975 Conventional flags for mbox messages are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000976
Benjamin Petersone41251e2008-04-25 01:59:09 +0000977 +------+----------+--------------------------------+
978 | Flag | Meaning | Explanation |
979 +======+==========+================================+
980 | R | Read | Read |
981 +------+----------+--------------------------------+
982 | O | Old | Previously detected by MUA |
983 +------+----------+--------------------------------+
984 | D | Deleted | Marked for subsequent deletion |
985 +------+----------+--------------------------------+
986 | F | Flagged | Marked as important |
987 +------+----------+--------------------------------+
988 | A | Answered | Replied to |
989 +------+----------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000990
Benjamin Petersone41251e2008-04-25 01:59:09 +0000991 The "R" and "O" flags are stored in the :mailheader:`Status` header, and the
992 "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The
993 flags and headers typically appear in the order mentioned.
Georg Brandl116aa622007-08-15 14:28:22 +0000994
Benjamin Petersone41251e2008-04-25 01:59:09 +0000995 :class:`mboxMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000996
997
Benjamin Petersone41251e2008-04-25 01:59:09 +0000998 .. method:: get_from()
Georg Brandl116aa622007-08-15 14:28:22 +0000999
Benjamin Petersone41251e2008-04-25 01:59:09 +00001000 Return a string representing the "From " line that marks the start of the
1001 message in an mbox mailbox. The leading "From " and the trailing newline
1002 are excluded.
Georg Brandl116aa622007-08-15 14:28:22 +00001003
1004
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001005 .. method:: set_from(from_, time_=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001006
Benjamin Petersone41251e2008-04-25 01:59:09 +00001007 Set the "From " line to *from_*, which should be specified without a
1008 leading "From " or trailing newline. For convenience, *time_* may be
1009 specified and will be formatted appropriately and appended to *from_*. If
1010 *time_* is specified, it should be a :class:`struct_time` instance, a
1011 tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use
1012 :meth:`time.gmtime`).
Georg Brandl116aa622007-08-15 14:28:22 +00001013
1014
Benjamin Petersone41251e2008-04-25 01:59:09 +00001015 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +00001016
Benjamin Petersone41251e2008-04-25 01:59:09 +00001017 Return a string specifying the flags that are currently set. If the
1018 message complies with the conventional format, the result is the
1019 concatenation in the following order of zero or one occurrence of each of
1020 ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001021
1022
Benjamin Petersone41251e2008-04-25 01:59:09 +00001023 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +00001024
Benjamin Petersone41251e2008-04-25 01:59:09 +00001025 Set the flags specified by *flags* and unset all others. Parameter *flags*
1026 should be the concatenation in any order of zero or more occurrences of
1027 each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001028
1029
Benjamin Petersone41251e2008-04-25 01:59:09 +00001030 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001031
Benjamin Petersone41251e2008-04-25 01:59:09 +00001032 Set the flag(s) specified by *flag* without changing other flags. To add
1033 more than one flag at a time, *flag* may be a string of more than one
1034 character.
Georg Brandl116aa622007-08-15 14:28:22 +00001035
1036
Benjamin Petersone41251e2008-04-25 01:59:09 +00001037 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001038
Benjamin Petersone41251e2008-04-25 01:59:09 +00001039 Unset the flag(s) specified by *flag* without changing other flags. To
1040 remove more than one flag at a time, *flag* maybe a string of more than
1041 one character.
Georg Brandl116aa622007-08-15 14:28:22 +00001042
1043When an :class:`mboxMessage` instance is created based upon a
1044:class:`MaildirMessage` instance, a "From " line is generated based upon the
1045:class:`MaildirMessage` instance's delivery date, and the following conversions
1046take place:
1047
1048+-----------------+-------------------------------+
1049| Resulting state | :class:`MaildirMessage` state |
1050+=================+===============================+
1051| R flag | S flag |
1052+-----------------+-------------------------------+
1053| O flag | "cur" subdirectory |
1054+-----------------+-------------------------------+
1055| D flag | T flag |
1056+-----------------+-------------------------------+
1057| F flag | F flag |
1058+-----------------+-------------------------------+
1059| A flag | R flag |
1060+-----------------+-------------------------------+
1061
1062When an :class:`mboxMessage` instance is created based upon an
1063:class:`MHMessage` instance, the following conversions take place:
1064
1065+-------------------+--------------------------+
1066| Resulting state | :class:`MHMessage` state |
1067+===================+==========================+
1068| R flag and O flag | no "unseen" sequence |
1069+-------------------+--------------------------+
1070| O flag | "unseen" sequence |
1071+-------------------+--------------------------+
1072| F flag | "flagged" sequence |
1073+-------------------+--------------------------+
1074| A flag | "replied" sequence |
1075+-------------------+--------------------------+
1076
1077When an :class:`mboxMessage` instance is created based upon a
1078:class:`BabylMessage` instance, the following conversions take place:
1079
1080+-------------------+-----------------------------+
1081| Resulting state | :class:`BabylMessage` state |
1082+===================+=============================+
1083| R flag and O flag | no "unseen" label |
1084+-------------------+-----------------------------+
1085| O flag | "unseen" label |
1086+-------------------+-----------------------------+
1087| D flag | "deleted" label |
1088+-------------------+-----------------------------+
1089| A flag | "answered" label |
1090+-------------------+-----------------------------+
1091
1092When a :class:`Message` instance is created based upon an :class:`MMDFMessage`
1093instance, the "From " line is copied and all flags directly correspond:
1094
1095+-----------------+----------------------------+
1096| Resulting state | :class:`MMDFMessage` state |
1097+=================+============================+
1098| R flag | R flag |
1099+-----------------+----------------------------+
1100| O flag | O flag |
1101+-----------------+----------------------------+
1102| D flag | D flag |
1103+-----------------+----------------------------+
1104| F flag | F flag |
1105+-----------------+----------------------------+
1106| A flag | A flag |
1107+-----------------+----------------------------+
1108
1109
1110.. _mailbox-mhmessage:
1111
1112:class:`MHMessage`
1113^^^^^^^^^^^^^^^^^^
1114
1115
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001116.. class:: MHMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001117
1118 A message with MH-specific behaviors. Parameter *message* has the same meaning
1119 as with the :class:`Message` constructor.
1120
Benjamin Petersone41251e2008-04-25 01:59:09 +00001121 MH messages do not support marks or flags in the traditional sense, but they
1122 do support sequences, which are logical groupings of arbitrary messages. Some
1123 mail reading programs (although not the standard :program:`mh` and
1124 :program:`nmh`) use sequences in much the same way flags are used with other
1125 formats, as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001126
Benjamin Petersone41251e2008-04-25 01:59:09 +00001127 +----------+------------------------------------------+
1128 | Sequence | Explanation |
1129 +==========+==========================================+
1130 | unseen | Not read, but previously detected by MUA |
1131 +----------+------------------------------------------+
1132 | replied | Replied to |
1133 +----------+------------------------------------------+
1134 | flagged | Marked as important |
1135 +----------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001136
Benjamin Petersone41251e2008-04-25 01:59:09 +00001137 :class:`MHMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001138
1139
Benjamin Petersone41251e2008-04-25 01:59:09 +00001140 .. method:: get_sequences()
Georg Brandl116aa622007-08-15 14:28:22 +00001141
Benjamin Petersone41251e2008-04-25 01:59:09 +00001142 Return a list of the names of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001143
1144
Benjamin Petersone41251e2008-04-25 01:59:09 +00001145 .. method:: set_sequences(sequences)
Georg Brandl116aa622007-08-15 14:28:22 +00001146
Benjamin Petersone41251e2008-04-25 01:59:09 +00001147 Set the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001148
1149
Benjamin Petersone41251e2008-04-25 01:59:09 +00001150 .. method:: add_sequence(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +00001151
Benjamin Petersone41251e2008-04-25 01:59:09 +00001152 Add *sequence* to the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001153
1154
Benjamin Petersone41251e2008-04-25 01:59:09 +00001155 .. method:: remove_sequence(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +00001156
Benjamin Petersone41251e2008-04-25 01:59:09 +00001157 Remove *sequence* from the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001158
1159When an :class:`MHMessage` instance is created based upon a
1160:class:`MaildirMessage` instance, the following conversions take place:
1161
1162+--------------------+-------------------------------+
1163| Resulting state | :class:`MaildirMessage` state |
1164+====================+===============================+
1165| "unseen" sequence | no S flag |
1166+--------------------+-------------------------------+
1167| "replied" sequence | R flag |
1168+--------------------+-------------------------------+
1169| "flagged" sequence | F flag |
1170+--------------------+-------------------------------+
1171
1172When an :class:`MHMessage` instance is created based upon an
1173:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
1174and :mailheader:`X-Status` headers are omitted and the following conversions
1175take place:
1176
1177+--------------------+----------------------------------------------+
1178| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
1179| | state |
1180+====================+==============================================+
1181| "unseen" sequence | no R flag |
1182+--------------------+----------------------------------------------+
1183| "replied" sequence | A flag |
1184+--------------------+----------------------------------------------+
1185| "flagged" sequence | F flag |
1186+--------------------+----------------------------------------------+
1187
1188When an :class:`MHMessage` instance is created based upon a
1189:class:`BabylMessage` instance, the following conversions take place:
1190
1191+--------------------+-----------------------------+
1192| Resulting state | :class:`BabylMessage` state |
1193+====================+=============================+
1194| "unseen" sequence | "unseen" label |
1195+--------------------+-----------------------------+
1196| "replied" sequence | "answered" label |
1197+--------------------+-----------------------------+
1198
1199
1200.. _mailbox-babylmessage:
1201
1202:class:`BabylMessage`
1203^^^^^^^^^^^^^^^^^^^^^
1204
1205
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001206.. class:: BabylMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001207
1208 A message with Babyl-specific behaviors. Parameter *message* has the same
1209 meaning as with the :class:`Message` constructor.
1210
Benjamin Petersone41251e2008-04-25 01:59:09 +00001211 Certain message labels, called :dfn:`attributes`, are defined by convention
1212 to have special meanings. The attributes are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001213
Benjamin Petersone41251e2008-04-25 01:59:09 +00001214 +-----------+------------------------------------------+
1215 | Label | Explanation |
1216 +===========+==========================================+
1217 | unseen | Not read, but previously detected by MUA |
1218 +-----------+------------------------------------------+
1219 | deleted | Marked for subsequent deletion |
1220 +-----------+------------------------------------------+
1221 | filed | Copied to another file or mailbox |
1222 +-----------+------------------------------------------+
1223 | answered | Replied to |
1224 +-----------+------------------------------------------+
1225 | forwarded | Forwarded |
1226 +-----------+------------------------------------------+
1227 | edited | Modified by the user |
1228 +-----------+------------------------------------------+
1229 | resent | Resent |
1230 +-----------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001231
Benjamin Petersone41251e2008-04-25 01:59:09 +00001232 By default, Rmail displays only visible headers. The :class:`BabylMessage`
1233 class, though, uses the original headers because they are more
1234 complete. Visible headers may be accessed explicitly if desired.
Georg Brandl116aa622007-08-15 14:28:22 +00001235
Benjamin Petersone41251e2008-04-25 01:59:09 +00001236 :class:`BabylMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001237
1238
Benjamin Petersone41251e2008-04-25 01:59:09 +00001239 .. method:: get_labels()
Georg Brandl116aa622007-08-15 14:28:22 +00001240
Benjamin Petersone41251e2008-04-25 01:59:09 +00001241 Return a list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001242
1243
Benjamin Petersone41251e2008-04-25 01:59:09 +00001244 .. method:: set_labels(labels)
Georg Brandl116aa622007-08-15 14:28:22 +00001245
Benjamin Petersone41251e2008-04-25 01:59:09 +00001246 Set the list of labels on the message to *labels*.
Georg Brandl116aa622007-08-15 14:28:22 +00001247
1248
Benjamin Petersone41251e2008-04-25 01:59:09 +00001249 .. method:: add_label(label)
Georg Brandl116aa622007-08-15 14:28:22 +00001250
Benjamin Petersone41251e2008-04-25 01:59:09 +00001251 Add *label* to the list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001252
1253
Benjamin Petersone41251e2008-04-25 01:59:09 +00001254 .. method:: remove_label(label)
Georg Brandl116aa622007-08-15 14:28:22 +00001255
Benjamin Petersone41251e2008-04-25 01:59:09 +00001256 Remove *label* from the list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001257
1258
Benjamin Petersone41251e2008-04-25 01:59:09 +00001259 .. method:: get_visible()
Georg Brandl116aa622007-08-15 14:28:22 +00001260
Benjamin Petersone41251e2008-04-25 01:59:09 +00001261 Return an :class:`Message` instance whose headers are the message's
1262 visible headers and whose body is empty.
Georg Brandl116aa622007-08-15 14:28:22 +00001263
1264
Benjamin Petersone41251e2008-04-25 01:59:09 +00001265 .. method:: set_visible(visible)
Georg Brandl116aa622007-08-15 14:28:22 +00001266
Benjamin Petersone41251e2008-04-25 01:59:09 +00001267 Set the message's visible headers to be the same as the headers in
1268 *message*. Parameter *visible* should be a :class:`Message` instance, an
1269 :class:`email.Message.Message` instance, a string, or a file-like object
1270 (which should be open in text mode).
Georg Brandl116aa622007-08-15 14:28:22 +00001271
1272
Benjamin Petersone41251e2008-04-25 01:59:09 +00001273 .. method:: update_visible()
Georg Brandl116aa622007-08-15 14:28:22 +00001274
Benjamin Petersone41251e2008-04-25 01:59:09 +00001275 When a :class:`BabylMessage` instance's original headers are modified, the
1276 visible headers are not automatically modified to correspond. This method
1277 updates the visible headers as follows: each visible header with a
1278 corresponding original header is set to the value of the original header,
1279 each visible header without a corresponding original header is removed,
1280 and any of :mailheader:`Date`, :mailheader:`From`, :mailheader:`Reply-To`,
1281 :mailheader:`To`, :mailheader:`CC`, and :mailheader:`Subject` that are
1282 present in the original headers but not the visible headers are added to
1283 the visible headers.
Georg Brandl116aa622007-08-15 14:28:22 +00001284
1285When a :class:`BabylMessage` instance is created based upon a
1286:class:`MaildirMessage` instance, the following conversions take place:
1287
1288+-------------------+-------------------------------+
1289| Resulting state | :class:`MaildirMessage` state |
1290+===================+===============================+
1291| "unseen" label | no S flag |
1292+-------------------+-------------------------------+
1293| "deleted" label | T flag |
1294+-------------------+-------------------------------+
1295| "answered" label | R flag |
1296+-------------------+-------------------------------+
1297| "forwarded" label | P flag |
1298+-------------------+-------------------------------+
1299
1300When a :class:`BabylMessage` instance is created based upon an
1301:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
1302and :mailheader:`X-Status` headers are omitted and the following conversions
1303take place:
1304
1305+------------------+----------------------------------------------+
1306| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
1307| | state |
1308+==================+==============================================+
1309| "unseen" label | no R flag |
1310+------------------+----------------------------------------------+
1311| "deleted" label | D flag |
1312+------------------+----------------------------------------------+
1313| "answered" label | A flag |
1314+------------------+----------------------------------------------+
1315
1316When a :class:`BabylMessage` instance is created based upon an
1317:class:`MHMessage` instance, the following conversions take place:
1318
1319+------------------+--------------------------+
1320| Resulting state | :class:`MHMessage` state |
1321+==================+==========================+
1322| "unseen" label | "unseen" sequence |
1323+------------------+--------------------------+
1324| "answered" label | "replied" sequence |
1325+------------------+--------------------------+
1326
1327
1328.. _mailbox-mmdfmessage:
1329
1330:class:`MMDFMessage`
1331^^^^^^^^^^^^^^^^^^^^
1332
1333
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001334.. class:: MMDFMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001335
1336 A message with MMDF-specific behaviors. Parameter *message* has the same meaning
1337 as with the :class:`Message` constructor.
1338
Benjamin Petersone41251e2008-04-25 01:59:09 +00001339 As with message in an mbox mailbox, MMDF messages are stored with the
1340 sender's address and the delivery date in an initial line beginning with
1341 "From ". Likewise, flags that indicate the state of the message are
1342 typically stored in :mailheader:`Status` and :mailheader:`X-Status` headers.
Georg Brandl116aa622007-08-15 14:28:22 +00001343
Benjamin Petersone41251e2008-04-25 01:59:09 +00001344 Conventional flags for MMDF messages are identical to those of mbox message
1345 and are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001346
Benjamin Petersone41251e2008-04-25 01:59:09 +00001347 +------+----------+--------------------------------+
1348 | Flag | Meaning | Explanation |
1349 +======+==========+================================+
1350 | R | Read | Read |
1351 +------+----------+--------------------------------+
1352 | O | Old | Previously detected by MUA |
1353 +------+----------+--------------------------------+
1354 | D | Deleted | Marked for subsequent deletion |
1355 +------+----------+--------------------------------+
1356 | F | Flagged | Marked as important |
1357 +------+----------+--------------------------------+
1358 | A | Answered | Replied to |
1359 +------+----------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001360
Benjamin Petersone41251e2008-04-25 01:59:09 +00001361 The "R" and "O" flags are stored in the :mailheader:`Status` header, and the
1362 "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The
1363 flags and headers typically appear in the order mentioned.
Georg Brandl116aa622007-08-15 14:28:22 +00001364
Benjamin Petersone41251e2008-04-25 01:59:09 +00001365 :class:`MMDFMessage` instances offer the following methods, which are
1366 identical to those offered by :class:`mboxMessage`:
Georg Brandl116aa622007-08-15 14:28:22 +00001367
1368
Benjamin Petersone41251e2008-04-25 01:59:09 +00001369 .. method:: get_from()
Georg Brandl116aa622007-08-15 14:28:22 +00001370
Benjamin Petersone41251e2008-04-25 01:59:09 +00001371 Return a string representing the "From " line that marks the start of the
1372 message in an mbox mailbox. The leading "From " and the trailing newline
1373 are excluded.
Georg Brandl116aa622007-08-15 14:28:22 +00001374
1375
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001376 .. method:: set_from(from_, time_=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001377
Benjamin Petersone41251e2008-04-25 01:59:09 +00001378 Set the "From " line to *from_*, which should be specified without a
1379 leading "From " or trailing newline. For convenience, *time_* may be
1380 specified and will be formatted appropriately and appended to *from_*. If
1381 *time_* is specified, it should be a :class:`struct_time` instance, a
1382 tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use
1383 :meth:`time.gmtime`).
Georg Brandl116aa622007-08-15 14:28:22 +00001384
1385
Benjamin Petersone41251e2008-04-25 01:59:09 +00001386 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +00001387
Benjamin Petersone41251e2008-04-25 01:59:09 +00001388 Return a string specifying the flags that are currently set. If the
1389 message complies with the conventional format, the result is the
1390 concatenation in the following order of zero or one occurrence of each of
1391 ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001392
1393
Benjamin Petersone41251e2008-04-25 01:59:09 +00001394 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +00001395
Benjamin Petersone41251e2008-04-25 01:59:09 +00001396 Set the flags specified by *flags* and unset all others. Parameter *flags*
1397 should be the concatenation in any order of zero or more occurrences of
1398 each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001399
1400
Benjamin Petersone41251e2008-04-25 01:59:09 +00001401 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001402
Benjamin Petersone41251e2008-04-25 01:59:09 +00001403 Set the flag(s) specified by *flag* without changing other flags. To add
1404 more than one flag at a time, *flag* may be a string of more than one
1405 character.
Georg Brandl116aa622007-08-15 14:28:22 +00001406
1407
Benjamin Petersone41251e2008-04-25 01:59:09 +00001408 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001409
Benjamin Petersone41251e2008-04-25 01:59:09 +00001410 Unset the flag(s) specified by *flag* without changing other flags. To
1411 remove more than one flag at a time, *flag* maybe a string of more than
1412 one character.
Georg Brandl116aa622007-08-15 14:28:22 +00001413
1414When an :class:`MMDFMessage` instance is created based upon a
1415:class:`MaildirMessage` instance, a "From " line is generated based upon the
1416:class:`MaildirMessage` instance's delivery date, and the following conversions
1417take place:
1418
1419+-----------------+-------------------------------+
1420| Resulting state | :class:`MaildirMessage` state |
1421+=================+===============================+
1422| R flag | S flag |
1423+-----------------+-------------------------------+
1424| O flag | "cur" subdirectory |
1425+-----------------+-------------------------------+
1426| D flag | T flag |
1427+-----------------+-------------------------------+
1428| F flag | F flag |
1429+-----------------+-------------------------------+
1430| A flag | R flag |
1431+-----------------+-------------------------------+
1432
1433When an :class:`MMDFMessage` instance is created based upon an
1434:class:`MHMessage` instance, the following conversions take place:
1435
1436+-------------------+--------------------------+
1437| Resulting state | :class:`MHMessage` state |
1438+===================+==========================+
1439| R flag and O flag | no "unseen" sequence |
1440+-------------------+--------------------------+
1441| O flag | "unseen" sequence |
1442+-------------------+--------------------------+
1443| F flag | "flagged" sequence |
1444+-------------------+--------------------------+
1445| A flag | "replied" sequence |
1446+-------------------+--------------------------+
1447
1448When an :class:`MMDFMessage` instance is created based upon a
1449:class:`BabylMessage` instance, the following conversions take place:
1450
1451+-------------------+-----------------------------+
1452| Resulting state | :class:`BabylMessage` state |
1453+===================+=============================+
1454| R flag and O flag | no "unseen" label |
1455+-------------------+-----------------------------+
1456| O flag | "unseen" label |
1457+-------------------+-----------------------------+
1458| D flag | "deleted" label |
1459+-------------------+-----------------------------+
1460| A flag | "answered" label |
1461+-------------------+-----------------------------+
1462
1463When an :class:`MMDFMessage` instance is created based upon an
1464:class:`mboxMessage` instance, the "From " line is copied and all flags directly
1465correspond:
1466
1467+-----------------+----------------------------+
1468| Resulting state | :class:`mboxMessage` state |
1469+=================+============================+
1470| R flag | R flag |
1471+-----------------+----------------------------+
1472| O flag | O flag |
1473+-----------------+----------------------------+
1474| D flag | D flag |
1475+-----------------+----------------------------+
1476| F flag | F flag |
1477+-----------------+----------------------------+
1478| A flag | A flag |
1479+-----------------+----------------------------+
1480
1481
1482Exceptions
1483----------
1484
1485The following exception classes are defined in the :mod:`mailbox` module:
1486
1487
Benjamin Petersone41251e2008-04-25 01:59:09 +00001488.. exception:: Error()
Georg Brandl116aa622007-08-15 14:28:22 +00001489
1490 The based class for all other module-specific exceptions.
1491
1492
Benjamin Petersone41251e2008-04-25 01:59:09 +00001493.. exception:: NoSuchMailboxError()
Georg Brandl116aa622007-08-15 14:28:22 +00001494
1495 Raised when a mailbox is expected but is not found, such as when instantiating a
1496 :class:`Mailbox` subclass with a path that does not exist (and with the *create*
1497 parameter set to ``False``), or when opening a folder that does not exist.
1498
1499
Georg Brandl734e2682008-08-12 08:18:18 +00001500.. exception:: NotEmptyError()
Georg Brandl116aa622007-08-15 14:28:22 +00001501
1502 Raised when a mailbox is not empty but is expected to be, such as when deleting
1503 a folder that contains messages.
1504
1505
Benjamin Petersone41251e2008-04-25 01:59:09 +00001506.. exception:: ExternalClashError()
Georg Brandl116aa622007-08-15 14:28:22 +00001507
1508 Raised when some mailbox-related condition beyond the control of the program
1509 causes it to be unable to proceed, such as when failing to acquire a lock that
1510 another program already holds a lock, or when a uniquely-generated file name
1511 already exists.
1512
1513
Benjamin Petersone41251e2008-04-25 01:59:09 +00001514.. exception:: FormatError()
Georg Brandl116aa622007-08-15 14:28:22 +00001515
1516 Raised when the data in a file cannot be parsed, such as when an :class:`MH`
1517 instance attempts to read a corrupted :file:`.mh_sequences` file.
1518
1519
Georg Brandl116aa622007-08-15 14:28:22 +00001520.. _mailbox-examples:
1521
1522Examples
1523--------
1524
1525A simple example of printing the subjects of all messages in a mailbox that seem
1526interesting::
1527
1528 import mailbox
1529 for message in mailbox.mbox('~/mbox'):
1530 subject = message['subject'] # Could possibly be None.
1531 if subject and 'python' in subject.lower():
Georg Brandl6911e3c2007-09-04 07:15:32 +00001532 print(subject)
Georg Brandl116aa622007-08-15 14:28:22 +00001533
1534To copy all mail from a Babyl mailbox to an MH mailbox, converting all of the
1535format-specific information that can be converted::
1536
1537 import mailbox
1538 destination = mailbox.MH('~/Mail')
1539 destination.lock()
1540 for message in mailbox.Babyl('~/RMAIL'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001541 destination.add(mailbox.MHMessage(message))
Georg Brandl116aa622007-08-15 14:28:22 +00001542 destination.flush()
1543 destination.unlock()
1544
1545This example sorts mail from several mailing lists into different mailboxes,
1546being careful to avoid mail corruption due to concurrent modification by other
1547programs, mail loss due to interruption of the program, or premature termination
1548due to malformed messages in the mailbox::
1549
1550 import mailbox
1551 import email.Errors
1552
1553 list_names = ('python-list', 'python-dev', 'python-bugs')
1554
Georg Brandlf6945182008-02-01 11:56:49 +00001555 boxes = {name: mailbox.mbox('~/email/%s' % name) for name in list_names}
Georg Brandl116aa622007-08-15 14:28:22 +00001556 inbox = mailbox.Maildir('~/Maildir', factory=None)
1557
1558 for key in inbox.iterkeys():
1559 try:
1560 message = inbox[key]
1561 except email.Errors.MessageParseError:
1562 continue # The message is malformed. Just leave it.
1563
1564 for name in list_names:
1565 list_id = message['list-id']
1566 if list_id and name in list_id:
1567 # Get mailbox to use
1568 box = boxes[name]
1569
1570 # Write copy to disk before removing original.
1571 # If there's a crash, you might duplicate a message, but
1572 # that's better than losing a message completely.
1573 box.lock()
1574 box.add(message)
Georg Brandl48310cd2009-01-03 21:18:54 +00001575 box.flush()
Georg Brandl116aa622007-08-15 14:28:22 +00001576 box.unlock()
1577
1578 # Remove original message
1579 inbox.lock()
1580 inbox.discard(key)
1581 inbox.flush()
1582 inbox.unlock()
1583 break # Found destination, so stop looking.
1584
1585 for box in boxes.itervalues():
1586 box.close()
1587