blob: 0478ed1087bf7de39ea418c8a89498acb852dc43 [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.
R David Murray53202502012-09-28 15:19:16 -040013:class:`Message` extends the :mod:`email.message` module's
14:class:`~email.message.Message` class with format-specific state and behavior.
15Supported mailbox formats are Maildir, mbox, MH, Babyl, and MMDF.
Georg Brandl116aa622007-08-15 14:28:22 +000016
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 Murray53202502012-09-28 15:19:16 -040084 :class:`email.message.Message` instance, a string, a byte string, or a
R. David Murrayb7deff12011-01-30 06:21:28 +000085 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
Georg Brandl33369cf2012-06-24 22:48:03 +020092 .. versionchanged:: 3.2
93 Support for binary input was added.
R. David Murrayb7deff12011-01-30 06:21:28 +000094
Georg Brandl116aa622007-08-15 14:28:22 +000095
Benjamin Petersone41251e2008-04-25 01:59:09 +000096 .. method:: remove(key)
97 __delitem__(key)
98 discard(key)
Georg Brandl116aa622007-08-15 14:28:22 +000099
Benjamin Petersone41251e2008-04-25 01:59:09 +0000100 Delete the message corresponding to *key* from the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000101
Benjamin Petersone41251e2008-04-25 01:59:09 +0000102 If no such message exists, a :exc:`KeyError` exception is raised if the
103 method was called as :meth:`remove` or :meth:`__delitem__` but no
104 exception is raised if the method was called as :meth:`discard`. The
105 behavior of :meth:`discard` may be preferred if the underlying mailbox
106 format supports concurrent modification by other processes.
Georg Brandl116aa622007-08-15 14:28:22 +0000107
108
Benjamin Petersone41251e2008-04-25 01:59:09 +0000109 .. method:: __setitem__(key, message)
Georg Brandl116aa622007-08-15 14:28:22 +0000110
Benjamin Petersone41251e2008-04-25 01:59:09 +0000111 Replace the message corresponding to *key* with *message*. Raise a
112 :exc:`KeyError` exception if no message already corresponds to *key*.
Georg Brandl116aa622007-08-15 14:28:22 +0000113
Benjamin Petersone41251e2008-04-25 01:59:09 +0000114 As with :meth:`add`, parameter *message* may be a :class:`Message`
R David Murray53202502012-09-28 15:19:16 -0400115 instance, an :class:`email.message.Message` instance, a string, a byte
R. David Murrayb7deff12011-01-30 06:21:28 +0000116 string, or a file-like object (which should be open in binary mode). If
117 *message* is an
Benjamin Petersone41251e2008-04-25 01:59:09 +0000118 instance of the appropriate format-specific :class:`Message` subclass
119 (e.g., if it's an :class:`mboxMessage` instance and this is an
120 :class:`mbox` instance), its format-specific information is
121 used. Otherwise, the format-specific information of the message that
122 currently corresponds to *key* is left unchanged.
Georg Brandl116aa622007-08-15 14:28:22 +0000123
124
Benjamin Petersone41251e2008-04-25 01:59:09 +0000125 .. method:: iterkeys()
126 keys()
Georg Brandl116aa622007-08-15 14:28:22 +0000127
Benjamin Petersone41251e2008-04-25 01:59:09 +0000128 Return an iterator over all keys if called as :meth:`iterkeys` or return a
129 list of keys if called as :meth:`keys`.
Georg Brandl116aa622007-08-15 14:28:22 +0000130
131
Benjamin Petersone41251e2008-04-25 01:59:09 +0000132 .. method:: itervalues()
133 __iter__()
134 values()
Georg Brandl116aa622007-08-15 14:28:22 +0000135
Benjamin Petersone41251e2008-04-25 01:59:09 +0000136 Return an iterator over representations of all messages if called as
137 :meth:`itervalues` or :meth:`__iter__` or return a list of such
138 representations if called as :meth:`values`. The messages are represented
139 as instances of the appropriate format-specific :class:`Message` subclass
140 unless a custom message factory was specified when the :class:`Mailbox`
141 instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000142
Benjamin Petersone41251e2008-04-25 01:59:09 +0000143 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000144
Benjamin Petersone41251e2008-04-25 01:59:09 +0000145 The behavior of :meth:`__iter__` is unlike that of dictionaries, which
146 iterate over keys.
Georg Brandl116aa622007-08-15 14:28:22 +0000147
148
Benjamin Petersone41251e2008-04-25 01:59:09 +0000149 .. method:: iteritems()
150 items()
Georg Brandl116aa622007-08-15 14:28:22 +0000151
Benjamin Petersone41251e2008-04-25 01:59:09 +0000152 Return an iterator over (*key*, *message*) pairs, where *key* is a key and
153 *message* is a message representation, if called as :meth:`iteritems` or
154 return a list of such pairs if called as :meth:`items`. The messages are
155 represented as instances of the appropriate format-specific
156 :class:`Message` subclass unless a custom message factory was specified
157 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000158
159
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000160 .. method:: get(key, default=None)
Benjamin Petersone41251e2008-04-25 01:59:09 +0000161 __getitem__(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Benjamin Petersone41251e2008-04-25 01:59:09 +0000163 Return a representation of the message corresponding to *key*. If no such
164 message exists, *default* is returned if the method was called as
165 :meth:`get` and a :exc:`KeyError` exception is raised if the method was
166 called as :meth:`__getitem__`. The message is represented as an instance
167 of the appropriate format-specific :class:`Message` subclass unless a
168 custom message factory was specified when the :class:`Mailbox` instance
169 was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000170
171
Benjamin Petersone41251e2008-04-25 01:59:09 +0000172 .. method:: get_message(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000173
Benjamin Petersone41251e2008-04-25 01:59:09 +0000174 Return a representation of the message corresponding to *key* as an
175 instance of the appropriate format-specific :class:`Message` subclass, or
176 raise a :exc:`KeyError` exception if no such message exists.
Georg Brandl116aa622007-08-15 14:28:22 +0000177
178
R. David Murrayb7deff12011-01-30 06:21:28 +0000179 .. method:: get_bytes(key)
180
181 Return a byte representation of the message corresponding to *key*, or
182 raise a :exc:`KeyError` exception if no such message exists.
183
184 .. versionadded:: 3.2
185
186
Benjamin Petersone41251e2008-04-25 01:59:09 +0000187 .. method:: get_string(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000188
Benjamin Petersone41251e2008-04-25 01:59:09 +0000189 Return a string representation of the message corresponding to *key*, or
R. David Murrayb7deff12011-01-30 06:21:28 +0000190 raise a :exc:`KeyError` exception if no such message exists. The
191 message is processed through :class:`email.message.Message` to
192 convert it to a 7bit clean representation.
Georg Brandl116aa622007-08-15 14:28:22 +0000193
194
Benjamin Petersone41251e2008-04-25 01:59:09 +0000195 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000196
Benjamin Petersone41251e2008-04-25 01:59:09 +0000197 Return a file-like representation of the message corresponding to *key*,
Georg Brandl6ce29fa2010-10-30 14:33:28 +0000198 or raise a :exc:`KeyError` exception if no such message exists. The
199 file-like object behaves as if open in binary mode. This file should be
Benjamin Petersone41251e2008-04-25 01:59:09 +0000200 closed once it is no longer needed.
Georg Brandl116aa622007-08-15 14:28:22 +0000201
R. David Murrayb7deff12011-01-30 06:21:28 +0000202 .. versionchanged:: 3.2
203 The file object really is a binary file; previously it was incorrectly
204 returned in text mode. Also, the file-like object now supports the
205 context manager protocol: you can use a :keyword:`with` statement to
206 automatically close it.
Georg Brandl6ce29fa2010-10-30 14:33:28 +0000207
Benjamin Petersone41251e2008-04-25 01:59:09 +0000208 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000209
Benjamin Petersone41251e2008-04-25 01:59:09 +0000210 Unlike other representations of messages, file-like representations are
211 not necessarily independent of the :class:`Mailbox` instance that
Georg Brandl6ce29fa2010-10-30 14:33:28 +0000212 created them or of the underlying mailbox. More specific documentation
Benjamin Petersone41251e2008-04-25 01:59:09 +0000213 is provided by each subclass.
Georg Brandl116aa622007-08-15 14:28:22 +0000214
215
Benjamin Petersone41251e2008-04-25 01:59:09 +0000216 .. method:: __contains__(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000217
Benjamin Petersone41251e2008-04-25 01:59:09 +0000218 Return ``True`` if *key* corresponds to a message, ``False`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000219
220
Benjamin Petersone41251e2008-04-25 01:59:09 +0000221 .. method:: __len__()
Georg Brandl116aa622007-08-15 14:28:22 +0000222
Benjamin Petersone41251e2008-04-25 01:59:09 +0000223 Return a count of messages in the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000224
225
Benjamin Petersone41251e2008-04-25 01:59:09 +0000226 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +0000227
Benjamin Petersone41251e2008-04-25 01:59:09 +0000228 Delete all messages from the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000229
230
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000231 .. method:: pop(key, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000232
Benjamin Petersone41251e2008-04-25 01:59:09 +0000233 Return a representation of the message corresponding to *key* and delete
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000234 the message. If no such message exists, return *default*. The message is
Benjamin Petersone41251e2008-04-25 01:59:09 +0000235 represented as an instance of the appropriate format-specific
236 :class:`Message` subclass unless a custom message factory was specified
237 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000238
239
Benjamin Petersone41251e2008-04-25 01:59:09 +0000240 .. method:: popitem()
Georg Brandl116aa622007-08-15 14:28:22 +0000241
Benjamin Petersone41251e2008-04-25 01:59:09 +0000242 Return an arbitrary (*key*, *message*) pair, where *key* is a key and
243 *message* is a message representation, and delete the corresponding
244 message. If the mailbox is empty, raise a :exc:`KeyError` exception. The
245 message is represented as an instance of the appropriate format-specific
246 :class:`Message` subclass unless a custom message factory was specified
247 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000248
249
Benjamin Petersone41251e2008-04-25 01:59:09 +0000250 .. method:: update(arg)
Georg Brandl116aa622007-08-15 14:28:22 +0000251
Benjamin Petersone41251e2008-04-25 01:59:09 +0000252 Parameter *arg* should be a *key*-to-*message* mapping or an iterable of
253 (*key*, *message*) pairs. Updates the mailbox so that, for each given
254 *key* and *message*, the message corresponding to *key* is set to
255 *message* as if by using :meth:`__setitem__`. As with :meth:`__setitem__`,
256 each *key* must already correspond to a message in the mailbox or else a
257 :exc:`KeyError` exception will be raised, so in general it is incorrect
258 for *arg* to be a :class:`Mailbox` instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000259
Benjamin Petersone41251e2008-04-25 01:59:09 +0000260 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000261
Benjamin Petersone41251e2008-04-25 01:59:09 +0000262 Unlike with dictionaries, keyword arguments are not supported.
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264
Benjamin Petersone41251e2008-04-25 01:59:09 +0000265 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000266
Benjamin Petersone41251e2008-04-25 01:59:09 +0000267 Write any pending changes to the filesystem. For some :class:`Mailbox`
268 subclasses, changes are always written immediately and :meth:`flush` does
269 nothing, but you should still make a habit of calling this method.
Georg Brandl116aa622007-08-15 14:28:22 +0000270
271
Benjamin Petersone41251e2008-04-25 01:59:09 +0000272 .. method:: lock()
Georg Brandl116aa622007-08-15 14:28:22 +0000273
Benjamin Petersone41251e2008-04-25 01:59:09 +0000274 Acquire an exclusive advisory lock on the mailbox so that other processes
275 know not to modify it. An :exc:`ExternalClashError` is raised if the lock
276 is not available. The particular locking mechanisms used depend upon the
277 mailbox format. You should *always* lock the mailbox before making any
278 modifications to its contents.
Georg Brandl116aa622007-08-15 14:28:22 +0000279
280
Benjamin Petersone41251e2008-04-25 01:59:09 +0000281 .. method:: unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000282
Benjamin Petersone41251e2008-04-25 01:59:09 +0000283 Release the lock on the mailbox, if any.
Georg Brandl116aa622007-08-15 14:28:22 +0000284
285
Benjamin Petersone41251e2008-04-25 01:59:09 +0000286 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000287
Benjamin Petersone41251e2008-04-25 01:59:09 +0000288 Flush the mailbox, unlock it if necessary, and close any open files. For
289 some :class:`Mailbox` subclasses, this method does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000290
291
292.. _mailbox-maildir:
293
294:class:`Maildir`
295^^^^^^^^^^^^^^^^
296
297
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000298.. class:: Maildir(dirname, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000299
300 A subclass of :class:`Mailbox` for mailboxes in Maildir format. Parameter
301 *factory* is a callable object that accepts a file-like message representation
302 (which behaves as if opened in binary mode) and returns a custom representation.
303 If *factory* is ``None``, :class:`MaildirMessage` is used as the default message
304 representation. If *create* is ``True``, the mailbox is created if it does not
305 exist.
306
Georg Brandlaa5b4112008-05-11 20:51:18 +0000307 It is for historical reasons that *dirname* is named as such rather than *path*.
Georg Brandl116aa622007-08-15 14:28:22 +0000308
Benjamin Petersone41251e2008-04-25 01:59:09 +0000309 Maildir is a directory-based mailbox format invented for the qmail mail
310 transfer agent and now widely supported by other programs. Messages in a
311 Maildir mailbox are stored in separate files within a common directory
312 structure. This design allows Maildir mailboxes to be accessed and modified
313 by multiple unrelated programs without data corruption, so file locking is
314 unnecessary.
Georg Brandl116aa622007-08-15 14:28:22 +0000315
Benjamin Petersone41251e2008-04-25 01:59:09 +0000316 Maildir mailboxes contain three subdirectories, namely: :file:`tmp`,
317 :file:`new`, and :file:`cur`. Messages are created momentarily in the
318 :file:`tmp` subdirectory and then moved to the :file:`new` subdirectory to
319 finalize delivery. A mail user agent may subsequently move the message to the
320 :file:`cur` subdirectory and store information about the state of the message
321 in a special "info" section appended to its file name.
Georg Brandl116aa622007-08-15 14:28:22 +0000322
Benjamin Petersone41251e2008-04-25 01:59:09 +0000323 Folders of the style introduced by the Courier mail transfer agent are also
324 supported. Any subdirectory of the main mailbox is considered a folder if
325 ``'.'`` is the first character in its name. Folder names are represented by
326 :class:`Maildir` without the leading ``'.'``. Each folder is itself a Maildir
327 mailbox but should not contain other folders. Instead, a logical nesting is
328 indicated using ``'.'`` to delimit levels, e.g., "Archived.2005.07".
Georg Brandl116aa622007-08-15 14:28:22 +0000329
Benjamin Petersone41251e2008-04-25 01:59:09 +0000330 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000331
Benjamin Petersone41251e2008-04-25 01:59:09 +0000332 The Maildir specification requires the use of a colon (``':'``) in certain
333 message file names. However, some operating systems do not permit this
334 character in file names, If you wish to use a Maildir-like format on such
335 an operating system, you should specify another character to use
336 instead. The exclamation point (``'!'``) is a popular choice. For
337 example::
Georg Brandl116aa622007-08-15 14:28:22 +0000338
Benjamin Petersone41251e2008-04-25 01:59:09 +0000339 import mailbox
340 mailbox.Maildir.colon = '!'
Georg Brandl116aa622007-08-15 14:28:22 +0000341
Benjamin Petersone41251e2008-04-25 01:59:09 +0000342 The :attr:`colon` attribute may also be set on a per-instance basis.
Georg Brandl116aa622007-08-15 14:28:22 +0000343
Benjamin Petersone41251e2008-04-25 01:59:09 +0000344 :class:`Maildir` instances have all of the methods of :class:`Mailbox` in
345 addition to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000346
347
Benjamin Petersone41251e2008-04-25 01:59:09 +0000348 .. method:: list_folders()
Georg Brandl116aa622007-08-15 14:28:22 +0000349
Benjamin Petersone41251e2008-04-25 01:59:09 +0000350 Return a list of the names of all folders.
Georg Brandl116aa622007-08-15 14:28:22 +0000351
352
Ezio Melotti2d352d02009-09-15 18:51:37 +0000353 .. method:: get_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000354
Benjamin Petersone41251e2008-04-25 01:59:09 +0000355 Return a :class:`Maildir` instance representing the folder whose name is
356 *folder*. A :exc:`NoSuchMailboxError` exception is raised if the folder
357 does not exist.
Georg Brandl116aa622007-08-15 14:28:22 +0000358
359
Benjamin Petersone41251e2008-04-25 01:59:09 +0000360 .. method:: add_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000361
Benjamin Petersone41251e2008-04-25 01:59:09 +0000362 Create a folder whose name is *folder* and return a :class:`Maildir`
363 instance representing it.
Georg Brandl116aa622007-08-15 14:28:22 +0000364
365
Benjamin Petersone41251e2008-04-25 01:59:09 +0000366 .. method:: remove_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000367
Benjamin Petersone41251e2008-04-25 01:59:09 +0000368 Delete the folder whose name is *folder*. If the folder contains any
369 messages, a :exc:`NotEmptyError` exception will be raised and the folder
370 will not be deleted.
Georg Brandl116aa622007-08-15 14:28:22 +0000371
372
Benjamin Petersone41251e2008-04-25 01:59:09 +0000373 .. method:: clean()
Georg Brandl116aa622007-08-15 14:28:22 +0000374
Benjamin Petersone41251e2008-04-25 01:59:09 +0000375 Delete temporary files from the mailbox that have not been accessed in the
376 last 36 hours. The Maildir specification says that mail-reading programs
377 should do this occasionally.
Georg Brandl116aa622007-08-15 14:28:22 +0000378
Benjamin Petersone41251e2008-04-25 01:59:09 +0000379 Some :class:`Mailbox` methods implemented by :class:`Maildir` deserve special
380 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000381
382
Benjamin Petersone41251e2008-04-25 01:59:09 +0000383 .. method:: add(message)
384 __setitem__(key, message)
385 update(arg)
Georg Brandl116aa622007-08-15 14:28:22 +0000386
Benjamin Petersone41251e2008-04-25 01:59:09 +0000387 .. warning::
Georg Brandl116aa622007-08-15 14:28:22 +0000388
Benjamin Petersone41251e2008-04-25 01:59:09 +0000389 These methods generate unique file names based upon the current process
390 ID. When using multiple threads, undetected name clashes may occur and
391 cause corruption of the mailbox unless threads are coordinated to avoid
392 using these methods to manipulate the same mailbox simultaneously.
Georg Brandl116aa622007-08-15 14:28:22 +0000393
394
Benjamin Petersone41251e2008-04-25 01:59:09 +0000395 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000396
Benjamin Petersone41251e2008-04-25 01:59:09 +0000397 All changes to Maildir mailboxes are immediately applied, so this method
398 does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000399
400
Benjamin Petersone41251e2008-04-25 01:59:09 +0000401 .. method:: lock()
402 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000403
Benjamin Petersone41251e2008-04-25 01:59:09 +0000404 Maildir mailboxes do not support (or require) locking, so these methods do
405 nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000406
407
Benjamin Petersone41251e2008-04-25 01:59:09 +0000408 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000409
Benjamin Petersone41251e2008-04-25 01:59:09 +0000410 :class:`Maildir` instances do not keep any open files and the underlying
411 mailboxes do not support locking, so this method does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000412
413
Benjamin Petersone41251e2008-04-25 01:59:09 +0000414 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000415
Benjamin Petersone41251e2008-04-25 01:59:09 +0000416 Depending upon the host platform, it may not be possible to modify or
417 remove the underlying message while the returned file remains open.
Georg Brandl116aa622007-08-15 14:28:22 +0000418
419
420.. seealso::
421
422 `maildir man page from qmail <http://www.qmail.org/man/man5/maildir.html>`_
423 The original specification of the format.
424
425 `Using maildir format <http://cr.yp.to/proto/maildir.html>`_
426 Notes on Maildir by its inventor. Includes an updated name-creation scheme and
427 details on "info" semantics.
428
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000429 `maildir man page from Courier <http://www.courier-mta.org/maildir.html>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000430 Another specification of the format. Describes a common extension for supporting
431 folders.
432
433
434.. _mailbox-mbox:
435
436:class:`mbox`
437^^^^^^^^^^^^^
438
439
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000440.. class:: mbox(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000441
442 A subclass of :class:`Mailbox` for mailboxes in mbox format. Parameter *factory*
443 is a callable object that accepts a file-like message representation (which
444 behaves as if opened in binary mode) and returns a custom representation. If
445 *factory* is ``None``, :class:`mboxMessage` is used as the default message
446 representation. If *create* is ``True``, the mailbox is created if it does not
447 exist.
448
Benjamin Petersone41251e2008-04-25 01:59:09 +0000449 The mbox format is the classic format for storing mail on Unix systems. All
450 messages in an mbox mailbox are stored in a single file with the beginning of
451 each message indicated by a line whose first five characters are "From ".
Georg Brandl116aa622007-08-15 14:28:22 +0000452
Benjamin Petersone41251e2008-04-25 01:59:09 +0000453 Several variations of the mbox format exist to address perceived shortcomings in
454 the original. In the interest of compatibility, :class:`mbox` implements the
455 original format, which is sometimes referred to as :dfn:`mboxo`. This means that
456 the :mailheader:`Content-Length` header, if present, is ignored and that any
457 occurrences of "From " at the beginning of a line in a message body are
458 transformed to ">From " when storing the message, although occurrences of ">From
459 " are not transformed to "From " when reading the message.
Georg Brandl116aa622007-08-15 14:28:22 +0000460
Benjamin Petersone41251e2008-04-25 01:59:09 +0000461 Some :class:`Mailbox` methods implemented by :class:`mbox` deserve special
462 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000463
464
Benjamin Petersone41251e2008-04-25 01:59:09 +0000465 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000466
Benjamin Petersone41251e2008-04-25 01:59:09 +0000467 Using the file after calling :meth:`flush` or :meth:`close` on the
468 :class:`mbox` instance may yield unpredictable results or raise an
469 exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000470
471
Benjamin Petersone41251e2008-04-25 01:59:09 +0000472 .. method:: lock()
473 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000474
Benjamin Petersone41251e2008-04-25 01:59:09 +0000475 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000476 :c:func:`flock` and :c:func:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000477
478
479.. seealso::
480
481 `mbox man page from qmail <http://www.qmail.org/man/man5/mbox.html>`_
482 A specification of the format and its variations.
483
484 `mbox man page from tin <http://www.tin.org/bin/man.cgi?section=5&topic=mbox>`_
485 Another specification of the format, with details on locking.
486
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000487 `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 +0000488 An argument for using the original mbox format rather than a variation.
489
490 `"mbox" is a family of several mutually incompatible mailbox formats <http://homepages.tesco.net./~J.deBoynePollard/FGA/mail-mbox-formats.html>`_
491 A history of mbox variations.
492
493
494.. _mailbox-mh:
495
496:class:`MH`
497^^^^^^^^^^^
498
499
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000500.. class:: MH(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000501
502 A subclass of :class:`Mailbox` for mailboxes in MH format. Parameter *factory*
503 is a callable object that accepts a file-like message representation (which
504 behaves as if opened in binary mode) and returns a custom representation. If
505 *factory* is ``None``, :class:`MHMessage` is used as the default message
506 representation. If *create* is ``True``, the mailbox is created if it does not
507 exist.
508
Benjamin Petersone41251e2008-04-25 01:59:09 +0000509 MH is a directory-based mailbox format invented for the MH Message Handling
510 System, a mail user agent. Each message in an MH mailbox resides in its own
511 file. An MH mailbox may contain other MH mailboxes (called :dfn:`folders`) in
512 addition to messages. Folders may be nested indefinitely. MH mailboxes also
513 support :dfn:`sequences`, which are named lists used to logically group
514 messages without moving them to sub-folders. Sequences are defined in a file
515 called :file:`.mh_sequences` in each folder.
Georg Brandl116aa622007-08-15 14:28:22 +0000516
Benjamin Petersone41251e2008-04-25 01:59:09 +0000517 The :class:`MH` class manipulates MH mailboxes, but it does not attempt to
518 emulate all of :program:`mh`'s behaviors. In particular, it does not modify
519 and is not affected by the :file:`context` or :file:`.mh_profile` files that
520 are used by :program:`mh` to store its state and configuration.
Georg Brandl116aa622007-08-15 14:28:22 +0000521
Benjamin Petersone41251e2008-04-25 01:59:09 +0000522 :class:`MH` instances have all of the methods of :class:`Mailbox` in addition
523 to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000524
525
Benjamin Petersone41251e2008-04-25 01:59:09 +0000526 .. method:: list_folders()
Georg Brandl116aa622007-08-15 14:28:22 +0000527
Benjamin Petersone41251e2008-04-25 01:59:09 +0000528 Return a list of the names of all folders.
Georg Brandl116aa622007-08-15 14:28:22 +0000529
530
Benjamin Petersone41251e2008-04-25 01:59:09 +0000531 .. method:: get_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000532
Benjamin Petersone41251e2008-04-25 01:59:09 +0000533 Return an :class:`MH` instance representing the folder whose name is
534 *folder*. A :exc:`NoSuchMailboxError` exception is raised if the folder
535 does not exist.
Georg Brandl116aa622007-08-15 14:28:22 +0000536
537
Benjamin Petersone41251e2008-04-25 01:59:09 +0000538 .. method:: add_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000539
Benjamin Petersone41251e2008-04-25 01:59:09 +0000540 Create a folder whose name is *folder* and return an :class:`MH` instance
541 representing it.
Georg Brandl116aa622007-08-15 14:28:22 +0000542
543
Benjamin Petersone41251e2008-04-25 01:59:09 +0000544 .. method:: remove_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000545
Benjamin Petersone41251e2008-04-25 01:59:09 +0000546 Delete the folder whose name is *folder*. If the folder contains any
547 messages, a :exc:`NotEmptyError` exception will be raised and the folder
548 will not be deleted.
Georg Brandl116aa622007-08-15 14:28:22 +0000549
550
Benjamin Petersone41251e2008-04-25 01:59:09 +0000551 .. method:: get_sequences()
Georg Brandl116aa622007-08-15 14:28:22 +0000552
Benjamin Petersone41251e2008-04-25 01:59:09 +0000553 Return a dictionary of sequence names mapped to key lists. If there are no
554 sequences, the empty dictionary is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000555
556
Benjamin Petersone41251e2008-04-25 01:59:09 +0000557 .. method:: set_sequences(sequences)
Georg Brandl116aa622007-08-15 14:28:22 +0000558
Benjamin Petersone41251e2008-04-25 01:59:09 +0000559 Re-define the sequences that exist in the mailbox based upon *sequences*,
560 a dictionary of names mapped to key lists, like returned by
561 :meth:`get_sequences`.
Georg Brandl116aa622007-08-15 14:28:22 +0000562
563
Benjamin Petersone41251e2008-04-25 01:59:09 +0000564 .. method:: pack()
Georg Brandl116aa622007-08-15 14:28:22 +0000565
Benjamin Petersone41251e2008-04-25 01:59:09 +0000566 Rename messages in the mailbox as necessary to eliminate gaps in
567 numbering. Entries in the sequences list are updated correspondingly.
Georg Brandl116aa622007-08-15 14:28:22 +0000568
Benjamin Petersone41251e2008-04-25 01:59:09 +0000569 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000570
Benjamin Petersone41251e2008-04-25 01:59:09 +0000571 Already-issued keys are invalidated by this operation and should not be
572 subsequently used.
Georg Brandl116aa622007-08-15 14:28:22 +0000573
Benjamin Petersone41251e2008-04-25 01:59:09 +0000574 Some :class:`Mailbox` methods implemented by :class:`MH` deserve special
575 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000576
577
Benjamin Petersone41251e2008-04-25 01:59:09 +0000578 .. method:: remove(key)
579 __delitem__(key)
580 discard(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000581
Benjamin Petersone41251e2008-04-25 01:59:09 +0000582 These methods immediately delete the message. The MH convention of marking
583 a message for deletion by prepending a comma to its name is not used.
Georg Brandl116aa622007-08-15 14:28:22 +0000584
585
Benjamin Petersone41251e2008-04-25 01:59:09 +0000586 .. method:: lock()
587 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000588
Benjamin Petersone41251e2008-04-25 01:59:09 +0000589 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000590 :c:func:`flock` and :c:func:`lockf` system calls. For MH mailboxes, locking
Benjamin Petersone41251e2008-04-25 01:59:09 +0000591 the mailbox means locking the :file:`.mh_sequences` file and, only for the
592 duration of any operations that affect them, locking individual message
593 files.
Georg Brandl116aa622007-08-15 14:28:22 +0000594
595
Benjamin Petersone41251e2008-04-25 01:59:09 +0000596 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000597
Benjamin Petersone41251e2008-04-25 01:59:09 +0000598 Depending upon the host platform, it may not be possible to remove the
599 underlying message while the returned file remains open.
Georg Brandl116aa622007-08-15 14:28:22 +0000600
601
Benjamin Petersone41251e2008-04-25 01:59:09 +0000602 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000603
Benjamin Petersone41251e2008-04-25 01:59:09 +0000604 All changes to MH mailboxes are immediately applied, so this method does
605 nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000606
607
Benjamin Petersone41251e2008-04-25 01:59:09 +0000608 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000609
Benjamin Petersone41251e2008-04-25 01:59:09 +0000610 :class:`MH` instances do not keep any open files, so this method is
611 equivalent to :meth:`unlock`.
Georg Brandl116aa622007-08-15 14:28:22 +0000612
613
614.. seealso::
615
616 `nmh - Message Handling System <http://www.nongnu.org/nmh/>`_
617 Home page of :program:`nmh`, an updated version of the original :program:`mh`.
618
Georg Brandl495f7b52009-10-27 15:28:25 +0000619 `MH & nmh: Email for Users & Programmers <http://rand-mh.sourceforge.net/book/>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000620 A GPL-licensed book on :program:`mh` and :program:`nmh`, with some information
621 on the mailbox format.
622
623
624.. _mailbox-babyl:
625
626:class:`Babyl`
627^^^^^^^^^^^^^^
628
629
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000630.. class:: Babyl(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000631
632 A subclass of :class:`Mailbox` for mailboxes in Babyl format. Parameter
633 *factory* is a callable object that accepts a file-like message representation
634 (which behaves as if opened in binary mode) and returns a custom representation.
635 If *factory* is ``None``, :class:`BabylMessage` is used as the default message
636 representation. If *create* is ``True``, the mailbox is created if it does not
637 exist.
638
Benjamin Petersone41251e2008-04-25 01:59:09 +0000639 Babyl is a single-file mailbox format used by the Rmail mail user agent
640 included with Emacs. The beginning of a message is indicated by a line
641 containing the two characters Control-Underscore (``'\037'``) and Control-L
642 (``'\014'``). The end of a message is indicated by the start of the next
643 message or, in the case of the last message, a line containing a
644 Control-Underscore (``'\037'``) character.
Georg Brandl116aa622007-08-15 14:28:22 +0000645
Benjamin Petersone41251e2008-04-25 01:59:09 +0000646 Messages in a Babyl mailbox have two sets of headers, original headers and
647 so-called visible headers. Visible headers are typically a subset of the
648 original headers that have been reformatted or abridged to be more
649 attractive. Each message in a Babyl mailbox also has an accompanying list of
650 :dfn:`labels`, or short strings that record extra information about the
651 message, and a list of all user-defined labels found in the mailbox is kept
652 in the Babyl options section.
Georg Brandl116aa622007-08-15 14:28:22 +0000653
Benjamin Petersone41251e2008-04-25 01:59:09 +0000654 :class:`Babyl` instances have all of the methods of :class:`Mailbox` in
655 addition to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000656
657
Benjamin Petersone41251e2008-04-25 01:59:09 +0000658 .. method:: get_labels()
Georg Brandl116aa622007-08-15 14:28:22 +0000659
Benjamin Petersone41251e2008-04-25 01:59:09 +0000660 Return a list of the names of all user-defined labels used in the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000661
Benjamin Petersone41251e2008-04-25 01:59:09 +0000662 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000663
Benjamin Petersone41251e2008-04-25 01:59:09 +0000664 The actual messages are inspected to determine which labels exist in
665 the mailbox rather than consulting the list of labels in the Babyl
666 options section, but the Babyl section is updated whenever the mailbox
667 is modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000668
Benjamin Petersone41251e2008-04-25 01:59:09 +0000669 Some :class:`Mailbox` methods implemented by :class:`Babyl` deserve special
670 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000671
672
Benjamin Petersone41251e2008-04-25 01:59:09 +0000673 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000674
Benjamin Petersone41251e2008-04-25 01:59:09 +0000675 In Babyl mailboxes, the headers of a message are not stored contiguously
676 with the body of the message. To generate a file-like representation, the
Serhiy Storchakae79be872013-08-17 00:09:55 +0300677 headers and body are copied together into a :class:`io.BytesIO` instance,
678 which has an API identical to that of a
Benjamin Petersone41251e2008-04-25 01:59:09 +0000679 file. As a result, the file-like object is truly independent of the
680 underlying mailbox but does not save memory compared to a string
681 representation.
Georg Brandl116aa622007-08-15 14:28:22 +0000682
683
Benjamin Petersone41251e2008-04-25 01:59:09 +0000684 .. method:: lock()
685 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000686
Benjamin Petersone41251e2008-04-25 01:59:09 +0000687 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000688 :c:func:`flock` and :c:func:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000689
690
691.. seealso::
692
693 `Format of Version 5 Babyl Files <http://quimby.gnus.org/notes/BABYL>`_
694 A specification of the Babyl format.
695
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000696 `Reading Mail with Rmail <http://www.gnu.org/software/emacs/manual/html_node/emacs/Rmail.html>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000697 The Rmail manual, with some information on Babyl semantics.
698
699
700.. _mailbox-mmdf:
701
702:class:`MMDF`
703^^^^^^^^^^^^^
704
705
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000706.. class:: MMDF(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000707
708 A subclass of :class:`Mailbox` for mailboxes in MMDF format. Parameter *factory*
709 is a callable object that accepts a file-like message representation (which
710 behaves as if opened in binary mode) and returns a custom representation. If
711 *factory* is ``None``, :class:`MMDFMessage` is used as the default message
712 representation. If *create* is ``True``, the mailbox is created if it does not
713 exist.
714
Benjamin Petersone41251e2008-04-25 01:59:09 +0000715 MMDF is a single-file mailbox format invented for the Multichannel Memorandum
716 Distribution Facility, a mail transfer agent. Each message is in the same
717 form as an mbox message but is bracketed before and after by lines containing
718 four Control-A (``'\001'``) characters. As with the mbox format, the
719 beginning of each message is indicated by a line whose first five characters
720 are "From ", but additional occurrences of "From " are not transformed to
721 ">From " when storing messages because the extra message separator lines
722 prevent mistaking such occurrences for the starts of subsequent messages.
Georg Brandl116aa622007-08-15 14:28:22 +0000723
Benjamin Petersone41251e2008-04-25 01:59:09 +0000724 Some :class:`Mailbox` methods implemented by :class:`MMDF` deserve special
725 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000726
727
Benjamin Petersone41251e2008-04-25 01:59:09 +0000728 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000729
Benjamin Petersone41251e2008-04-25 01:59:09 +0000730 Using the file after calling :meth:`flush` or :meth:`close` on the
731 :class:`MMDF` instance may yield unpredictable results or raise an
732 exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000733
734
Benjamin Petersone41251e2008-04-25 01:59:09 +0000735 .. method:: lock()
736 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000737
Benjamin Petersone41251e2008-04-25 01:59:09 +0000738 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000739 :c:func:`flock` and :c:func:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000740
741
742.. seealso::
743
744 `mmdf man page from tin <http://www.tin.org/bin/man.cgi?section=5&topic=mmdf>`_
745 A specification of MMDF format from the documentation of tin, a newsreader.
746
747 `MMDF <http://en.wikipedia.org/wiki/MMDF>`_
748 A Wikipedia article describing the Multichannel Memorandum Distribution
749 Facility.
750
751
752.. _mailbox-message-objects:
753
754:class:`Message` objects
755------------------------
756
757
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000758.. class:: Message(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000759
R David Murrayc5fe4072012-09-28 15:09:31 -0400760 A subclass of the :mod:`email.message` module's
761 :class:`~email.message.Message`. Subclasses of :class:`mailbox.Message` add
762 mailbox-format-specific state and behavior.
Georg Brandl116aa622007-08-15 14:28:22 +0000763
764 If *message* is omitted, the new instance is created in a default, empty state.
R David Murrayc5fe4072012-09-28 15:09:31 -0400765 If *message* is an :class:`email.message.Message` instance, its contents are
Georg Brandl116aa622007-08-15 14:28:22 +0000766 copied; furthermore, any format-specific information is converted insofar as
R. David Murrayb7deff12011-01-30 06:21:28 +0000767 possible if *message* is a :class:`Message` instance. If *message* is a string,
768 a byte string,
Georg Brandl116aa622007-08-15 14:28:22 +0000769 or a file, it should contain an :rfc:`2822`\ -compliant message, which is read
R. David Murrayb7deff12011-01-30 06:21:28 +0000770 and parsed. Files should be open in binary mode, but text mode files
771 are accepted for backward compatibility.
Georg Brandl116aa622007-08-15 14:28:22 +0000772
Benjamin Petersone41251e2008-04-25 01:59:09 +0000773 The format-specific state and behaviors offered by subclasses vary, but in
774 general it is only the properties that are not specific to a particular
775 mailbox that are supported (although presumably the properties are specific
776 to a particular mailbox format). For example, file offsets for single-file
777 mailbox formats and file names for directory-based mailbox formats are not
778 retained, because they are only applicable to the original mailbox. But state
779 such as whether a message has been read by the user or marked as important is
780 retained, because it applies to the message itself.
Georg Brandl116aa622007-08-15 14:28:22 +0000781
Benjamin Petersone41251e2008-04-25 01:59:09 +0000782 There is no requirement that :class:`Message` instances be used to represent
783 messages retrieved using :class:`Mailbox` instances. In some situations, the
784 time and memory required to generate :class:`Message` representations might
Ezio Melottie130a522011-10-19 10:58:56 +0300785 not be acceptable. For such situations, :class:`Mailbox` instances also
Benjamin Petersone41251e2008-04-25 01:59:09 +0000786 offer string and file-like representations, and a custom message factory may
787 be specified when a :class:`Mailbox` instance is initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000788
789
790.. _mailbox-maildirmessage:
791
792:class:`MaildirMessage`
793^^^^^^^^^^^^^^^^^^^^^^^
794
795
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000796.. class:: MaildirMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000797
798 A message with Maildir-specific behaviors. Parameter *message* has the same
799 meaning as with the :class:`Message` constructor.
800
Benjamin Petersone41251e2008-04-25 01:59:09 +0000801 Typically, a mail user agent application moves all of the messages in the
802 :file:`new` subdirectory to the :file:`cur` subdirectory after the first time
803 the user opens and closes the mailbox, recording that the messages are old
804 whether or not they've actually been read. Each message in :file:`cur` has an
805 "info" section added to its file name to store information about its state.
806 (Some mail readers may also add an "info" section to messages in
807 :file:`new`.) The "info" section may take one of two forms: it may contain
808 "2," followed by a list of standardized flags (e.g., "2,FR") or it may
809 contain "1," followed by so-called experimental information. Standard flags
810 for Maildir messages are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000811
Benjamin Petersone41251e2008-04-25 01:59:09 +0000812 +------+---------+--------------------------------+
813 | Flag | Meaning | Explanation |
814 +======+=========+================================+
815 | D | Draft | Under composition |
816 +------+---------+--------------------------------+
817 | F | Flagged | Marked as important |
818 +------+---------+--------------------------------+
819 | P | Passed | Forwarded, resent, or bounced |
820 +------+---------+--------------------------------+
821 | R | Replied | Replied to |
822 +------+---------+--------------------------------+
823 | S | Seen | Read |
824 +------+---------+--------------------------------+
825 | T | Trashed | Marked for subsequent deletion |
826 +------+---------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000827
Benjamin Petersone41251e2008-04-25 01:59:09 +0000828 :class:`MaildirMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000829
830
Benjamin Petersone41251e2008-04-25 01:59:09 +0000831 .. method:: get_subdir()
Georg Brandl116aa622007-08-15 14:28:22 +0000832
Benjamin Petersone41251e2008-04-25 01:59:09 +0000833 Return either "new" (if the message should be stored in the :file:`new`
834 subdirectory) or "cur" (if the message should be stored in the :file:`cur`
835 subdirectory).
Georg Brandl116aa622007-08-15 14:28:22 +0000836
Benjamin Petersone41251e2008-04-25 01:59:09 +0000837 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000838
Benjamin Petersone41251e2008-04-25 01:59:09 +0000839 A message is typically moved from :file:`new` to :file:`cur` after its
840 mailbox has been accessed, whether or not the message is has been
841 read. A message ``msg`` has been read if ``"S" in msg.get_flags()`` is
842 ``True``.
Georg Brandl116aa622007-08-15 14:28:22 +0000843
844
Benjamin Petersone41251e2008-04-25 01:59:09 +0000845 .. method:: set_subdir(subdir)
Georg Brandl116aa622007-08-15 14:28:22 +0000846
Benjamin Petersone41251e2008-04-25 01:59:09 +0000847 Set the subdirectory the message should be stored in. Parameter *subdir*
848 must be either "new" or "cur".
Georg Brandl116aa622007-08-15 14:28:22 +0000849
850
Benjamin Petersone41251e2008-04-25 01:59:09 +0000851 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +0000852
Benjamin Petersone41251e2008-04-25 01:59:09 +0000853 Return a string specifying the flags that are currently set. If the
854 message complies with the standard Maildir format, the result is the
855 concatenation in alphabetical order of zero or one occurrence of each of
856 ``'D'``, ``'F'``, ``'P'``, ``'R'``, ``'S'``, and ``'T'``. The empty string
857 is returned if no flags are set or if "info" contains experimental
858 semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000859
860
Benjamin Petersone41251e2008-04-25 01:59:09 +0000861 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +0000862
Benjamin Petersone41251e2008-04-25 01:59:09 +0000863 Set the flags specified by *flags* and unset all others.
Georg Brandl116aa622007-08-15 14:28:22 +0000864
865
Benjamin Petersone41251e2008-04-25 01:59:09 +0000866 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000867
Benjamin Petersone41251e2008-04-25 01:59:09 +0000868 Set the flag(s) specified by *flag* without changing other flags. To add
869 more than one flag at a time, *flag* may be a string of more than one
870 character. The current "info" is overwritten whether or not it contains
871 experimental information rather than flags.
Georg Brandl116aa622007-08-15 14:28:22 +0000872
873
Benjamin Petersone41251e2008-04-25 01:59:09 +0000874 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000875
Benjamin Petersone41251e2008-04-25 01:59:09 +0000876 Unset the flag(s) specified by *flag* without changing other flags. To
877 remove more than one flag at a time, *flag* maybe a string of more than
878 one character. If "info" contains experimental information rather than
879 flags, the current "info" is not modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000880
881
Benjamin Petersone41251e2008-04-25 01:59:09 +0000882 .. method:: get_date()
Georg Brandl116aa622007-08-15 14:28:22 +0000883
Benjamin Petersone41251e2008-04-25 01:59:09 +0000884 Return the delivery date of the message as a floating-point number
885 representing seconds since the epoch.
Georg Brandl116aa622007-08-15 14:28:22 +0000886
887
Benjamin Petersone41251e2008-04-25 01:59:09 +0000888 .. method:: set_date(date)
Georg Brandl116aa622007-08-15 14:28:22 +0000889
Benjamin Petersone41251e2008-04-25 01:59:09 +0000890 Set the delivery date of the message to *date*, a floating-point number
891 representing seconds since the epoch.
Georg Brandl116aa622007-08-15 14:28:22 +0000892
893
Benjamin Petersone41251e2008-04-25 01:59:09 +0000894 .. method:: get_info()
Georg Brandl116aa622007-08-15 14:28:22 +0000895
Benjamin Petersone41251e2008-04-25 01:59:09 +0000896 Return a string containing the "info" for a message. This is useful for
897 accessing and modifying "info" that is experimental (i.e., not a list of
898 flags).
Georg Brandl116aa622007-08-15 14:28:22 +0000899
900
Benjamin Petersone41251e2008-04-25 01:59:09 +0000901 .. method:: set_info(info)
Georg Brandl116aa622007-08-15 14:28:22 +0000902
Benjamin Petersone41251e2008-04-25 01:59:09 +0000903 Set "info" to *info*, which should be a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000904
905When a :class:`MaildirMessage` instance is created based upon an
906:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
907and :mailheader:`X-Status` headers are omitted and the following conversions
908take place:
909
910+--------------------+----------------------------------------------+
911| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
912| | state |
913+====================+==============================================+
914| "cur" subdirectory | O flag |
915+--------------------+----------------------------------------------+
916| F flag | F flag |
917+--------------------+----------------------------------------------+
918| R flag | A flag |
919+--------------------+----------------------------------------------+
920| S flag | R flag |
921+--------------------+----------------------------------------------+
922| T flag | D flag |
923+--------------------+----------------------------------------------+
924
925When a :class:`MaildirMessage` instance is created based upon an
926:class:`MHMessage` instance, the following conversions take place:
927
928+-------------------------------+--------------------------+
929| Resulting state | :class:`MHMessage` state |
930+===============================+==========================+
931| "cur" subdirectory | "unseen" sequence |
932+-------------------------------+--------------------------+
933| "cur" subdirectory and S flag | no "unseen" sequence |
934+-------------------------------+--------------------------+
935| F flag | "flagged" sequence |
936+-------------------------------+--------------------------+
937| R flag | "replied" sequence |
938+-------------------------------+--------------------------+
939
940When a :class:`MaildirMessage` instance is created based upon a
941:class:`BabylMessage` instance, the following conversions take place:
942
943+-------------------------------+-------------------------------+
944| Resulting state | :class:`BabylMessage` state |
945+===============================+===============================+
946| "cur" subdirectory | "unseen" label |
947+-------------------------------+-------------------------------+
948| "cur" subdirectory and S flag | no "unseen" label |
949+-------------------------------+-------------------------------+
950| P flag | "forwarded" or "resent" label |
951+-------------------------------+-------------------------------+
952| R flag | "answered" label |
953+-------------------------------+-------------------------------+
954| T flag | "deleted" label |
955+-------------------------------+-------------------------------+
956
957
958.. _mailbox-mboxmessage:
959
960:class:`mboxMessage`
961^^^^^^^^^^^^^^^^^^^^
962
963
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000964.. class:: mboxMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000965
966 A message with mbox-specific behaviors. Parameter *message* has the same meaning
967 as with the :class:`Message` constructor.
968
Benjamin Petersone41251e2008-04-25 01:59:09 +0000969 Messages in an mbox mailbox are stored together in a single file. The
970 sender's envelope address and the time of delivery are typically stored in a
971 line beginning with "From " that is used to indicate the start of a message,
972 though there is considerable variation in the exact format of this data among
973 mbox implementations. Flags that indicate the state of the message, such as
974 whether it has been read or marked as important, are typically stored in
975 :mailheader:`Status` and :mailheader:`X-Status` headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000976
Benjamin Petersone41251e2008-04-25 01:59:09 +0000977 Conventional flags for mbox messages are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000978
Benjamin Petersone41251e2008-04-25 01:59:09 +0000979 +------+----------+--------------------------------+
980 | Flag | Meaning | Explanation |
981 +======+==========+================================+
982 | R | Read | Read |
983 +------+----------+--------------------------------+
984 | O | Old | Previously detected by MUA |
985 +------+----------+--------------------------------+
986 | D | Deleted | Marked for subsequent deletion |
987 +------+----------+--------------------------------+
988 | F | Flagged | Marked as important |
989 +------+----------+--------------------------------+
990 | A | Answered | Replied to |
991 +------+----------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000992
Benjamin Petersone41251e2008-04-25 01:59:09 +0000993 The "R" and "O" flags are stored in the :mailheader:`Status` header, and the
994 "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The
995 flags and headers typically appear in the order mentioned.
Georg Brandl116aa622007-08-15 14:28:22 +0000996
Benjamin Petersone41251e2008-04-25 01:59:09 +0000997 :class:`mboxMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000998
999
Benjamin Petersone41251e2008-04-25 01:59:09 +00001000 .. method:: get_from()
Georg Brandl116aa622007-08-15 14:28:22 +00001001
Benjamin Petersone41251e2008-04-25 01:59:09 +00001002 Return a string representing the "From " line that marks the start of the
1003 message in an mbox mailbox. The leading "From " and the trailing newline
1004 are excluded.
Georg Brandl116aa622007-08-15 14:28:22 +00001005
1006
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001007 .. method:: set_from(from_, time_=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001008
Benjamin Petersone41251e2008-04-25 01:59:09 +00001009 Set the "From " line to *from_*, which should be specified without a
1010 leading "From " or trailing newline. For convenience, *time_* may be
1011 specified and will be formatted appropriately and appended to *from_*. If
Serhiy Storchakabfdcd432013-10-13 23:09:14 +03001012 *time_* is specified, it should be a :class:`time.struct_time` instance, a
Benjamin Petersone41251e2008-04-25 01:59:09 +00001013 tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use
1014 :meth:`time.gmtime`).
Georg Brandl116aa622007-08-15 14:28:22 +00001015
1016
Benjamin Petersone41251e2008-04-25 01:59:09 +00001017 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +00001018
Benjamin Petersone41251e2008-04-25 01:59:09 +00001019 Return a string specifying the flags that are currently set. If the
1020 message complies with the conventional format, the result is the
1021 concatenation in the following order of zero or one occurrence of each of
1022 ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001023
1024
Benjamin Petersone41251e2008-04-25 01:59:09 +00001025 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +00001026
Benjamin Petersone41251e2008-04-25 01:59:09 +00001027 Set the flags specified by *flags* and unset all others. Parameter *flags*
1028 should be the concatenation in any order of zero or more occurrences of
1029 each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001030
1031
Benjamin Petersone41251e2008-04-25 01:59:09 +00001032 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001033
Benjamin Petersone41251e2008-04-25 01:59:09 +00001034 Set the flag(s) specified by *flag* without changing other flags. To add
1035 more than one flag at a time, *flag* may be a string of more than one
1036 character.
Georg Brandl116aa622007-08-15 14:28:22 +00001037
1038
Benjamin Petersone41251e2008-04-25 01:59:09 +00001039 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001040
Benjamin Petersone41251e2008-04-25 01:59:09 +00001041 Unset the flag(s) specified by *flag* without changing other flags. To
1042 remove more than one flag at a time, *flag* maybe a string of more than
1043 one character.
Georg Brandl116aa622007-08-15 14:28:22 +00001044
1045When an :class:`mboxMessage` instance is created based upon a
1046:class:`MaildirMessage` instance, a "From " line is generated based upon the
1047:class:`MaildirMessage` instance's delivery date, and the following conversions
1048take place:
1049
1050+-----------------+-------------------------------+
1051| Resulting state | :class:`MaildirMessage` state |
1052+=================+===============================+
1053| R flag | S flag |
1054+-----------------+-------------------------------+
1055| O flag | "cur" subdirectory |
1056+-----------------+-------------------------------+
1057| D flag | T flag |
1058+-----------------+-------------------------------+
1059| F flag | F flag |
1060+-----------------+-------------------------------+
1061| A flag | R flag |
1062+-----------------+-------------------------------+
1063
1064When an :class:`mboxMessage` instance is created based upon an
1065:class:`MHMessage` instance, the following conversions take place:
1066
1067+-------------------+--------------------------+
1068| Resulting state | :class:`MHMessage` state |
1069+===================+==========================+
1070| R flag and O flag | no "unseen" sequence |
1071+-------------------+--------------------------+
1072| O flag | "unseen" sequence |
1073+-------------------+--------------------------+
1074| F flag | "flagged" sequence |
1075+-------------------+--------------------------+
1076| A flag | "replied" sequence |
1077+-------------------+--------------------------+
1078
1079When an :class:`mboxMessage` instance is created based upon a
1080:class:`BabylMessage` instance, the following conversions take place:
1081
1082+-------------------+-----------------------------+
1083| Resulting state | :class:`BabylMessage` state |
1084+===================+=============================+
1085| R flag and O flag | no "unseen" label |
1086+-------------------+-----------------------------+
1087| O flag | "unseen" label |
1088+-------------------+-----------------------------+
1089| D flag | "deleted" label |
1090+-------------------+-----------------------------+
1091| A flag | "answered" label |
1092+-------------------+-----------------------------+
1093
1094When a :class:`Message` instance is created based upon an :class:`MMDFMessage`
1095instance, the "From " line is copied and all flags directly correspond:
1096
1097+-----------------+----------------------------+
1098| Resulting state | :class:`MMDFMessage` state |
1099+=================+============================+
1100| R flag | R flag |
1101+-----------------+----------------------------+
1102| O flag | O flag |
1103+-----------------+----------------------------+
1104| D flag | D flag |
1105+-----------------+----------------------------+
1106| F flag | F flag |
1107+-----------------+----------------------------+
1108| A flag | A flag |
1109+-----------------+----------------------------+
1110
1111
1112.. _mailbox-mhmessage:
1113
1114:class:`MHMessage`
1115^^^^^^^^^^^^^^^^^^
1116
1117
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001118.. class:: MHMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001119
1120 A message with MH-specific behaviors. Parameter *message* has the same meaning
1121 as with the :class:`Message` constructor.
1122
Benjamin Petersone41251e2008-04-25 01:59:09 +00001123 MH messages do not support marks or flags in the traditional sense, but they
1124 do support sequences, which are logical groupings of arbitrary messages. Some
1125 mail reading programs (although not the standard :program:`mh` and
1126 :program:`nmh`) use sequences in much the same way flags are used with other
1127 formats, as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001128
Benjamin Petersone41251e2008-04-25 01:59:09 +00001129 +----------+------------------------------------------+
1130 | Sequence | Explanation |
1131 +==========+==========================================+
1132 | unseen | Not read, but previously detected by MUA |
1133 +----------+------------------------------------------+
1134 | replied | Replied to |
1135 +----------+------------------------------------------+
1136 | flagged | Marked as important |
1137 +----------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001138
Benjamin Petersone41251e2008-04-25 01:59:09 +00001139 :class:`MHMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001140
1141
Benjamin Petersone41251e2008-04-25 01:59:09 +00001142 .. method:: get_sequences()
Georg Brandl116aa622007-08-15 14:28:22 +00001143
Benjamin Petersone41251e2008-04-25 01:59:09 +00001144 Return a list of the names of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001145
1146
Benjamin Petersone41251e2008-04-25 01:59:09 +00001147 .. method:: set_sequences(sequences)
Georg Brandl116aa622007-08-15 14:28:22 +00001148
Benjamin Petersone41251e2008-04-25 01:59:09 +00001149 Set the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001150
1151
Benjamin Petersone41251e2008-04-25 01:59:09 +00001152 .. method:: add_sequence(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +00001153
Benjamin Petersone41251e2008-04-25 01:59:09 +00001154 Add *sequence* to the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001155
1156
Benjamin Petersone41251e2008-04-25 01:59:09 +00001157 .. method:: remove_sequence(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +00001158
Benjamin Petersone41251e2008-04-25 01:59:09 +00001159 Remove *sequence* from the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001160
1161When an :class:`MHMessage` instance is created based upon a
1162:class:`MaildirMessage` instance, the following conversions take place:
1163
1164+--------------------+-------------------------------+
1165| Resulting state | :class:`MaildirMessage` state |
1166+====================+===============================+
1167| "unseen" sequence | no S flag |
1168+--------------------+-------------------------------+
1169| "replied" sequence | R flag |
1170+--------------------+-------------------------------+
1171| "flagged" sequence | F flag |
1172+--------------------+-------------------------------+
1173
1174When an :class:`MHMessage` instance is created based upon an
1175:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
1176and :mailheader:`X-Status` headers are omitted and the following conversions
1177take place:
1178
1179+--------------------+----------------------------------------------+
1180| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
1181| | state |
1182+====================+==============================================+
1183| "unseen" sequence | no R flag |
1184+--------------------+----------------------------------------------+
1185| "replied" sequence | A flag |
1186+--------------------+----------------------------------------------+
1187| "flagged" sequence | F flag |
1188+--------------------+----------------------------------------------+
1189
1190When an :class:`MHMessage` instance is created based upon a
1191:class:`BabylMessage` instance, the following conversions take place:
1192
1193+--------------------+-----------------------------+
1194| Resulting state | :class:`BabylMessage` state |
1195+====================+=============================+
1196| "unseen" sequence | "unseen" label |
1197+--------------------+-----------------------------+
1198| "replied" sequence | "answered" label |
1199+--------------------+-----------------------------+
1200
1201
1202.. _mailbox-babylmessage:
1203
1204:class:`BabylMessage`
1205^^^^^^^^^^^^^^^^^^^^^
1206
1207
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001208.. class:: BabylMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001209
1210 A message with Babyl-specific behaviors. Parameter *message* has the same
1211 meaning as with the :class:`Message` constructor.
1212
Benjamin Petersone41251e2008-04-25 01:59:09 +00001213 Certain message labels, called :dfn:`attributes`, are defined by convention
1214 to have special meanings. The attributes are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001215
Benjamin Petersone41251e2008-04-25 01:59:09 +00001216 +-----------+------------------------------------------+
1217 | Label | Explanation |
1218 +===========+==========================================+
1219 | unseen | Not read, but previously detected by MUA |
1220 +-----------+------------------------------------------+
1221 | deleted | Marked for subsequent deletion |
1222 +-----------+------------------------------------------+
1223 | filed | Copied to another file or mailbox |
1224 +-----------+------------------------------------------+
1225 | answered | Replied to |
1226 +-----------+------------------------------------------+
1227 | forwarded | Forwarded |
1228 +-----------+------------------------------------------+
1229 | edited | Modified by the user |
1230 +-----------+------------------------------------------+
1231 | resent | Resent |
1232 +-----------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001233
Benjamin Petersone41251e2008-04-25 01:59:09 +00001234 By default, Rmail displays only visible headers. The :class:`BabylMessage`
1235 class, though, uses the original headers because they are more
1236 complete. Visible headers may be accessed explicitly if desired.
Georg Brandl116aa622007-08-15 14:28:22 +00001237
Benjamin Petersone41251e2008-04-25 01:59:09 +00001238 :class:`BabylMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001239
1240
Benjamin Petersone41251e2008-04-25 01:59:09 +00001241 .. method:: get_labels()
Georg Brandl116aa622007-08-15 14:28:22 +00001242
Benjamin Petersone41251e2008-04-25 01:59:09 +00001243 Return a list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001244
1245
Benjamin Petersone41251e2008-04-25 01:59:09 +00001246 .. method:: set_labels(labels)
Georg Brandl116aa622007-08-15 14:28:22 +00001247
Benjamin Petersone41251e2008-04-25 01:59:09 +00001248 Set the list of labels on the message to *labels*.
Georg Brandl116aa622007-08-15 14:28:22 +00001249
1250
Benjamin Petersone41251e2008-04-25 01:59:09 +00001251 .. method:: add_label(label)
Georg Brandl116aa622007-08-15 14:28:22 +00001252
Benjamin Petersone41251e2008-04-25 01:59:09 +00001253 Add *label* to the list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001254
1255
Benjamin Petersone41251e2008-04-25 01:59:09 +00001256 .. method:: remove_label(label)
Georg Brandl116aa622007-08-15 14:28:22 +00001257
Benjamin Petersone41251e2008-04-25 01:59:09 +00001258 Remove *label* from the list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001259
1260
Benjamin Petersone41251e2008-04-25 01:59:09 +00001261 .. method:: get_visible()
Georg Brandl116aa622007-08-15 14:28:22 +00001262
Benjamin Petersone41251e2008-04-25 01:59:09 +00001263 Return an :class:`Message` instance whose headers are the message's
1264 visible headers and whose body is empty.
Georg Brandl116aa622007-08-15 14:28:22 +00001265
1266
Benjamin Petersone41251e2008-04-25 01:59:09 +00001267 .. method:: set_visible(visible)
Georg Brandl116aa622007-08-15 14:28:22 +00001268
Benjamin Petersone41251e2008-04-25 01:59:09 +00001269 Set the message's visible headers to be the same as the headers in
1270 *message*. Parameter *visible* should be a :class:`Message` instance, an
R David Murray53202502012-09-28 15:19:16 -04001271 :class:`email.message.Message` instance, a string, or a file-like object
Benjamin Petersone41251e2008-04-25 01:59:09 +00001272 (which should be open in text mode).
Georg Brandl116aa622007-08-15 14:28:22 +00001273
1274
Benjamin Petersone41251e2008-04-25 01:59:09 +00001275 .. method:: update_visible()
Georg Brandl116aa622007-08-15 14:28:22 +00001276
Benjamin Petersone41251e2008-04-25 01:59:09 +00001277 When a :class:`BabylMessage` instance's original headers are modified, the
1278 visible headers are not automatically modified to correspond. This method
1279 updates the visible headers as follows: each visible header with a
1280 corresponding original header is set to the value of the original header,
1281 each visible header without a corresponding original header is removed,
1282 and any of :mailheader:`Date`, :mailheader:`From`, :mailheader:`Reply-To`,
1283 :mailheader:`To`, :mailheader:`CC`, and :mailheader:`Subject` that are
1284 present in the original headers but not the visible headers are added to
1285 the visible headers.
Georg Brandl116aa622007-08-15 14:28:22 +00001286
1287When a :class:`BabylMessage` instance is created based upon a
1288:class:`MaildirMessage` instance, the following conversions take place:
1289
1290+-------------------+-------------------------------+
1291| Resulting state | :class:`MaildirMessage` state |
1292+===================+===============================+
1293| "unseen" label | no S flag |
1294+-------------------+-------------------------------+
1295| "deleted" label | T flag |
1296+-------------------+-------------------------------+
1297| "answered" label | R flag |
1298+-------------------+-------------------------------+
1299| "forwarded" label | P flag |
1300+-------------------+-------------------------------+
1301
1302When a :class:`BabylMessage` instance is created based upon an
1303:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
1304and :mailheader:`X-Status` headers are omitted and the following conversions
1305take place:
1306
1307+------------------+----------------------------------------------+
1308| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
1309| | state |
1310+==================+==============================================+
1311| "unseen" label | no R flag |
1312+------------------+----------------------------------------------+
1313| "deleted" label | D flag |
1314+------------------+----------------------------------------------+
1315| "answered" label | A flag |
1316+------------------+----------------------------------------------+
1317
1318When a :class:`BabylMessage` instance is created based upon an
1319:class:`MHMessage` instance, the following conversions take place:
1320
1321+------------------+--------------------------+
1322| Resulting state | :class:`MHMessage` state |
1323+==================+==========================+
1324| "unseen" label | "unseen" sequence |
1325+------------------+--------------------------+
1326| "answered" label | "replied" sequence |
1327+------------------+--------------------------+
1328
1329
1330.. _mailbox-mmdfmessage:
1331
1332:class:`MMDFMessage`
1333^^^^^^^^^^^^^^^^^^^^
1334
1335
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001336.. class:: MMDFMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001337
1338 A message with MMDF-specific behaviors. Parameter *message* has the same meaning
1339 as with the :class:`Message` constructor.
1340
Benjamin Petersone41251e2008-04-25 01:59:09 +00001341 As with message in an mbox mailbox, MMDF messages are stored with the
1342 sender's address and the delivery date in an initial line beginning with
1343 "From ". Likewise, flags that indicate the state of the message are
1344 typically stored in :mailheader:`Status` and :mailheader:`X-Status` headers.
Georg Brandl116aa622007-08-15 14:28:22 +00001345
Benjamin Petersone41251e2008-04-25 01:59:09 +00001346 Conventional flags for MMDF messages are identical to those of mbox message
1347 and are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001348
Benjamin Petersone41251e2008-04-25 01:59:09 +00001349 +------+----------+--------------------------------+
1350 | Flag | Meaning | Explanation |
1351 +======+==========+================================+
1352 | R | Read | Read |
1353 +------+----------+--------------------------------+
1354 | O | Old | Previously detected by MUA |
1355 +------+----------+--------------------------------+
1356 | D | Deleted | Marked for subsequent deletion |
1357 +------+----------+--------------------------------+
1358 | F | Flagged | Marked as important |
1359 +------+----------+--------------------------------+
1360 | A | Answered | Replied to |
1361 +------+----------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001362
Benjamin Petersone41251e2008-04-25 01:59:09 +00001363 The "R" and "O" flags are stored in the :mailheader:`Status` header, and the
1364 "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The
1365 flags and headers typically appear in the order mentioned.
Georg Brandl116aa622007-08-15 14:28:22 +00001366
Benjamin Petersone41251e2008-04-25 01:59:09 +00001367 :class:`MMDFMessage` instances offer the following methods, which are
1368 identical to those offered by :class:`mboxMessage`:
Georg Brandl116aa622007-08-15 14:28:22 +00001369
1370
Benjamin Petersone41251e2008-04-25 01:59:09 +00001371 .. method:: get_from()
Georg Brandl116aa622007-08-15 14:28:22 +00001372
Benjamin Petersone41251e2008-04-25 01:59:09 +00001373 Return a string representing the "From " line that marks the start of the
1374 message in an mbox mailbox. The leading "From " and the trailing newline
1375 are excluded.
Georg Brandl116aa622007-08-15 14:28:22 +00001376
1377
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001378 .. method:: set_from(from_, time_=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001379
Benjamin Petersone41251e2008-04-25 01:59:09 +00001380 Set the "From " line to *from_*, which should be specified without a
1381 leading "From " or trailing newline. For convenience, *time_* may be
1382 specified and will be formatted appropriately and appended to *from_*. If
Serhiy Storchakabfdcd432013-10-13 23:09:14 +03001383 *time_* is specified, it should be a :class:`time.struct_time` instance, a
Benjamin Petersone41251e2008-04-25 01:59:09 +00001384 tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use
1385 :meth:`time.gmtime`).
Georg Brandl116aa622007-08-15 14:28:22 +00001386
1387
Benjamin Petersone41251e2008-04-25 01:59:09 +00001388 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +00001389
Benjamin Petersone41251e2008-04-25 01:59:09 +00001390 Return a string specifying the flags that are currently set. If the
1391 message complies with the conventional format, the result is the
1392 concatenation in the following order of zero or one occurrence of each of
1393 ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001394
1395
Benjamin Petersone41251e2008-04-25 01:59:09 +00001396 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +00001397
Benjamin Petersone41251e2008-04-25 01:59:09 +00001398 Set the flags specified by *flags* and unset all others. Parameter *flags*
1399 should be the concatenation in any order of zero or more occurrences of
1400 each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001401
1402
Benjamin Petersone41251e2008-04-25 01:59:09 +00001403 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001404
Benjamin Petersone41251e2008-04-25 01:59:09 +00001405 Set the flag(s) specified by *flag* without changing other flags. To add
1406 more than one flag at a time, *flag* may be a string of more than one
1407 character.
Georg Brandl116aa622007-08-15 14:28:22 +00001408
1409
Benjamin Petersone41251e2008-04-25 01:59:09 +00001410 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001411
Benjamin Petersone41251e2008-04-25 01:59:09 +00001412 Unset the flag(s) specified by *flag* without changing other flags. To
1413 remove more than one flag at a time, *flag* maybe a string of more than
1414 one character.
Georg Brandl116aa622007-08-15 14:28:22 +00001415
1416When an :class:`MMDFMessage` instance is created based upon a
1417:class:`MaildirMessage` instance, a "From " line is generated based upon the
1418:class:`MaildirMessage` instance's delivery date, and the following conversions
1419take place:
1420
1421+-----------------+-------------------------------+
1422| Resulting state | :class:`MaildirMessage` state |
1423+=================+===============================+
1424| R flag | S flag |
1425+-----------------+-------------------------------+
1426| O flag | "cur" subdirectory |
1427+-----------------+-------------------------------+
1428| D flag | T flag |
1429+-----------------+-------------------------------+
1430| F flag | F flag |
1431+-----------------+-------------------------------+
1432| A flag | R flag |
1433+-----------------+-------------------------------+
1434
1435When an :class:`MMDFMessage` instance is created based upon an
1436:class:`MHMessage` instance, the following conversions take place:
1437
1438+-------------------+--------------------------+
1439| Resulting state | :class:`MHMessage` state |
1440+===================+==========================+
1441| R flag and O flag | no "unseen" sequence |
1442+-------------------+--------------------------+
1443| O flag | "unseen" sequence |
1444+-------------------+--------------------------+
1445| F flag | "flagged" sequence |
1446+-------------------+--------------------------+
1447| A flag | "replied" sequence |
1448+-------------------+--------------------------+
1449
1450When an :class:`MMDFMessage` instance is created based upon a
1451:class:`BabylMessage` instance, the following conversions take place:
1452
1453+-------------------+-----------------------------+
1454| Resulting state | :class:`BabylMessage` state |
1455+===================+=============================+
1456| R flag and O flag | no "unseen" label |
1457+-------------------+-----------------------------+
1458| O flag | "unseen" label |
1459+-------------------+-----------------------------+
1460| D flag | "deleted" label |
1461+-------------------+-----------------------------+
1462| A flag | "answered" label |
1463+-------------------+-----------------------------+
1464
1465When an :class:`MMDFMessage` instance is created based upon an
1466:class:`mboxMessage` instance, the "From " line is copied and all flags directly
1467correspond:
1468
1469+-----------------+----------------------------+
1470| Resulting state | :class:`mboxMessage` state |
1471+=================+============================+
1472| R flag | R flag |
1473+-----------------+----------------------------+
1474| O flag | O flag |
1475+-----------------+----------------------------+
1476| D flag | D flag |
1477+-----------------+----------------------------+
1478| F flag | F flag |
1479+-----------------+----------------------------+
1480| A flag | A flag |
1481+-----------------+----------------------------+
1482
1483
1484Exceptions
1485----------
1486
1487The following exception classes are defined in the :mod:`mailbox` module:
1488
1489
Benjamin Petersone41251e2008-04-25 01:59:09 +00001490.. exception:: Error()
Georg Brandl116aa622007-08-15 14:28:22 +00001491
1492 The based class for all other module-specific exceptions.
1493
1494
Benjamin Petersone41251e2008-04-25 01:59:09 +00001495.. exception:: NoSuchMailboxError()
Georg Brandl116aa622007-08-15 14:28:22 +00001496
1497 Raised when a mailbox is expected but is not found, such as when instantiating a
1498 :class:`Mailbox` subclass with a path that does not exist (and with the *create*
1499 parameter set to ``False``), or when opening a folder that does not exist.
1500
1501
Georg Brandl734e2682008-08-12 08:18:18 +00001502.. exception:: NotEmptyError()
Georg Brandl116aa622007-08-15 14:28:22 +00001503
1504 Raised when a mailbox is not empty but is expected to be, such as when deleting
1505 a folder that contains messages.
1506
1507
Benjamin Petersone41251e2008-04-25 01:59:09 +00001508.. exception:: ExternalClashError()
Georg Brandl116aa622007-08-15 14:28:22 +00001509
1510 Raised when some mailbox-related condition beyond the control of the program
1511 causes it to be unable to proceed, such as when failing to acquire a lock that
1512 another program already holds a lock, or when a uniquely-generated file name
1513 already exists.
1514
1515
Benjamin Petersone41251e2008-04-25 01:59:09 +00001516.. exception:: FormatError()
Georg Brandl116aa622007-08-15 14:28:22 +00001517
1518 Raised when the data in a file cannot be parsed, such as when an :class:`MH`
1519 instance attempts to read a corrupted :file:`.mh_sequences` file.
1520
1521
Georg Brandl116aa622007-08-15 14:28:22 +00001522.. _mailbox-examples:
1523
1524Examples
1525--------
1526
1527A simple example of printing the subjects of all messages in a mailbox that seem
1528interesting::
1529
1530 import mailbox
1531 for message in mailbox.mbox('~/mbox'):
1532 subject = message['subject'] # Could possibly be None.
1533 if subject and 'python' in subject.lower():
Georg Brandl6911e3c2007-09-04 07:15:32 +00001534 print(subject)
Georg Brandl116aa622007-08-15 14:28:22 +00001535
1536To copy all mail from a Babyl mailbox to an MH mailbox, converting all of the
1537format-specific information that can be converted::
1538
1539 import mailbox
1540 destination = mailbox.MH('~/Mail')
1541 destination.lock()
1542 for message in mailbox.Babyl('~/RMAIL'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001543 destination.add(mailbox.MHMessage(message))
Georg Brandl116aa622007-08-15 14:28:22 +00001544 destination.flush()
1545 destination.unlock()
1546
1547This example sorts mail from several mailing lists into different mailboxes,
1548being careful to avoid mail corruption due to concurrent modification by other
1549programs, mail loss due to interruption of the program, or premature termination
1550due to malformed messages in the mailbox::
1551
1552 import mailbox
Ezio Melotti956040a2013-12-14 12:42:29 +02001553 import email.errors
Georg Brandl116aa622007-08-15 14:28:22 +00001554
1555 list_names = ('python-list', 'python-dev', 'python-bugs')
1556
Georg Brandlf6945182008-02-01 11:56:49 +00001557 boxes = {name: mailbox.mbox('~/email/%s' % name) for name in list_names}
Georg Brandl116aa622007-08-15 14:28:22 +00001558 inbox = mailbox.Maildir('~/Maildir', factory=None)
1559
1560 for key in inbox.iterkeys():
1561 try:
1562 message = inbox[key]
Ezio Melotti956040a2013-12-14 12:42:29 +02001563 except email.errors.MessageParseError:
Georg Brandl116aa622007-08-15 14:28:22 +00001564 continue # The message is malformed. Just leave it.
1565
1566 for name in list_names:
1567 list_id = message['list-id']
1568 if list_id and name in list_id:
1569 # Get mailbox to use
1570 box = boxes[name]
1571
1572 # Write copy to disk before removing original.
1573 # If there's a crash, you might duplicate a message, but
1574 # that's better than losing a message completely.
1575 box.lock()
1576 box.add(message)
Georg Brandl48310cd2009-01-03 21:18:54 +00001577 box.flush()
Georg Brandl116aa622007-08-15 14:28:22 +00001578 box.unlock()
1579
1580 # Remove original message
1581 inbox.lock()
1582 inbox.discard(key)
1583 inbox.flush()
1584 inbox.unlock()
1585 break # Found destination, so stop looking.
1586
1587 for box in boxes.itervalues():
1588 box.close()
1589