blob: 8623630aaaa8290ff22c1471db02ad15adc5ab51 [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
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 Murrayb7deff12011-01-30 06:21:28 +0000115 instance, an :class:`email.Message.Message` instance, a string, a byte
116 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
677 headers and body are copied together into a :class:`StringIO` instance
678 (from the :mod:`StringIO` module), which has an API identical to that of a
679 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
760 A subclass of the :mod:`email.Message` module's :class:`Message`. Subclasses of
761 :class:`mailbox.Message` add mailbox-format-specific state and behavior.
762
763 If *message* is omitted, the new instance is created in a default, empty state.
764 If *message* is an :class:`email.Message.Message` instance, its contents are
765 copied; furthermore, any format-specific information is converted insofar as
R. David Murrayb7deff12011-01-30 06:21:28 +0000766 possible if *message* is a :class:`Message` instance. If *message* is a string,
767 a byte string,
Georg Brandl116aa622007-08-15 14:28:22 +0000768 or a file, it should contain an :rfc:`2822`\ -compliant message, which is read
R. David Murrayb7deff12011-01-30 06:21:28 +0000769 and parsed. Files should be open in binary mode, but text mode files
770 are accepted for backward compatibility.
Georg Brandl116aa622007-08-15 14:28:22 +0000771
Benjamin Petersone41251e2008-04-25 01:59:09 +0000772 The format-specific state and behaviors offered by subclasses vary, but in
773 general it is only the properties that are not specific to a particular
774 mailbox that are supported (although presumably the properties are specific
775 to a particular mailbox format). For example, file offsets for single-file
776 mailbox formats and file names for directory-based mailbox formats are not
777 retained, because they are only applicable to the original mailbox. But state
778 such as whether a message has been read by the user or marked as important is
779 retained, because it applies to the message itself.
Georg Brandl116aa622007-08-15 14:28:22 +0000780
Benjamin Petersone41251e2008-04-25 01:59:09 +0000781 There is no requirement that :class:`Message` instances be used to represent
782 messages retrieved using :class:`Mailbox` instances. In some situations, the
783 time and memory required to generate :class:`Message` representations might
Ezio Melottie130a522011-10-19 10:58:56 +0300784 not be acceptable. For such situations, :class:`Mailbox` instances also
Benjamin Petersone41251e2008-04-25 01:59:09 +0000785 offer string and file-like representations, and a custom message factory may
786 be specified when a :class:`Mailbox` instance is initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000787
788
789.. _mailbox-maildirmessage:
790
791:class:`MaildirMessage`
792^^^^^^^^^^^^^^^^^^^^^^^
793
794
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000795.. class:: MaildirMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000796
797 A message with Maildir-specific behaviors. Parameter *message* has the same
798 meaning as with the :class:`Message` constructor.
799
Benjamin Petersone41251e2008-04-25 01:59:09 +0000800 Typically, a mail user agent application moves all of the messages in the
801 :file:`new` subdirectory to the :file:`cur` subdirectory after the first time
802 the user opens and closes the mailbox, recording that the messages are old
803 whether or not they've actually been read. Each message in :file:`cur` has an
804 "info" section added to its file name to store information about its state.
805 (Some mail readers may also add an "info" section to messages in
806 :file:`new`.) The "info" section may take one of two forms: it may contain
807 "2," followed by a list of standardized flags (e.g., "2,FR") or it may
808 contain "1," followed by so-called experimental information. Standard flags
809 for Maildir messages are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000810
Benjamin Petersone41251e2008-04-25 01:59:09 +0000811 +------+---------+--------------------------------+
812 | Flag | Meaning | Explanation |
813 +======+=========+================================+
814 | D | Draft | Under composition |
815 +------+---------+--------------------------------+
816 | F | Flagged | Marked as important |
817 +------+---------+--------------------------------+
818 | P | Passed | Forwarded, resent, or bounced |
819 +------+---------+--------------------------------+
820 | R | Replied | Replied to |
821 +------+---------+--------------------------------+
822 | S | Seen | Read |
823 +------+---------+--------------------------------+
824 | T | Trashed | Marked for subsequent deletion |
825 +------+---------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000826
Benjamin Petersone41251e2008-04-25 01:59:09 +0000827 :class:`MaildirMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000828
829
Benjamin Petersone41251e2008-04-25 01:59:09 +0000830 .. method:: get_subdir()
Georg Brandl116aa622007-08-15 14:28:22 +0000831
Benjamin Petersone41251e2008-04-25 01:59:09 +0000832 Return either "new" (if the message should be stored in the :file:`new`
833 subdirectory) or "cur" (if the message should be stored in the :file:`cur`
834 subdirectory).
Georg Brandl116aa622007-08-15 14:28:22 +0000835
Benjamin Petersone41251e2008-04-25 01:59:09 +0000836 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000837
Benjamin Petersone41251e2008-04-25 01:59:09 +0000838 A message is typically moved from :file:`new` to :file:`cur` after its
839 mailbox has been accessed, whether or not the message is has been
840 read. A message ``msg`` has been read if ``"S" in msg.get_flags()`` is
841 ``True``.
Georg Brandl116aa622007-08-15 14:28:22 +0000842
843
Benjamin Petersone41251e2008-04-25 01:59:09 +0000844 .. method:: set_subdir(subdir)
Georg Brandl116aa622007-08-15 14:28:22 +0000845
Benjamin Petersone41251e2008-04-25 01:59:09 +0000846 Set the subdirectory the message should be stored in. Parameter *subdir*
847 must be either "new" or "cur".
Georg Brandl116aa622007-08-15 14:28:22 +0000848
849
Benjamin Petersone41251e2008-04-25 01:59:09 +0000850 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +0000851
Benjamin Petersone41251e2008-04-25 01:59:09 +0000852 Return a string specifying the flags that are currently set. If the
853 message complies with the standard Maildir format, the result is the
854 concatenation in alphabetical order of zero or one occurrence of each of
855 ``'D'``, ``'F'``, ``'P'``, ``'R'``, ``'S'``, and ``'T'``. The empty string
856 is returned if no flags are set or if "info" contains experimental
857 semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000858
859
Benjamin Petersone41251e2008-04-25 01:59:09 +0000860 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +0000861
Benjamin Petersone41251e2008-04-25 01:59:09 +0000862 Set the flags specified by *flags* and unset all others.
Georg Brandl116aa622007-08-15 14:28:22 +0000863
864
Benjamin Petersone41251e2008-04-25 01:59:09 +0000865 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000866
Benjamin Petersone41251e2008-04-25 01:59:09 +0000867 Set the flag(s) specified by *flag* without changing other flags. To add
868 more than one flag at a time, *flag* may be a string of more than one
869 character. The current "info" is overwritten whether or not it contains
870 experimental information rather than flags.
Georg Brandl116aa622007-08-15 14:28:22 +0000871
872
Benjamin Petersone41251e2008-04-25 01:59:09 +0000873 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000874
Benjamin Petersone41251e2008-04-25 01:59:09 +0000875 Unset the flag(s) specified by *flag* without changing other flags. To
876 remove more than one flag at a time, *flag* maybe a string of more than
877 one character. If "info" contains experimental information rather than
878 flags, the current "info" is not modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000879
880
Benjamin Petersone41251e2008-04-25 01:59:09 +0000881 .. method:: get_date()
Georg Brandl116aa622007-08-15 14:28:22 +0000882
Benjamin Petersone41251e2008-04-25 01:59:09 +0000883 Return the delivery date of the message as a floating-point number
884 representing seconds since the epoch.
Georg Brandl116aa622007-08-15 14:28:22 +0000885
886
Benjamin Petersone41251e2008-04-25 01:59:09 +0000887 .. method:: set_date(date)
Georg Brandl116aa622007-08-15 14:28:22 +0000888
Benjamin Petersone41251e2008-04-25 01:59:09 +0000889 Set the delivery date of the message to *date*, a floating-point number
890 representing seconds since the epoch.
Georg Brandl116aa622007-08-15 14:28:22 +0000891
892
Benjamin Petersone41251e2008-04-25 01:59:09 +0000893 .. method:: get_info()
Georg Brandl116aa622007-08-15 14:28:22 +0000894
Benjamin Petersone41251e2008-04-25 01:59:09 +0000895 Return a string containing the "info" for a message. This is useful for
896 accessing and modifying "info" that is experimental (i.e., not a list of
897 flags).
Georg Brandl116aa622007-08-15 14:28:22 +0000898
899
Benjamin Petersone41251e2008-04-25 01:59:09 +0000900 .. method:: set_info(info)
Georg Brandl116aa622007-08-15 14:28:22 +0000901
Benjamin Petersone41251e2008-04-25 01:59:09 +0000902 Set "info" to *info*, which should be a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000903
904When a :class:`MaildirMessage` instance is created based upon an
905:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
906and :mailheader:`X-Status` headers are omitted and the following conversions
907take place:
908
909+--------------------+----------------------------------------------+
910| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
911| | state |
912+====================+==============================================+
913| "cur" subdirectory | O flag |
914+--------------------+----------------------------------------------+
915| F flag | F flag |
916+--------------------+----------------------------------------------+
917| R flag | A flag |
918+--------------------+----------------------------------------------+
919| S flag | R flag |
920+--------------------+----------------------------------------------+
921| T flag | D flag |
922+--------------------+----------------------------------------------+
923
924When a :class:`MaildirMessage` instance is created based upon an
925:class:`MHMessage` instance, the following conversions take place:
926
927+-------------------------------+--------------------------+
928| Resulting state | :class:`MHMessage` state |
929+===============================+==========================+
930| "cur" subdirectory | "unseen" sequence |
931+-------------------------------+--------------------------+
932| "cur" subdirectory and S flag | no "unseen" sequence |
933+-------------------------------+--------------------------+
934| F flag | "flagged" sequence |
935+-------------------------------+--------------------------+
936| R flag | "replied" sequence |
937+-------------------------------+--------------------------+
938
939When a :class:`MaildirMessage` instance is created based upon a
940:class:`BabylMessage` instance, the following conversions take place:
941
942+-------------------------------+-------------------------------+
943| Resulting state | :class:`BabylMessage` state |
944+===============================+===============================+
945| "cur" subdirectory | "unseen" label |
946+-------------------------------+-------------------------------+
947| "cur" subdirectory and S flag | no "unseen" label |
948+-------------------------------+-------------------------------+
949| P flag | "forwarded" or "resent" label |
950+-------------------------------+-------------------------------+
951| R flag | "answered" label |
952+-------------------------------+-------------------------------+
953| T flag | "deleted" label |
954+-------------------------------+-------------------------------+
955
956
957.. _mailbox-mboxmessage:
958
959:class:`mboxMessage`
960^^^^^^^^^^^^^^^^^^^^
961
962
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000963.. class:: mboxMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000964
965 A message with mbox-specific behaviors. Parameter *message* has the same meaning
966 as with the :class:`Message` constructor.
967
Benjamin Petersone41251e2008-04-25 01:59:09 +0000968 Messages in an mbox mailbox are stored together in a single file. The
969 sender's envelope address and the time of delivery are typically stored in a
970 line beginning with "From " that is used to indicate the start of a message,
971 though there is considerable variation in the exact format of this data among
972 mbox implementations. Flags that indicate the state of the message, such as
973 whether it has been read or marked as important, are typically stored in
974 :mailheader:`Status` and :mailheader:`X-Status` headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000975
Benjamin Petersone41251e2008-04-25 01:59:09 +0000976 Conventional flags for mbox messages are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000977
Benjamin Petersone41251e2008-04-25 01:59:09 +0000978 +------+----------+--------------------------------+
979 | Flag | Meaning | Explanation |
980 +======+==========+================================+
981 | R | Read | Read |
982 +------+----------+--------------------------------+
983 | O | Old | Previously detected by MUA |
984 +------+----------+--------------------------------+
985 | D | Deleted | Marked for subsequent deletion |
986 +------+----------+--------------------------------+
987 | F | Flagged | Marked as important |
988 +------+----------+--------------------------------+
989 | A | Answered | Replied to |
990 +------+----------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000991
Benjamin Petersone41251e2008-04-25 01:59:09 +0000992 The "R" and "O" flags are stored in the :mailheader:`Status` header, and the
993 "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The
994 flags and headers typically appear in the order mentioned.
Georg Brandl116aa622007-08-15 14:28:22 +0000995
Benjamin Petersone41251e2008-04-25 01:59:09 +0000996 :class:`mboxMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000997
998
Benjamin Petersone41251e2008-04-25 01:59:09 +0000999 .. method:: get_from()
Georg Brandl116aa622007-08-15 14:28:22 +00001000
Benjamin Petersone41251e2008-04-25 01:59:09 +00001001 Return a string representing the "From " line that marks the start of the
1002 message in an mbox mailbox. The leading "From " and the trailing newline
1003 are excluded.
Georg Brandl116aa622007-08-15 14:28:22 +00001004
1005
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001006 .. method:: set_from(from_, time_=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001007
Benjamin Petersone41251e2008-04-25 01:59:09 +00001008 Set the "From " line to *from_*, which should be specified without a
1009 leading "From " or trailing newline. For convenience, *time_* may be
1010 specified and will be formatted appropriately and appended to *from_*. If
1011 *time_* is specified, it should be a :class:`struct_time` instance, a
1012 tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use
1013 :meth:`time.gmtime`).
Georg Brandl116aa622007-08-15 14:28:22 +00001014
1015
Benjamin Petersone41251e2008-04-25 01:59:09 +00001016 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +00001017
Benjamin Petersone41251e2008-04-25 01:59:09 +00001018 Return a string specifying the flags that are currently set. If the
1019 message complies with the conventional format, the result is the
1020 concatenation in the following order of zero or one occurrence of each of
1021 ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001022
1023
Benjamin Petersone41251e2008-04-25 01:59:09 +00001024 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +00001025
Benjamin Petersone41251e2008-04-25 01:59:09 +00001026 Set the flags specified by *flags* and unset all others. Parameter *flags*
1027 should be the concatenation in any order of zero or more occurrences of
1028 each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001029
1030
Benjamin Petersone41251e2008-04-25 01:59:09 +00001031 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001032
Benjamin Petersone41251e2008-04-25 01:59:09 +00001033 Set the flag(s) specified by *flag* without changing other flags. To add
1034 more than one flag at a time, *flag* may be a string of more than one
1035 character.
Georg Brandl116aa622007-08-15 14:28:22 +00001036
1037
Benjamin Petersone41251e2008-04-25 01:59:09 +00001038 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001039
Benjamin Petersone41251e2008-04-25 01:59:09 +00001040 Unset the flag(s) specified by *flag* without changing other flags. To
1041 remove more than one flag at a time, *flag* maybe a string of more than
1042 one character.
Georg Brandl116aa622007-08-15 14:28:22 +00001043
1044When an :class:`mboxMessage` instance is created based upon a
1045:class:`MaildirMessage` instance, a "From " line is generated based upon the
1046:class:`MaildirMessage` instance's delivery date, and the following conversions
1047take place:
1048
1049+-----------------+-------------------------------+
1050| Resulting state | :class:`MaildirMessage` state |
1051+=================+===============================+
1052| R flag | S flag |
1053+-----------------+-------------------------------+
1054| O flag | "cur" subdirectory |
1055+-----------------+-------------------------------+
1056| D flag | T flag |
1057+-----------------+-------------------------------+
1058| F flag | F flag |
1059+-----------------+-------------------------------+
1060| A flag | R flag |
1061+-----------------+-------------------------------+
1062
1063When an :class:`mboxMessage` instance is created based upon an
1064:class:`MHMessage` instance, the following conversions take place:
1065
1066+-------------------+--------------------------+
1067| Resulting state | :class:`MHMessage` state |
1068+===================+==========================+
1069| R flag and O flag | no "unseen" sequence |
1070+-------------------+--------------------------+
1071| O flag | "unseen" sequence |
1072+-------------------+--------------------------+
1073| F flag | "flagged" sequence |
1074+-------------------+--------------------------+
1075| A flag | "replied" sequence |
1076+-------------------+--------------------------+
1077
1078When an :class:`mboxMessage` instance is created based upon a
1079:class:`BabylMessage` instance, the following conversions take place:
1080
1081+-------------------+-----------------------------+
1082| Resulting state | :class:`BabylMessage` state |
1083+===================+=============================+
1084| R flag and O flag | no "unseen" label |
1085+-------------------+-----------------------------+
1086| O flag | "unseen" label |
1087+-------------------+-----------------------------+
1088| D flag | "deleted" label |
1089+-------------------+-----------------------------+
1090| A flag | "answered" label |
1091+-------------------+-----------------------------+
1092
1093When a :class:`Message` instance is created based upon an :class:`MMDFMessage`
1094instance, the "From " line is copied and all flags directly correspond:
1095
1096+-----------------+----------------------------+
1097| Resulting state | :class:`MMDFMessage` state |
1098+=================+============================+
1099| R flag | R flag |
1100+-----------------+----------------------------+
1101| O flag | O flag |
1102+-----------------+----------------------------+
1103| D flag | D flag |
1104+-----------------+----------------------------+
1105| F flag | F flag |
1106+-----------------+----------------------------+
1107| A flag | A flag |
1108+-----------------+----------------------------+
1109
1110
1111.. _mailbox-mhmessage:
1112
1113:class:`MHMessage`
1114^^^^^^^^^^^^^^^^^^
1115
1116
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001117.. class:: MHMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001118
1119 A message with MH-specific behaviors. Parameter *message* has the same meaning
1120 as with the :class:`Message` constructor.
1121
Benjamin Petersone41251e2008-04-25 01:59:09 +00001122 MH messages do not support marks or flags in the traditional sense, but they
1123 do support sequences, which are logical groupings of arbitrary messages. Some
1124 mail reading programs (although not the standard :program:`mh` and
1125 :program:`nmh`) use sequences in much the same way flags are used with other
1126 formats, as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001127
Benjamin Petersone41251e2008-04-25 01:59:09 +00001128 +----------+------------------------------------------+
1129 | Sequence | Explanation |
1130 +==========+==========================================+
1131 | unseen | Not read, but previously detected by MUA |
1132 +----------+------------------------------------------+
1133 | replied | Replied to |
1134 +----------+------------------------------------------+
1135 | flagged | Marked as important |
1136 +----------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001137
Benjamin Petersone41251e2008-04-25 01:59:09 +00001138 :class:`MHMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001139
1140
Benjamin Petersone41251e2008-04-25 01:59:09 +00001141 .. method:: get_sequences()
Georg Brandl116aa622007-08-15 14:28:22 +00001142
Benjamin Petersone41251e2008-04-25 01:59:09 +00001143 Return a list of the names of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001144
1145
Benjamin Petersone41251e2008-04-25 01:59:09 +00001146 .. method:: set_sequences(sequences)
Georg Brandl116aa622007-08-15 14:28:22 +00001147
Benjamin Petersone41251e2008-04-25 01:59:09 +00001148 Set the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001149
1150
Benjamin Petersone41251e2008-04-25 01:59:09 +00001151 .. method:: add_sequence(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +00001152
Benjamin Petersone41251e2008-04-25 01:59:09 +00001153 Add *sequence* to the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001154
1155
Benjamin Petersone41251e2008-04-25 01:59:09 +00001156 .. method:: remove_sequence(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +00001157
Benjamin Petersone41251e2008-04-25 01:59:09 +00001158 Remove *sequence* from the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001159
1160When an :class:`MHMessage` instance is created based upon a
1161:class:`MaildirMessage` instance, the following conversions take place:
1162
1163+--------------------+-------------------------------+
1164| Resulting state | :class:`MaildirMessage` state |
1165+====================+===============================+
1166| "unseen" sequence | no S flag |
1167+--------------------+-------------------------------+
1168| "replied" sequence | R flag |
1169+--------------------+-------------------------------+
1170| "flagged" sequence | F flag |
1171+--------------------+-------------------------------+
1172
1173When an :class:`MHMessage` instance is created based upon an
1174:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
1175and :mailheader:`X-Status` headers are omitted and the following conversions
1176take place:
1177
1178+--------------------+----------------------------------------------+
1179| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
1180| | state |
1181+====================+==============================================+
1182| "unseen" sequence | no R flag |
1183+--------------------+----------------------------------------------+
1184| "replied" sequence | A flag |
1185+--------------------+----------------------------------------------+
1186| "flagged" sequence | F flag |
1187+--------------------+----------------------------------------------+
1188
1189When an :class:`MHMessage` instance is created based upon a
1190:class:`BabylMessage` instance, the following conversions take place:
1191
1192+--------------------+-----------------------------+
1193| Resulting state | :class:`BabylMessage` state |
1194+====================+=============================+
1195| "unseen" sequence | "unseen" label |
1196+--------------------+-----------------------------+
1197| "replied" sequence | "answered" label |
1198+--------------------+-----------------------------+
1199
1200
1201.. _mailbox-babylmessage:
1202
1203:class:`BabylMessage`
1204^^^^^^^^^^^^^^^^^^^^^
1205
1206
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001207.. class:: BabylMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001208
1209 A message with Babyl-specific behaviors. Parameter *message* has the same
1210 meaning as with the :class:`Message` constructor.
1211
Benjamin Petersone41251e2008-04-25 01:59:09 +00001212 Certain message labels, called :dfn:`attributes`, are defined by convention
1213 to have special meanings. The attributes are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001214
Benjamin Petersone41251e2008-04-25 01:59:09 +00001215 +-----------+------------------------------------------+
1216 | Label | Explanation |
1217 +===========+==========================================+
1218 | unseen | Not read, but previously detected by MUA |
1219 +-----------+------------------------------------------+
1220 | deleted | Marked for subsequent deletion |
1221 +-----------+------------------------------------------+
1222 | filed | Copied to another file or mailbox |
1223 +-----------+------------------------------------------+
1224 | answered | Replied to |
1225 +-----------+------------------------------------------+
1226 | forwarded | Forwarded |
1227 +-----------+------------------------------------------+
1228 | edited | Modified by the user |
1229 +-----------+------------------------------------------+
1230 | resent | Resent |
1231 +-----------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001232
Benjamin Petersone41251e2008-04-25 01:59:09 +00001233 By default, Rmail displays only visible headers. The :class:`BabylMessage`
1234 class, though, uses the original headers because they are more
1235 complete. Visible headers may be accessed explicitly if desired.
Georg Brandl116aa622007-08-15 14:28:22 +00001236
Benjamin Petersone41251e2008-04-25 01:59:09 +00001237 :class:`BabylMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001238
1239
Benjamin Petersone41251e2008-04-25 01:59:09 +00001240 .. method:: get_labels()
Georg Brandl116aa622007-08-15 14:28:22 +00001241
Benjamin Petersone41251e2008-04-25 01:59:09 +00001242 Return a list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001243
1244
Benjamin Petersone41251e2008-04-25 01:59:09 +00001245 .. method:: set_labels(labels)
Georg Brandl116aa622007-08-15 14:28:22 +00001246
Benjamin Petersone41251e2008-04-25 01:59:09 +00001247 Set the list of labels on the message to *labels*.
Georg Brandl116aa622007-08-15 14:28:22 +00001248
1249
Benjamin Petersone41251e2008-04-25 01:59:09 +00001250 .. method:: add_label(label)
Georg Brandl116aa622007-08-15 14:28:22 +00001251
Benjamin Petersone41251e2008-04-25 01:59:09 +00001252 Add *label* to the list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001253
1254
Benjamin Petersone41251e2008-04-25 01:59:09 +00001255 .. method:: remove_label(label)
Georg Brandl116aa622007-08-15 14:28:22 +00001256
Benjamin Petersone41251e2008-04-25 01:59:09 +00001257 Remove *label* from the list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001258
1259
Benjamin Petersone41251e2008-04-25 01:59:09 +00001260 .. method:: get_visible()
Georg Brandl116aa622007-08-15 14:28:22 +00001261
Benjamin Petersone41251e2008-04-25 01:59:09 +00001262 Return an :class:`Message` instance whose headers are the message's
1263 visible headers and whose body is empty.
Georg Brandl116aa622007-08-15 14:28:22 +00001264
1265
Benjamin Petersone41251e2008-04-25 01:59:09 +00001266 .. method:: set_visible(visible)
Georg Brandl116aa622007-08-15 14:28:22 +00001267
Benjamin Petersone41251e2008-04-25 01:59:09 +00001268 Set the message's visible headers to be the same as the headers in
1269 *message*. Parameter *visible* should be a :class:`Message` instance, an
1270 :class:`email.Message.Message` instance, a string, or a file-like object
1271 (which should be open in text mode).
Georg Brandl116aa622007-08-15 14:28:22 +00001272
1273
Benjamin Petersone41251e2008-04-25 01:59:09 +00001274 .. method:: update_visible()
Georg Brandl116aa622007-08-15 14:28:22 +00001275
Benjamin Petersone41251e2008-04-25 01:59:09 +00001276 When a :class:`BabylMessage` instance's original headers are modified, the
1277 visible headers are not automatically modified to correspond. This method
1278 updates the visible headers as follows: each visible header with a
1279 corresponding original header is set to the value of the original header,
1280 each visible header without a corresponding original header is removed,
1281 and any of :mailheader:`Date`, :mailheader:`From`, :mailheader:`Reply-To`,
1282 :mailheader:`To`, :mailheader:`CC`, and :mailheader:`Subject` that are
1283 present in the original headers but not the visible headers are added to
1284 the visible headers.
Georg Brandl116aa622007-08-15 14:28:22 +00001285
1286When a :class:`BabylMessage` instance is created based upon a
1287:class:`MaildirMessage` instance, the following conversions take place:
1288
1289+-------------------+-------------------------------+
1290| Resulting state | :class:`MaildirMessage` state |
1291+===================+===============================+
1292| "unseen" label | no S flag |
1293+-------------------+-------------------------------+
1294| "deleted" label | T flag |
1295+-------------------+-------------------------------+
1296| "answered" label | R flag |
1297+-------------------+-------------------------------+
1298| "forwarded" label | P flag |
1299+-------------------+-------------------------------+
1300
1301When a :class:`BabylMessage` instance is created based upon an
1302:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
1303and :mailheader:`X-Status` headers are omitted and the following conversions
1304take place:
1305
1306+------------------+----------------------------------------------+
1307| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
1308| | state |
1309+==================+==============================================+
1310| "unseen" label | no R flag |
1311+------------------+----------------------------------------------+
1312| "deleted" label | D flag |
1313+------------------+----------------------------------------------+
1314| "answered" label | A flag |
1315+------------------+----------------------------------------------+
1316
1317When a :class:`BabylMessage` instance is created based upon an
1318:class:`MHMessage` instance, the following conversions take place:
1319
1320+------------------+--------------------------+
1321| Resulting state | :class:`MHMessage` state |
1322+==================+==========================+
1323| "unseen" label | "unseen" sequence |
1324+------------------+--------------------------+
1325| "answered" label | "replied" sequence |
1326+------------------+--------------------------+
1327
1328
1329.. _mailbox-mmdfmessage:
1330
1331:class:`MMDFMessage`
1332^^^^^^^^^^^^^^^^^^^^
1333
1334
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001335.. class:: MMDFMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001336
1337 A message with MMDF-specific behaviors. Parameter *message* has the same meaning
1338 as with the :class:`Message` constructor.
1339
Benjamin Petersone41251e2008-04-25 01:59:09 +00001340 As with message in an mbox mailbox, MMDF messages are stored with the
1341 sender's address and the delivery date in an initial line beginning with
1342 "From ". Likewise, flags that indicate the state of the message are
1343 typically stored in :mailheader:`Status` and :mailheader:`X-Status` headers.
Georg Brandl116aa622007-08-15 14:28:22 +00001344
Benjamin Petersone41251e2008-04-25 01:59:09 +00001345 Conventional flags for MMDF messages are identical to those of mbox message
1346 and are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001347
Benjamin Petersone41251e2008-04-25 01:59:09 +00001348 +------+----------+--------------------------------+
1349 | Flag | Meaning | Explanation |
1350 +======+==========+================================+
1351 | R | Read | Read |
1352 +------+----------+--------------------------------+
1353 | O | Old | Previously detected by MUA |
1354 +------+----------+--------------------------------+
1355 | D | Deleted | Marked for subsequent deletion |
1356 +------+----------+--------------------------------+
1357 | F | Flagged | Marked as important |
1358 +------+----------+--------------------------------+
1359 | A | Answered | Replied to |
1360 +------+----------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001361
Benjamin Petersone41251e2008-04-25 01:59:09 +00001362 The "R" and "O" flags are stored in the :mailheader:`Status` header, and the
1363 "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The
1364 flags and headers typically appear in the order mentioned.
Georg Brandl116aa622007-08-15 14:28:22 +00001365
Benjamin Petersone41251e2008-04-25 01:59:09 +00001366 :class:`MMDFMessage` instances offer the following methods, which are
1367 identical to those offered by :class:`mboxMessage`:
Georg Brandl116aa622007-08-15 14:28:22 +00001368
1369
Benjamin Petersone41251e2008-04-25 01:59:09 +00001370 .. method:: get_from()
Georg Brandl116aa622007-08-15 14:28:22 +00001371
Benjamin Petersone41251e2008-04-25 01:59:09 +00001372 Return a string representing the "From " line that marks the start of the
1373 message in an mbox mailbox. The leading "From " and the trailing newline
1374 are excluded.
Georg Brandl116aa622007-08-15 14:28:22 +00001375
1376
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001377 .. method:: set_from(from_, time_=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001378
Benjamin Petersone41251e2008-04-25 01:59:09 +00001379 Set the "From " line to *from_*, which should be specified without a
1380 leading "From " or trailing newline. For convenience, *time_* may be
1381 specified and will be formatted appropriately and appended to *from_*. If
1382 *time_* is specified, it should be a :class:`struct_time` instance, a
1383 tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use
1384 :meth:`time.gmtime`).
Georg Brandl116aa622007-08-15 14:28:22 +00001385
1386
Benjamin Petersone41251e2008-04-25 01:59:09 +00001387 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +00001388
Benjamin Petersone41251e2008-04-25 01:59:09 +00001389 Return a string specifying the flags that are currently set. If the
1390 message complies with the conventional format, the result is the
1391 concatenation in the following order of zero or one occurrence of each of
1392 ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001393
1394
Benjamin Petersone41251e2008-04-25 01:59:09 +00001395 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +00001396
Benjamin Petersone41251e2008-04-25 01:59:09 +00001397 Set the flags specified by *flags* and unset all others. Parameter *flags*
1398 should be the concatenation in any order of zero or more occurrences of
1399 each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001400
1401
Benjamin Petersone41251e2008-04-25 01:59:09 +00001402 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001403
Benjamin Petersone41251e2008-04-25 01:59:09 +00001404 Set the flag(s) specified by *flag* without changing other flags. To add
1405 more than one flag at a time, *flag* may be a string of more than one
1406 character.
Georg Brandl116aa622007-08-15 14:28:22 +00001407
1408
Benjamin Petersone41251e2008-04-25 01:59:09 +00001409 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001410
Benjamin Petersone41251e2008-04-25 01:59:09 +00001411 Unset the flag(s) specified by *flag* without changing other flags. To
1412 remove more than one flag at a time, *flag* maybe a string of more than
1413 one character.
Georg Brandl116aa622007-08-15 14:28:22 +00001414
1415When an :class:`MMDFMessage` instance is created based upon a
1416:class:`MaildirMessage` instance, a "From " line is generated based upon the
1417:class:`MaildirMessage` instance's delivery date, and the following conversions
1418take place:
1419
1420+-----------------+-------------------------------+
1421| Resulting state | :class:`MaildirMessage` state |
1422+=================+===============================+
1423| R flag | S flag |
1424+-----------------+-------------------------------+
1425| O flag | "cur" subdirectory |
1426+-----------------+-------------------------------+
1427| D flag | T flag |
1428+-----------------+-------------------------------+
1429| F flag | F flag |
1430+-----------------+-------------------------------+
1431| A flag | R flag |
1432+-----------------+-------------------------------+
1433
1434When an :class:`MMDFMessage` instance is created based upon an
1435:class:`MHMessage` instance, the following conversions take place:
1436
1437+-------------------+--------------------------+
1438| Resulting state | :class:`MHMessage` state |
1439+===================+==========================+
1440| R flag and O flag | no "unseen" sequence |
1441+-------------------+--------------------------+
1442| O flag | "unseen" sequence |
1443+-------------------+--------------------------+
1444| F flag | "flagged" sequence |
1445+-------------------+--------------------------+
1446| A flag | "replied" sequence |
1447+-------------------+--------------------------+
1448
1449When an :class:`MMDFMessage` instance is created based upon a
1450:class:`BabylMessage` instance, the following conversions take place:
1451
1452+-------------------+-----------------------------+
1453| Resulting state | :class:`BabylMessage` state |
1454+===================+=============================+
1455| R flag and O flag | no "unseen" label |
1456+-------------------+-----------------------------+
1457| O flag | "unseen" label |
1458+-------------------+-----------------------------+
1459| D flag | "deleted" label |
1460+-------------------+-----------------------------+
1461| A flag | "answered" label |
1462+-------------------+-----------------------------+
1463
1464When an :class:`MMDFMessage` instance is created based upon an
1465:class:`mboxMessage` instance, the "From " line is copied and all flags directly
1466correspond:
1467
1468+-----------------+----------------------------+
1469| Resulting state | :class:`mboxMessage` state |
1470+=================+============================+
1471| R flag | R flag |
1472+-----------------+----------------------------+
1473| O flag | O flag |
1474+-----------------+----------------------------+
1475| D flag | D flag |
1476+-----------------+----------------------------+
1477| F flag | F flag |
1478+-----------------+----------------------------+
1479| A flag | A flag |
1480+-----------------+----------------------------+
1481
1482
1483Exceptions
1484----------
1485
1486The following exception classes are defined in the :mod:`mailbox` module:
1487
1488
Benjamin Petersone41251e2008-04-25 01:59:09 +00001489.. exception:: Error()
Georg Brandl116aa622007-08-15 14:28:22 +00001490
1491 The based class for all other module-specific exceptions.
1492
1493
Benjamin Petersone41251e2008-04-25 01:59:09 +00001494.. exception:: NoSuchMailboxError()
Georg Brandl116aa622007-08-15 14:28:22 +00001495
1496 Raised when a mailbox is expected but is not found, such as when instantiating a
1497 :class:`Mailbox` subclass with a path that does not exist (and with the *create*
1498 parameter set to ``False``), or when opening a folder that does not exist.
1499
1500
Georg Brandl734e2682008-08-12 08:18:18 +00001501.. exception:: NotEmptyError()
Georg Brandl116aa622007-08-15 14:28:22 +00001502
1503 Raised when a mailbox is not empty but is expected to be, such as when deleting
1504 a folder that contains messages.
1505
1506
Benjamin Petersone41251e2008-04-25 01:59:09 +00001507.. exception:: ExternalClashError()
Georg Brandl116aa622007-08-15 14:28:22 +00001508
1509 Raised when some mailbox-related condition beyond the control of the program
1510 causes it to be unable to proceed, such as when failing to acquire a lock that
1511 another program already holds a lock, or when a uniquely-generated file name
1512 already exists.
1513
1514
Benjamin Petersone41251e2008-04-25 01:59:09 +00001515.. exception:: FormatError()
Georg Brandl116aa622007-08-15 14:28:22 +00001516
1517 Raised when the data in a file cannot be parsed, such as when an :class:`MH`
1518 instance attempts to read a corrupted :file:`.mh_sequences` file.
1519
1520
Georg Brandl116aa622007-08-15 14:28:22 +00001521.. _mailbox-examples:
1522
1523Examples
1524--------
1525
1526A simple example of printing the subjects of all messages in a mailbox that seem
1527interesting::
1528
1529 import mailbox
1530 for message in mailbox.mbox('~/mbox'):
1531 subject = message['subject'] # Could possibly be None.
1532 if subject and 'python' in subject.lower():
Georg Brandl6911e3c2007-09-04 07:15:32 +00001533 print(subject)
Georg Brandl116aa622007-08-15 14:28:22 +00001534
1535To copy all mail from a Babyl mailbox to an MH mailbox, converting all of the
1536format-specific information that can be converted::
1537
1538 import mailbox
1539 destination = mailbox.MH('~/Mail')
1540 destination.lock()
1541 for message in mailbox.Babyl('~/RMAIL'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001542 destination.add(mailbox.MHMessage(message))
Georg Brandl116aa622007-08-15 14:28:22 +00001543 destination.flush()
1544 destination.unlock()
1545
1546This example sorts mail from several mailing lists into different mailboxes,
1547being careful to avoid mail corruption due to concurrent modification by other
1548programs, mail loss due to interruption of the program, or premature termination
1549due to malformed messages in the mailbox::
1550
1551 import mailbox
1552 import email.Errors
1553
1554 list_names = ('python-list', 'python-dev', 'python-bugs')
1555
Georg Brandlf6945182008-02-01 11:56:49 +00001556 boxes = {name: mailbox.mbox('~/email/%s' % name) for name in list_names}
Georg Brandl116aa622007-08-15 14:28:22 +00001557 inbox = mailbox.Maildir('~/Maildir', factory=None)
1558
1559 for key in inbox.iterkeys():
1560 try:
1561 message = inbox[key]
1562 except email.Errors.MessageParseError:
1563 continue # The message is malformed. Just leave it.
1564
1565 for name in list_names:
1566 list_id = message['list-id']
1567 if list_id and name in list_id:
1568 # Get mailbox to use
1569 box = boxes[name]
1570
1571 # Write copy to disk before removing original.
1572 # If there's a crash, you might duplicate a message, but
1573 # that's better than losing a message completely.
1574 box.lock()
1575 box.add(message)
Georg Brandl48310cd2009-01-03 21:18:54 +00001576 box.flush()
Georg Brandl116aa622007-08-15 14:28:22 +00001577 box.unlock()
1578
1579 # Remove original message
1580 inbox.lock()
1581 inbox.discard(key)
1582 inbox.flush()
1583 inbox.unlock()
1584 break # Found destination, so stop looking.
1585
1586 for box in boxes.itervalues():
1587 box.close()
1588