blob: 94d95d10290b006c376230beb66ec070798ec375 [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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Georg Brandl116aa622007-08-15 14:28:22 +00007.. moduleauthor:: Gregory K. Johnson <gkj@gregorykjohnson.com>
8.. sectionauthor:: Gregory K. Johnson <gkj@gregorykjohnson.com>
9
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010**Source code:** :source:`Lib/mailbox.py`
11
12--------------
Georg Brandl116aa622007-08-15 14:28:22 +000013
14This module defines two classes, :class:`Mailbox` and :class:`Message`, for
15accessing and manipulating on-disk mailboxes and the messages they contain.
16:class:`Mailbox` offers a dictionary-like mapping from keys to messages.
R David Murray53202502012-09-28 15:19:16 -040017:class:`Message` extends the :mod:`email.message` module's
18:class:`~email.message.Message` class with format-specific state and behavior.
19Supported mailbox formats are Maildir, mbox, MH, Babyl, and MMDF.
Georg Brandl116aa622007-08-15 14:28:22 +000020
21
22.. seealso::
23
24 Module :mod:`email`
25 Represent and manipulate messages.
26
27
28.. _mailbox-objects:
29
30:class:`Mailbox` objects
31------------------------
32
Georg Brandl116aa622007-08-15 14:28:22 +000033.. class:: Mailbox
34
35 A mailbox, which may be inspected and modified.
36
Benjamin Petersone41251e2008-04-25 01:59:09 +000037 The :class:`Mailbox` class defines an interface and is not intended to be
38 instantiated. Instead, format-specific subclasses should inherit from
39 :class:`Mailbox` and your code should instantiate a particular subclass.
Georg Brandl116aa622007-08-15 14:28:22 +000040
Benjamin Petersone41251e2008-04-25 01:59:09 +000041 The :class:`Mailbox` interface is dictionary-like, with small keys
42 corresponding to messages. Keys are issued by the :class:`Mailbox` instance
43 with which they will be used and are only meaningful to that :class:`Mailbox`
44 instance. A key continues to identify a message even if the corresponding
45 message is modified, such as by replacing it with another message.
Georg Brandl116aa622007-08-15 14:28:22 +000046
Benjamin Petersone41251e2008-04-25 01:59:09 +000047 Messages may be added to a :class:`Mailbox` instance using the set-like
48 method :meth:`add` and removed using a ``del`` statement or the set-like
49 methods :meth:`remove` and :meth:`discard`.
Georg Brandl116aa622007-08-15 14:28:22 +000050
Benjamin Petersone41251e2008-04-25 01:59:09 +000051 :class:`Mailbox` interface semantics differ from dictionary semantics in some
52 noteworthy ways. Each time a message is requested, a new representation
53 (typically a :class:`Message` instance) is generated based upon the current
54 state of the mailbox. Similarly, when a message is added to a
55 :class:`Mailbox` instance, the provided message representation's contents are
56 copied. In neither case is a reference to the message representation kept by
57 the :class:`Mailbox` instance.
Georg Brandl116aa622007-08-15 14:28:22 +000058
Benjamin Petersone41251e2008-04-25 01:59:09 +000059 The default :class:`Mailbox` iterator iterates over message representations,
60 not keys as the default dictionary iterator does. Moreover, modification of a
61 mailbox during iteration is safe and well-defined. Messages added to the
62 mailbox after an iterator is created will not be seen by the
63 iterator. Messages removed from the mailbox before the iterator yields them
64 will be silently skipped, though using a key from an iterator may result in a
65 :exc:`KeyError` exception if the corresponding message is subsequently
66 removed.
Georg Brandl116aa622007-08-15 14:28:22 +000067
Benjamin Petersone41251e2008-04-25 01:59:09 +000068 .. warning::
Georg Brandl116aa622007-08-15 14:28:22 +000069
Benjamin Petersone41251e2008-04-25 01:59:09 +000070 Be very cautious when modifying mailboxes that might be simultaneously
71 changed by some other process. The safest mailbox format to use for such
72 tasks is Maildir; try to avoid using single-file formats such as mbox for
73 concurrent writing. If you're modifying a mailbox, you *must* lock it by
74 calling the :meth:`lock` and :meth:`unlock` methods *before* reading any
75 messages in the file or making any changes by adding or deleting a
76 message. Failing to lock the mailbox runs the risk of losing messages or
77 corrupting the entire mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +000078
Benjamin Petersone41251e2008-04-25 01:59:09 +000079 :class:`Mailbox` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000080
81
Benjamin Petersone41251e2008-04-25 01:59:09 +000082 .. method:: add(message)
Georg Brandl116aa622007-08-15 14:28:22 +000083
Benjamin Petersone41251e2008-04-25 01:59:09 +000084 Add *message* to the mailbox and return the key that has been assigned to
85 it.
Georg Brandl116aa622007-08-15 14:28:22 +000086
Benjamin Petersone41251e2008-04-25 01:59:09 +000087 Parameter *message* may be a :class:`Message` instance, an
R David Murray53202502012-09-28 15:19:16 -040088 :class:`email.message.Message` instance, a string, a byte string, or a
R. David Murrayb7deff12011-01-30 06:21:28 +000089 file-like object (which should be open in binary mode). If *message* is
90 an instance of the
Benjamin Petersone41251e2008-04-25 01:59:09 +000091 appropriate format-specific :class:`Message` subclass (e.g., if it's an
92 :class:`mboxMessage` instance and this is an :class:`mbox` instance), its
93 format-specific information is used. Otherwise, reasonable defaults for
94 format-specific information are used.
Georg Brandl116aa622007-08-15 14:28:22 +000095
Georg Brandl33369cf2012-06-24 22:48:03 +020096 .. versionchanged:: 3.2
97 Support for binary input was added.
R. David Murrayb7deff12011-01-30 06:21:28 +000098
Georg Brandl116aa622007-08-15 14:28:22 +000099
Benjamin Petersone41251e2008-04-25 01:59:09 +0000100 .. method:: remove(key)
101 __delitem__(key)
102 discard(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Benjamin Petersone41251e2008-04-25 01:59:09 +0000104 Delete the message corresponding to *key* from the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000105
Benjamin Petersone41251e2008-04-25 01:59:09 +0000106 If no such message exists, a :exc:`KeyError` exception is raised if the
107 method was called as :meth:`remove` or :meth:`__delitem__` but no
108 exception is raised if the method was called as :meth:`discard`. The
109 behavior of :meth:`discard` may be preferred if the underlying mailbox
110 format supports concurrent modification by other processes.
Georg Brandl116aa622007-08-15 14:28:22 +0000111
112
Benjamin Petersone41251e2008-04-25 01:59:09 +0000113 .. method:: __setitem__(key, message)
Georg Brandl116aa622007-08-15 14:28:22 +0000114
Benjamin Petersone41251e2008-04-25 01:59:09 +0000115 Replace the message corresponding to *key* with *message*. Raise a
116 :exc:`KeyError` exception if no message already corresponds to *key*.
Georg Brandl116aa622007-08-15 14:28:22 +0000117
Benjamin Petersone41251e2008-04-25 01:59:09 +0000118 As with :meth:`add`, parameter *message* may be a :class:`Message`
R David Murray53202502012-09-28 15:19:16 -0400119 instance, an :class:`email.message.Message` instance, a string, a byte
R. David Murrayb7deff12011-01-30 06:21:28 +0000120 string, or a file-like object (which should be open in binary mode). If
121 *message* is an
Benjamin Petersone41251e2008-04-25 01:59:09 +0000122 instance of the appropriate format-specific :class:`Message` subclass
123 (e.g., if it's an :class:`mboxMessage` instance and this is an
124 :class:`mbox` instance), its format-specific information is
125 used. Otherwise, the format-specific information of the message that
126 currently corresponds to *key* is left unchanged.
Georg Brandl116aa622007-08-15 14:28:22 +0000127
128
Benjamin Petersone41251e2008-04-25 01:59:09 +0000129 .. method:: iterkeys()
130 keys()
Georg Brandl116aa622007-08-15 14:28:22 +0000131
Benjamin Petersone41251e2008-04-25 01:59:09 +0000132 Return an iterator over all keys if called as :meth:`iterkeys` or return a
133 list of keys if called as :meth:`keys`.
Georg Brandl116aa622007-08-15 14:28:22 +0000134
135
Benjamin Petersone41251e2008-04-25 01:59:09 +0000136 .. method:: itervalues()
137 __iter__()
138 values()
Georg Brandl116aa622007-08-15 14:28:22 +0000139
Benjamin Petersone41251e2008-04-25 01:59:09 +0000140 Return an iterator over representations of all messages if called as
141 :meth:`itervalues` or :meth:`__iter__` or return a list of such
142 representations if called as :meth:`values`. The messages are represented
143 as instances of the appropriate format-specific :class:`Message` subclass
144 unless a custom message factory was specified when the :class:`Mailbox`
145 instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000146
Benjamin Petersone41251e2008-04-25 01:59:09 +0000147 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000148
Benjamin Petersone41251e2008-04-25 01:59:09 +0000149 The behavior of :meth:`__iter__` is unlike that of dictionaries, which
150 iterate over keys.
Georg Brandl116aa622007-08-15 14:28:22 +0000151
152
Benjamin Petersone41251e2008-04-25 01:59:09 +0000153 .. method:: iteritems()
154 items()
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Benjamin Petersone41251e2008-04-25 01:59:09 +0000156 Return an iterator over (*key*, *message*) pairs, where *key* is a key and
157 *message* is a message representation, if called as :meth:`iteritems` or
158 return a list of such pairs if called as :meth:`items`. The messages are
159 represented as instances of the appropriate format-specific
160 :class:`Message` subclass unless a custom message factory was specified
161 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000162
163
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000164 .. method:: get(key, default=None)
Benjamin Petersone41251e2008-04-25 01:59:09 +0000165 __getitem__(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000166
Benjamin Petersone41251e2008-04-25 01:59:09 +0000167 Return a representation of the message corresponding to *key*. If no such
168 message exists, *default* is returned if the method was called as
169 :meth:`get` and a :exc:`KeyError` exception is raised if the method was
170 called as :meth:`__getitem__`. The message is represented as an instance
171 of the appropriate format-specific :class:`Message` subclass unless a
172 custom message factory was specified when the :class:`Mailbox` instance
173 was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000174
175
Benjamin Petersone41251e2008-04-25 01:59:09 +0000176 .. method:: get_message(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000177
Benjamin Petersone41251e2008-04-25 01:59:09 +0000178 Return a representation of the message corresponding to *key* as an
179 instance of the appropriate format-specific :class:`Message` subclass, or
180 raise a :exc:`KeyError` exception if no such message exists.
Georg Brandl116aa622007-08-15 14:28:22 +0000181
182
R. David Murrayb7deff12011-01-30 06:21:28 +0000183 .. method:: get_bytes(key)
184
185 Return a byte representation of the message corresponding to *key*, or
186 raise a :exc:`KeyError` exception if no such message exists.
187
188 .. versionadded:: 3.2
189
190
Benjamin Petersone41251e2008-04-25 01:59:09 +0000191 .. method:: get_string(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000192
Benjamin Petersone41251e2008-04-25 01:59:09 +0000193 Return a string representation of the message corresponding to *key*, or
R. David Murrayb7deff12011-01-30 06:21:28 +0000194 raise a :exc:`KeyError` exception if no such message exists. The
195 message is processed through :class:`email.message.Message` to
196 convert it to a 7bit clean representation.
Georg Brandl116aa622007-08-15 14:28:22 +0000197
198
Benjamin Petersone41251e2008-04-25 01:59:09 +0000199 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000200
Benjamin Petersone41251e2008-04-25 01:59:09 +0000201 Return a file-like representation of the message corresponding to *key*,
Georg Brandl6ce29fa2010-10-30 14:33:28 +0000202 or raise a :exc:`KeyError` exception if no such message exists. The
203 file-like object behaves as if open in binary mode. This file should be
Benjamin Petersone41251e2008-04-25 01:59:09 +0000204 closed once it is no longer needed.
Georg Brandl116aa622007-08-15 14:28:22 +0000205
R. David Murrayb7deff12011-01-30 06:21:28 +0000206 .. versionchanged:: 3.2
207 The file object really is a binary file; previously it was incorrectly
208 returned in text mode. Also, the file-like object now supports the
Serhiy Storchaka14867992014-09-10 23:43:41 +0300209 context management protocol: you can use a :keyword:`with` statement to
R. David Murrayb7deff12011-01-30 06:21:28 +0000210 automatically close it.
Georg Brandl6ce29fa2010-10-30 14:33:28 +0000211
Benjamin Petersone41251e2008-04-25 01:59:09 +0000212 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000213
Benjamin Petersone41251e2008-04-25 01:59:09 +0000214 Unlike other representations of messages, file-like representations are
215 not necessarily independent of the :class:`Mailbox` instance that
Georg Brandl6ce29fa2010-10-30 14:33:28 +0000216 created them or of the underlying mailbox. More specific documentation
Benjamin Petersone41251e2008-04-25 01:59:09 +0000217 is provided by each subclass.
Georg Brandl116aa622007-08-15 14:28:22 +0000218
219
Benjamin Petersone41251e2008-04-25 01:59:09 +0000220 .. method:: __contains__(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000221
Benjamin Petersone41251e2008-04-25 01:59:09 +0000222 Return ``True`` if *key* corresponds to a message, ``False`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000223
224
Benjamin Petersone41251e2008-04-25 01:59:09 +0000225 .. method:: __len__()
Georg Brandl116aa622007-08-15 14:28:22 +0000226
Benjamin Petersone41251e2008-04-25 01:59:09 +0000227 Return a count of messages in the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000228
229
Benjamin Petersone41251e2008-04-25 01:59:09 +0000230 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +0000231
Benjamin Petersone41251e2008-04-25 01:59:09 +0000232 Delete all messages from the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000233
234
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000235 .. method:: pop(key, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000236
Benjamin Petersone41251e2008-04-25 01:59:09 +0000237 Return a representation of the message corresponding to *key* and delete
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000238 the message. If no such message exists, return *default*. The message is
Benjamin Petersone41251e2008-04-25 01:59:09 +0000239 represented as an instance of the appropriate format-specific
240 :class:`Message` subclass unless a custom message factory was specified
241 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000242
243
Benjamin Petersone41251e2008-04-25 01:59:09 +0000244 .. method:: popitem()
Georg Brandl116aa622007-08-15 14:28:22 +0000245
Benjamin Petersone41251e2008-04-25 01:59:09 +0000246 Return an arbitrary (*key*, *message*) pair, where *key* is a key and
247 *message* is a message representation, and delete the corresponding
248 message. If the mailbox is empty, raise a :exc:`KeyError` exception. The
249 message is represented as an instance of the appropriate format-specific
250 :class:`Message` subclass unless a custom message factory was specified
251 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000252
253
Benjamin Petersone41251e2008-04-25 01:59:09 +0000254 .. method:: update(arg)
Georg Brandl116aa622007-08-15 14:28:22 +0000255
Benjamin Petersone41251e2008-04-25 01:59:09 +0000256 Parameter *arg* should be a *key*-to-*message* mapping or an iterable of
257 (*key*, *message*) pairs. Updates the mailbox so that, for each given
258 *key* and *message*, the message corresponding to *key* is set to
259 *message* as if by using :meth:`__setitem__`. As with :meth:`__setitem__`,
260 each *key* must already correspond to a message in the mailbox or else a
261 :exc:`KeyError` exception will be raised, so in general it is incorrect
262 for *arg* to be a :class:`Mailbox` instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000263
Benjamin Petersone41251e2008-04-25 01:59:09 +0000264 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000265
Benjamin Petersone41251e2008-04-25 01:59:09 +0000266 Unlike with dictionaries, keyword arguments are not supported.
Georg Brandl116aa622007-08-15 14:28:22 +0000267
268
Benjamin Petersone41251e2008-04-25 01:59:09 +0000269 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000270
Benjamin Petersone41251e2008-04-25 01:59:09 +0000271 Write any pending changes to the filesystem. For some :class:`Mailbox`
272 subclasses, changes are always written immediately and :meth:`flush` does
273 nothing, but you should still make a habit of calling this method.
Georg Brandl116aa622007-08-15 14:28:22 +0000274
275
Benjamin Petersone41251e2008-04-25 01:59:09 +0000276 .. method:: lock()
Georg Brandl116aa622007-08-15 14:28:22 +0000277
Benjamin Petersone41251e2008-04-25 01:59:09 +0000278 Acquire an exclusive advisory lock on the mailbox so that other processes
279 know not to modify it. An :exc:`ExternalClashError` is raised if the lock
280 is not available. The particular locking mechanisms used depend upon the
281 mailbox format. You should *always* lock the mailbox before making any
282 modifications to its contents.
Georg Brandl116aa622007-08-15 14:28:22 +0000283
284
Benjamin Petersone41251e2008-04-25 01:59:09 +0000285 .. method:: unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000286
Benjamin Petersone41251e2008-04-25 01:59:09 +0000287 Release the lock on the mailbox, if any.
Georg Brandl116aa622007-08-15 14:28:22 +0000288
289
Benjamin Petersone41251e2008-04-25 01:59:09 +0000290 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000291
Benjamin Petersone41251e2008-04-25 01:59:09 +0000292 Flush the mailbox, unlock it if necessary, and close any open files. For
293 some :class:`Mailbox` subclasses, this method does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000294
295
296.. _mailbox-maildir:
297
298:class:`Maildir`
299^^^^^^^^^^^^^^^^
300
301
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000302.. class:: Maildir(dirname, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000303
304 A subclass of :class:`Mailbox` for mailboxes in Maildir format. Parameter
305 *factory* is a callable object that accepts a file-like message representation
306 (which behaves as if opened in binary mode) and returns a custom representation.
307 If *factory* is ``None``, :class:`MaildirMessage` is used as the default message
308 representation. If *create* is ``True``, the mailbox is created if it does not
309 exist.
310
Sviatoslav Sydorenkoe4418472019-07-13 16:47:15 +0200311 If *create* is ``True`` and the *dirname* path exists, it will be treated as
312 an existing maildir without attempting to verify its directory layout.
313
Georg Brandlaa5b4112008-05-11 20:51:18 +0000314 It is for historical reasons that *dirname* is named as such rather than *path*.
Georg Brandl116aa622007-08-15 14:28:22 +0000315
Benjamin Petersone41251e2008-04-25 01:59:09 +0000316 Maildir is a directory-based mailbox format invented for the qmail mail
317 transfer agent and now widely supported by other programs. Messages in a
318 Maildir mailbox are stored in separate files within a common directory
319 structure. This design allows Maildir mailboxes to be accessed and modified
320 by multiple unrelated programs without data corruption, so file locking is
321 unnecessary.
Georg Brandl116aa622007-08-15 14:28:22 +0000322
Benjamin Petersone41251e2008-04-25 01:59:09 +0000323 Maildir mailboxes contain three subdirectories, namely: :file:`tmp`,
324 :file:`new`, and :file:`cur`. Messages are created momentarily in the
325 :file:`tmp` subdirectory and then moved to the :file:`new` subdirectory to
326 finalize delivery. A mail user agent may subsequently move the message to the
327 :file:`cur` subdirectory and store information about the state of the message
328 in a special "info" section appended to its file name.
Georg Brandl116aa622007-08-15 14:28:22 +0000329
Benjamin Petersone41251e2008-04-25 01:59:09 +0000330 Folders of the style introduced by the Courier mail transfer agent are also
331 supported. Any subdirectory of the main mailbox is considered a folder if
332 ``'.'`` is the first character in its name. Folder names are represented by
333 :class:`Maildir` without the leading ``'.'``. Each folder is itself a Maildir
334 mailbox but should not contain other folders. Instead, a logical nesting is
335 indicated using ``'.'`` to delimit levels, e.g., "Archived.2005.07".
Georg Brandl116aa622007-08-15 14:28:22 +0000336
Benjamin Petersone41251e2008-04-25 01:59:09 +0000337 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000338
Benjamin Petersone41251e2008-04-25 01:59:09 +0000339 The Maildir specification requires the use of a colon (``':'``) in certain
340 message file names. However, some operating systems do not permit this
341 character in file names, If you wish to use a Maildir-like format on such
342 an operating system, you should specify another character to use
343 instead. The exclamation point (``'!'``) is a popular choice. For
344 example::
Georg Brandl116aa622007-08-15 14:28:22 +0000345
Benjamin Petersone41251e2008-04-25 01:59:09 +0000346 import mailbox
347 mailbox.Maildir.colon = '!'
Georg Brandl116aa622007-08-15 14:28:22 +0000348
Benjamin Petersone41251e2008-04-25 01:59:09 +0000349 The :attr:`colon` attribute may also be set on a per-instance basis.
Georg Brandl116aa622007-08-15 14:28:22 +0000350
Benjamin Petersone41251e2008-04-25 01:59:09 +0000351 :class:`Maildir` instances have all of the methods of :class:`Mailbox` in
352 addition to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000353
354
Benjamin Petersone41251e2008-04-25 01:59:09 +0000355 .. method:: list_folders()
Georg Brandl116aa622007-08-15 14:28:22 +0000356
Benjamin Petersone41251e2008-04-25 01:59:09 +0000357 Return a list of the names of all folders.
Georg Brandl116aa622007-08-15 14:28:22 +0000358
359
Ezio Melotti2d352d02009-09-15 18:51:37 +0000360 .. method:: get_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000361
Benjamin Petersone41251e2008-04-25 01:59:09 +0000362 Return a :class:`Maildir` instance representing the folder whose name is
363 *folder*. A :exc:`NoSuchMailboxError` exception is raised if the folder
364 does not exist.
Georg Brandl116aa622007-08-15 14:28:22 +0000365
366
Benjamin Petersone41251e2008-04-25 01:59:09 +0000367 .. method:: add_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000368
Benjamin Petersone41251e2008-04-25 01:59:09 +0000369 Create a folder whose name is *folder* and return a :class:`Maildir`
370 instance representing it.
Georg Brandl116aa622007-08-15 14:28:22 +0000371
372
Benjamin Petersone41251e2008-04-25 01:59:09 +0000373 .. method:: remove_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000374
Benjamin Petersone41251e2008-04-25 01:59:09 +0000375 Delete the folder whose name is *folder*. If the folder contains any
376 messages, a :exc:`NotEmptyError` exception will be raised and the folder
377 will not be deleted.
Georg Brandl116aa622007-08-15 14:28:22 +0000378
379
Benjamin Petersone41251e2008-04-25 01:59:09 +0000380 .. method:: clean()
Georg Brandl116aa622007-08-15 14:28:22 +0000381
Benjamin Petersone41251e2008-04-25 01:59:09 +0000382 Delete temporary files from the mailbox that have not been accessed in the
383 last 36 hours. The Maildir specification says that mail-reading programs
384 should do this occasionally.
Georg Brandl116aa622007-08-15 14:28:22 +0000385
Benjamin Petersone41251e2008-04-25 01:59:09 +0000386 Some :class:`Mailbox` methods implemented by :class:`Maildir` deserve special
387 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000388
389
Benjamin Petersone41251e2008-04-25 01:59:09 +0000390 .. method:: add(message)
391 __setitem__(key, message)
392 update(arg)
Georg Brandl116aa622007-08-15 14:28:22 +0000393
Benjamin Petersone41251e2008-04-25 01:59:09 +0000394 .. warning::
Georg Brandl116aa622007-08-15 14:28:22 +0000395
Benjamin Petersone41251e2008-04-25 01:59:09 +0000396 These methods generate unique file names based upon the current process
397 ID. When using multiple threads, undetected name clashes may occur and
398 cause corruption of the mailbox unless threads are coordinated to avoid
399 using these methods to manipulate the same mailbox simultaneously.
Georg Brandl116aa622007-08-15 14:28:22 +0000400
401
Benjamin Petersone41251e2008-04-25 01:59:09 +0000402 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000403
Benjamin Petersone41251e2008-04-25 01:59:09 +0000404 All changes to Maildir mailboxes are immediately applied, so this method
405 does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000406
407
Benjamin Petersone41251e2008-04-25 01:59:09 +0000408 .. method:: lock()
409 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000410
Benjamin Petersone41251e2008-04-25 01:59:09 +0000411 Maildir mailboxes do not support (or require) locking, so these methods do
412 nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000413
414
Benjamin Petersone41251e2008-04-25 01:59:09 +0000415 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000416
Benjamin Petersone41251e2008-04-25 01:59:09 +0000417 :class:`Maildir` instances do not keep any open files and the underlying
418 mailboxes do not support locking, so this method does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000419
420
Benjamin Petersone41251e2008-04-25 01:59:09 +0000421 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000422
Benjamin Petersone41251e2008-04-25 01:59:09 +0000423 Depending upon the host platform, it may not be possible to modify or
424 remove the underlying message while the returned file remains open.
Georg Brandl116aa622007-08-15 14:28:22 +0000425
426
427.. seealso::
428
Zackery Spytzec388cf2020-10-22 17:33:28 -0600429 `maildir man page from Courier <http://www.courier-mta.org/maildir.html>`_
430 A specification of the format. Describes a common extension for
431 supporting folders.
Georg Brandl116aa622007-08-15 14:28:22 +0000432
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300433 `Using maildir format <https://cr.yp.to/proto/maildir.html>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000434 Notes on Maildir by its inventor. Includes an updated name-creation scheme and
435 details on "info" semantics.
436
Georg Brandl116aa622007-08-15 14:28:22 +0000437
438.. _mailbox-mbox:
439
440:class:`mbox`
441^^^^^^^^^^^^^
442
443
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000444.. class:: mbox(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000445
446 A subclass of :class:`Mailbox` for mailboxes in mbox format. Parameter *factory*
447 is a callable object that accepts a file-like message representation (which
448 behaves as if opened in binary mode) and returns a custom representation. If
449 *factory* is ``None``, :class:`mboxMessage` is used as the default message
450 representation. If *create* is ``True``, the mailbox is created if it does not
451 exist.
452
Benjamin Petersone41251e2008-04-25 01:59:09 +0000453 The mbox format is the classic format for storing mail on Unix systems. All
454 messages in an mbox mailbox are stored in a single file with the beginning of
455 each message indicated by a line whose first five characters are "From ".
Georg Brandl116aa622007-08-15 14:28:22 +0000456
Benjamin Petersone41251e2008-04-25 01:59:09 +0000457 Several variations of the mbox format exist to address perceived shortcomings in
458 the original. In the interest of compatibility, :class:`mbox` implements the
459 original format, which is sometimes referred to as :dfn:`mboxo`. This means that
460 the :mailheader:`Content-Length` header, if present, is ignored and that any
461 occurrences of "From " at the beginning of a line in a message body are
462 transformed to ">From " when storing the message, although occurrences of ">From
463 " are not transformed to "From " when reading the message.
Georg Brandl116aa622007-08-15 14:28:22 +0000464
Benjamin Petersone41251e2008-04-25 01:59:09 +0000465 Some :class:`Mailbox` methods implemented by :class:`mbox` deserve special
466 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000467
468
Benjamin Petersone41251e2008-04-25 01:59:09 +0000469 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000470
Benjamin Petersone41251e2008-04-25 01:59:09 +0000471 Using the file after calling :meth:`flush` or :meth:`close` on the
472 :class:`mbox` instance may yield unpredictable results or raise an
473 exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000474
475
Benjamin Petersone41251e2008-04-25 01:59:09 +0000476 .. method:: lock()
477 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000478
Benjamin Petersone41251e2008-04-25 01:59:09 +0000479 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000480 :c:func:`flock` and :c:func:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000481
482
483.. seealso::
484
Georg Brandl116aa622007-08-15 14:28:22 +0000485 `mbox man page from tin <http://www.tin.org/bin/man.cgi?section=5&topic=mbox>`_
Zackery Spytzec388cf2020-10-22 17:33:28 -0600486 A specification of the format, with details on locking.
Georg Brandl116aa622007-08-15 14:28:22 +0000487
Georg Brandl5d941342016-02-26 19:37:12 +0100488 `Configuring Netscape Mail on Unix: Why The Content-Length Format is Bad <https://www.jwz.org/doc/content-length.html>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000489 An argument for using the original mbox format rather than a variation.
490
jimmy4f29f3c2017-12-13 13:37:51 +0100491 `"mbox" is a family of several mutually incompatible mailbox formats <https://www.loc.gov/preservation/digital/formats/fdd/fdd000383.shtml>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000492 A history of mbox variations.
493
494
495.. _mailbox-mh:
496
497:class:`MH`
498^^^^^^^^^^^
499
500
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000501.. class:: MH(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000502
503 A subclass of :class:`Mailbox` for mailboxes in MH format. Parameter *factory*
504 is a callable object that accepts a file-like message representation (which
505 behaves as if opened in binary mode) and returns a custom representation. If
506 *factory* is ``None``, :class:`MHMessage` is used as the default message
507 representation. If *create* is ``True``, the mailbox is created if it does not
508 exist.
509
Benjamin Petersone41251e2008-04-25 01:59:09 +0000510 MH is a directory-based mailbox format invented for the MH Message Handling
511 System, a mail user agent. Each message in an MH mailbox resides in its own
512 file. An MH mailbox may contain other MH mailboxes (called :dfn:`folders`) in
513 addition to messages. Folders may be nested indefinitely. MH mailboxes also
514 support :dfn:`sequences`, which are named lists used to logically group
515 messages without moving them to sub-folders. Sequences are defined in a file
516 called :file:`.mh_sequences` in each folder.
Georg Brandl116aa622007-08-15 14:28:22 +0000517
Benjamin Petersone41251e2008-04-25 01:59:09 +0000518 The :class:`MH` class manipulates MH mailboxes, but it does not attempt to
519 emulate all of :program:`mh`'s behaviors. In particular, it does not modify
520 and is not affected by the :file:`context` or :file:`.mh_profile` files that
521 are used by :program:`mh` to store its state and configuration.
Georg Brandl116aa622007-08-15 14:28:22 +0000522
Benjamin Petersone41251e2008-04-25 01:59:09 +0000523 :class:`MH` instances have all of the methods of :class:`Mailbox` in addition
524 to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000525
526
Benjamin Petersone41251e2008-04-25 01:59:09 +0000527 .. method:: list_folders()
Georg Brandl116aa622007-08-15 14:28:22 +0000528
Benjamin Petersone41251e2008-04-25 01:59:09 +0000529 Return a list of the names of all folders.
Georg Brandl116aa622007-08-15 14:28:22 +0000530
531
Benjamin Petersone41251e2008-04-25 01:59:09 +0000532 .. method:: get_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000533
Benjamin Petersone41251e2008-04-25 01:59:09 +0000534 Return an :class:`MH` instance representing the folder whose name is
535 *folder*. A :exc:`NoSuchMailboxError` exception is raised if the folder
536 does not exist.
Georg Brandl116aa622007-08-15 14:28:22 +0000537
538
Benjamin Petersone41251e2008-04-25 01:59:09 +0000539 .. method:: add_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000540
Benjamin Petersone41251e2008-04-25 01:59:09 +0000541 Create a folder whose name is *folder* and return an :class:`MH` instance
542 representing it.
Georg Brandl116aa622007-08-15 14:28:22 +0000543
544
Benjamin Petersone41251e2008-04-25 01:59:09 +0000545 .. method:: remove_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000546
Benjamin Petersone41251e2008-04-25 01:59:09 +0000547 Delete the folder whose name is *folder*. If the folder contains any
548 messages, a :exc:`NotEmptyError` exception will be raised and the folder
549 will not be deleted.
Georg Brandl116aa622007-08-15 14:28:22 +0000550
551
Benjamin Petersone41251e2008-04-25 01:59:09 +0000552 .. method:: get_sequences()
Georg Brandl116aa622007-08-15 14:28:22 +0000553
Benjamin Petersone41251e2008-04-25 01:59:09 +0000554 Return a dictionary of sequence names mapped to key lists. If there are no
555 sequences, the empty dictionary is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000556
557
Benjamin Petersone41251e2008-04-25 01:59:09 +0000558 .. method:: set_sequences(sequences)
Georg Brandl116aa622007-08-15 14:28:22 +0000559
Benjamin Petersone41251e2008-04-25 01:59:09 +0000560 Re-define the sequences that exist in the mailbox based upon *sequences*,
561 a dictionary of names mapped to key lists, like returned by
562 :meth:`get_sequences`.
Georg Brandl116aa622007-08-15 14:28:22 +0000563
564
Benjamin Petersone41251e2008-04-25 01:59:09 +0000565 .. method:: pack()
Georg Brandl116aa622007-08-15 14:28:22 +0000566
Benjamin Petersone41251e2008-04-25 01:59:09 +0000567 Rename messages in the mailbox as necessary to eliminate gaps in
568 numbering. Entries in the sequences list are updated correspondingly.
Georg Brandl116aa622007-08-15 14:28:22 +0000569
Benjamin Petersone41251e2008-04-25 01:59:09 +0000570 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000571
Benjamin Petersone41251e2008-04-25 01:59:09 +0000572 Already-issued keys are invalidated by this operation and should not be
573 subsequently used.
Georg Brandl116aa622007-08-15 14:28:22 +0000574
Benjamin Petersone41251e2008-04-25 01:59:09 +0000575 Some :class:`Mailbox` methods implemented by :class:`MH` deserve special
576 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000577
578
Benjamin Petersone41251e2008-04-25 01:59:09 +0000579 .. method:: remove(key)
580 __delitem__(key)
581 discard(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000582
Benjamin Petersone41251e2008-04-25 01:59:09 +0000583 These methods immediately delete the message. The MH convention of marking
584 a message for deletion by prepending a comma to its name is not used.
Georg Brandl116aa622007-08-15 14:28:22 +0000585
586
Benjamin Petersone41251e2008-04-25 01:59:09 +0000587 .. method:: lock()
588 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000589
Benjamin Petersone41251e2008-04-25 01:59:09 +0000590 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000591 :c:func:`flock` and :c:func:`lockf` system calls. For MH mailboxes, locking
Benjamin Petersone41251e2008-04-25 01:59:09 +0000592 the mailbox means locking the :file:`.mh_sequences` file and, only for the
593 duration of any operations that affect them, locking individual message
594 files.
Georg Brandl116aa622007-08-15 14:28:22 +0000595
596
Benjamin Petersone41251e2008-04-25 01:59:09 +0000597 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000598
Benjamin Petersone41251e2008-04-25 01:59:09 +0000599 Depending upon the host platform, it may not be possible to remove the
600 underlying message while the returned file remains open.
Georg Brandl116aa622007-08-15 14:28:22 +0000601
602
Benjamin Petersone41251e2008-04-25 01:59:09 +0000603 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000604
Benjamin Petersone41251e2008-04-25 01:59:09 +0000605 All changes to MH mailboxes are immediately applied, so this method does
606 nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000607
608
Benjamin Petersone41251e2008-04-25 01:59:09 +0000609 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000610
Benjamin Petersone41251e2008-04-25 01:59:09 +0000611 :class:`MH` instances do not keep any open files, so this method is
612 equivalent to :meth:`unlock`.
Georg Brandl116aa622007-08-15 14:28:22 +0000613
614
615.. seealso::
616
617 `nmh - Message Handling System <http://www.nongnu.org/nmh/>`_
618 Home page of :program:`nmh`, an updated version of the original :program:`mh`.
619
Sanyam Khurana338cd832018-01-20 05:55:37 +0530620 `MH & nmh: Email for Users & Programmers <https://rand-mh.sourceforge.io/book/>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000621 A GPL-licensed book on :program:`mh` and :program:`nmh`, with some information
622 on the mailbox format.
623
624
625.. _mailbox-babyl:
626
627:class:`Babyl`
628^^^^^^^^^^^^^^
629
630
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000631.. class:: Babyl(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000632
633 A subclass of :class:`Mailbox` for mailboxes in Babyl format. Parameter
634 *factory* is a callable object that accepts a file-like message representation
635 (which behaves as if opened in binary mode) and returns a custom representation.
636 If *factory* is ``None``, :class:`BabylMessage` is used as the default message
637 representation. If *create* is ``True``, the mailbox is created if it does not
638 exist.
639
Benjamin Petersone41251e2008-04-25 01:59:09 +0000640 Babyl is a single-file mailbox format used by the Rmail mail user agent
641 included with Emacs. The beginning of a message is indicated by a line
642 containing the two characters Control-Underscore (``'\037'``) and Control-L
643 (``'\014'``). The end of a message is indicated by the start of the next
644 message or, in the case of the last message, a line containing a
645 Control-Underscore (``'\037'``) character.
Georg Brandl116aa622007-08-15 14:28:22 +0000646
Benjamin Petersone41251e2008-04-25 01:59:09 +0000647 Messages in a Babyl mailbox have two sets of headers, original headers and
648 so-called visible headers. Visible headers are typically a subset of the
649 original headers that have been reformatted or abridged to be more
650 attractive. Each message in a Babyl mailbox also has an accompanying list of
651 :dfn:`labels`, or short strings that record extra information about the
652 message, and a list of all user-defined labels found in the mailbox is kept
653 in the Babyl options section.
Georg Brandl116aa622007-08-15 14:28:22 +0000654
Benjamin Petersone41251e2008-04-25 01:59:09 +0000655 :class:`Babyl` instances have all of the methods of :class:`Mailbox` in
656 addition to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000657
658
Benjamin Petersone41251e2008-04-25 01:59:09 +0000659 .. method:: get_labels()
Georg Brandl116aa622007-08-15 14:28:22 +0000660
Benjamin Petersone41251e2008-04-25 01:59:09 +0000661 Return a list of the names of all user-defined labels used in the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000662
Benjamin Petersone41251e2008-04-25 01:59:09 +0000663 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000664
Benjamin Petersone41251e2008-04-25 01:59:09 +0000665 The actual messages are inspected to determine which labels exist in
666 the mailbox rather than consulting the list of labels in the Babyl
667 options section, but the Babyl section is updated whenever the mailbox
668 is modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000669
Benjamin Petersone41251e2008-04-25 01:59:09 +0000670 Some :class:`Mailbox` methods implemented by :class:`Babyl` deserve special
671 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000672
673
Benjamin Petersone41251e2008-04-25 01:59:09 +0000674 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000675
Benjamin Petersone41251e2008-04-25 01:59:09 +0000676 In Babyl mailboxes, the headers of a message are not stored contiguously
677 with the body of the message. To generate a file-like representation, the
Martin Panter7462b6492015-11-02 03:37:02 +0000678 headers and body are copied together into an :class:`io.BytesIO` instance,
Serhiy Storchakae79be872013-08-17 00:09:55 +0300679 which has an API identical to that of a
Benjamin Petersone41251e2008-04-25 01:59:09 +0000680 file. As a result, the file-like object is truly independent of the
681 underlying mailbox but does not save memory compared to a string
682 representation.
Georg Brandl116aa622007-08-15 14:28:22 +0000683
684
Benjamin Petersone41251e2008-04-25 01:59:09 +0000685 .. method:: lock()
686 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000687
Benjamin Petersone41251e2008-04-25 01:59:09 +0000688 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000689 :c:func:`flock` and :c:func:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000690
691
692.. seealso::
693
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300694 `Format of Version 5 Babyl Files <https://quimby.gnus.org/notes/BABYL>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000695 A specification of the Babyl format.
696
Serhiy Storchaka6dff0202016-05-07 10:49:07 +0300697 `Reading Mail with Rmail <https://www.gnu.org/software/emacs/manual/html_node/emacs/Rmail.html>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000698 The Rmail manual, with some information on Babyl semantics.
699
700
701.. _mailbox-mmdf:
702
703:class:`MMDF`
704^^^^^^^^^^^^^
705
706
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000707.. class:: MMDF(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000708
709 A subclass of :class:`Mailbox` for mailboxes in MMDF format. Parameter *factory*
710 is a callable object that accepts a file-like message representation (which
711 behaves as if opened in binary mode) and returns a custom representation. If
712 *factory* is ``None``, :class:`MMDFMessage` is used as the default message
713 representation. If *create* is ``True``, the mailbox is created if it does not
714 exist.
715
Benjamin Petersone41251e2008-04-25 01:59:09 +0000716 MMDF is a single-file mailbox format invented for the Multichannel Memorandum
717 Distribution Facility, a mail transfer agent. Each message is in the same
718 form as an mbox message but is bracketed before and after by lines containing
719 four Control-A (``'\001'``) characters. As with the mbox format, the
720 beginning of each message is indicated by a line whose first five characters
721 are "From ", but additional occurrences of "From " are not transformed to
722 ">From " when storing messages because the extra message separator lines
723 prevent mistaking such occurrences for the starts of subsequent messages.
Georg Brandl116aa622007-08-15 14:28:22 +0000724
Benjamin Petersone41251e2008-04-25 01:59:09 +0000725 Some :class:`Mailbox` methods implemented by :class:`MMDF` deserve special
726 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000727
728
Benjamin Petersone41251e2008-04-25 01:59:09 +0000729 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000730
Benjamin Petersone41251e2008-04-25 01:59:09 +0000731 Using the file after calling :meth:`flush` or :meth:`close` on the
732 :class:`MMDF` instance may yield unpredictable results or raise an
733 exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000734
735
Benjamin Petersone41251e2008-04-25 01:59:09 +0000736 .. method:: lock()
737 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000738
Benjamin Petersone41251e2008-04-25 01:59:09 +0000739 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000740 :c:func:`flock` and :c:func:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000741
742
743.. seealso::
744
745 `mmdf man page from tin <http://www.tin.org/bin/man.cgi?section=5&topic=mmdf>`_
746 A specification of MMDF format from the documentation of tin, a newsreader.
747
Georg Brandl5d941342016-02-26 19:37:12 +0100748 `MMDF <https://en.wikipedia.org/wiki/MMDF>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000749 A Wikipedia article describing the Multichannel Memorandum Distribution
750 Facility.
751
752
753.. _mailbox-message-objects:
754
755:class:`Message` objects
756------------------------
757
758
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000759.. class:: Message(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000760
R David Murrayc5fe4072012-09-28 15:09:31 -0400761 A subclass of the :mod:`email.message` module's
762 :class:`~email.message.Message`. Subclasses of :class:`mailbox.Message` add
763 mailbox-format-specific state and behavior.
Georg Brandl116aa622007-08-15 14:28:22 +0000764
765 If *message* is omitted, the new instance is created in a default, empty state.
R David Murrayc5fe4072012-09-28 15:09:31 -0400766 If *message* is an :class:`email.message.Message` instance, its contents are
Georg Brandl116aa622007-08-15 14:28:22 +0000767 copied; furthermore, any format-specific information is converted insofar as
R. David Murrayb7deff12011-01-30 06:21:28 +0000768 possible if *message* is a :class:`Message` instance. If *message* is a string,
769 a byte string,
Georg Brandl116aa622007-08-15 14:28:22 +0000770 or a file, it should contain an :rfc:`2822`\ -compliant message, which is read
R. David Murrayb7deff12011-01-30 06:21:28 +0000771 and parsed. Files should be open in binary mode, but text mode files
772 are accepted for backward compatibility.
Georg Brandl116aa622007-08-15 14:28:22 +0000773
Benjamin Petersone41251e2008-04-25 01:59:09 +0000774 The format-specific state and behaviors offered by subclasses vary, but in
775 general it is only the properties that are not specific to a particular
776 mailbox that are supported (although presumably the properties are specific
777 to a particular mailbox format). For example, file offsets for single-file
778 mailbox formats and file names for directory-based mailbox formats are not
779 retained, because they are only applicable to the original mailbox. But state
780 such as whether a message has been read by the user or marked as important is
781 retained, because it applies to the message itself.
Georg Brandl116aa622007-08-15 14:28:22 +0000782
Benjamin Petersone41251e2008-04-25 01:59:09 +0000783 There is no requirement that :class:`Message` instances be used to represent
784 messages retrieved using :class:`Mailbox` instances. In some situations, the
785 time and memory required to generate :class:`Message` representations might
Ezio Melottie130a522011-10-19 10:58:56 +0300786 not be acceptable. For such situations, :class:`Mailbox` instances also
Benjamin Petersone41251e2008-04-25 01:59:09 +0000787 offer string and file-like representations, and a custom message factory may
788 be specified when a :class:`Mailbox` instance is initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000789
790
791.. _mailbox-maildirmessage:
792
793:class:`MaildirMessage`
794^^^^^^^^^^^^^^^^^^^^^^^
795
796
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000797.. class:: MaildirMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000798
799 A message with Maildir-specific behaviors. Parameter *message* has the same
800 meaning as with the :class:`Message` constructor.
801
Benjamin Petersone41251e2008-04-25 01:59:09 +0000802 Typically, a mail user agent application moves all of the messages in the
803 :file:`new` subdirectory to the :file:`cur` subdirectory after the first time
804 the user opens and closes the mailbox, recording that the messages are old
805 whether or not they've actually been read. Each message in :file:`cur` has an
806 "info" section added to its file name to store information about its state.
807 (Some mail readers may also add an "info" section to messages in
808 :file:`new`.) The "info" section may take one of two forms: it may contain
809 "2," followed by a list of standardized flags (e.g., "2,FR") or it may
810 contain "1," followed by so-called experimental information. Standard flags
811 for Maildir messages are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000812
Benjamin Petersone41251e2008-04-25 01:59:09 +0000813 +------+---------+--------------------------------+
814 | Flag | Meaning | Explanation |
815 +======+=========+================================+
816 | D | Draft | Under composition |
817 +------+---------+--------------------------------+
818 | F | Flagged | Marked as important |
819 +------+---------+--------------------------------+
820 | P | Passed | Forwarded, resent, or bounced |
821 +------+---------+--------------------------------+
822 | R | Replied | Replied to |
823 +------+---------+--------------------------------+
824 | S | Seen | Read |
825 +------+---------+--------------------------------+
826 | T | Trashed | Marked for subsequent deletion |
827 +------+---------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000828
Benjamin Petersone41251e2008-04-25 01:59:09 +0000829 :class:`MaildirMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000830
831
Benjamin Petersone41251e2008-04-25 01:59:09 +0000832 .. method:: get_subdir()
Georg Brandl116aa622007-08-15 14:28:22 +0000833
Benjamin Petersone41251e2008-04-25 01:59:09 +0000834 Return either "new" (if the message should be stored in the :file:`new`
835 subdirectory) or "cur" (if the message should be stored in the :file:`cur`
836 subdirectory).
Georg Brandl116aa622007-08-15 14:28:22 +0000837
Benjamin Petersone41251e2008-04-25 01:59:09 +0000838 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000839
Benjamin Petersone41251e2008-04-25 01:59:09 +0000840 A message is typically moved from :file:`new` to :file:`cur` after its
841 mailbox has been accessed, whether or not the message is has been
842 read. A message ``msg`` has been read if ``"S" in msg.get_flags()`` is
843 ``True``.
Georg Brandl116aa622007-08-15 14:28:22 +0000844
845
Benjamin Petersone41251e2008-04-25 01:59:09 +0000846 .. method:: set_subdir(subdir)
Georg Brandl116aa622007-08-15 14:28:22 +0000847
Benjamin Petersone41251e2008-04-25 01:59:09 +0000848 Set the subdirectory the message should be stored in. Parameter *subdir*
849 must be either "new" or "cur".
Georg Brandl116aa622007-08-15 14:28:22 +0000850
851
Benjamin Petersone41251e2008-04-25 01:59:09 +0000852 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +0000853
Benjamin Petersone41251e2008-04-25 01:59:09 +0000854 Return a string specifying the flags that are currently set. If the
855 message complies with the standard Maildir format, the result is the
856 concatenation in alphabetical order of zero or one occurrence of each of
857 ``'D'``, ``'F'``, ``'P'``, ``'R'``, ``'S'``, and ``'T'``. The empty string
858 is returned if no flags are set or if "info" contains experimental
859 semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000860
861
Benjamin Petersone41251e2008-04-25 01:59:09 +0000862 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +0000863
Benjamin Petersone41251e2008-04-25 01:59:09 +0000864 Set the flags specified by *flags* and unset all others.
Georg Brandl116aa622007-08-15 14:28:22 +0000865
866
Benjamin Petersone41251e2008-04-25 01:59:09 +0000867 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000868
Benjamin Petersone41251e2008-04-25 01:59:09 +0000869 Set the flag(s) specified by *flag* without changing other flags. To add
870 more than one flag at a time, *flag* may be a string of more than one
871 character. The current "info" is overwritten whether or not it contains
872 experimental information rather than flags.
Georg Brandl116aa622007-08-15 14:28:22 +0000873
874
Benjamin Petersone41251e2008-04-25 01:59:09 +0000875 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000876
Benjamin Petersone41251e2008-04-25 01:59:09 +0000877 Unset the flag(s) specified by *flag* without changing other flags. To
878 remove more than one flag at a time, *flag* maybe a string of more than
879 one character. If "info" contains experimental information rather than
880 flags, the current "info" is not modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000881
882
Benjamin Petersone41251e2008-04-25 01:59:09 +0000883 .. method:: get_date()
Georg Brandl116aa622007-08-15 14:28:22 +0000884
Benjamin Petersone41251e2008-04-25 01:59:09 +0000885 Return the delivery date of the message as a floating-point number
886 representing seconds since the epoch.
Georg Brandl116aa622007-08-15 14:28:22 +0000887
888
Benjamin Petersone41251e2008-04-25 01:59:09 +0000889 .. method:: set_date(date)
Georg Brandl116aa622007-08-15 14:28:22 +0000890
Benjamin Petersone41251e2008-04-25 01:59:09 +0000891 Set the delivery date of the message to *date*, a floating-point number
892 representing seconds since the epoch.
Georg Brandl116aa622007-08-15 14:28:22 +0000893
894
Benjamin Petersone41251e2008-04-25 01:59:09 +0000895 .. method:: get_info()
Georg Brandl116aa622007-08-15 14:28:22 +0000896
Benjamin Petersone41251e2008-04-25 01:59:09 +0000897 Return a string containing the "info" for a message. This is useful for
898 accessing and modifying "info" that is experimental (i.e., not a list of
899 flags).
Georg Brandl116aa622007-08-15 14:28:22 +0000900
901
Benjamin Petersone41251e2008-04-25 01:59:09 +0000902 .. method:: set_info(info)
Georg Brandl116aa622007-08-15 14:28:22 +0000903
Benjamin Petersone41251e2008-04-25 01:59:09 +0000904 Set "info" to *info*, which should be a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000905
906When a :class:`MaildirMessage` instance is created based upon an
907:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
908and :mailheader:`X-Status` headers are omitted and the following conversions
909take place:
910
911+--------------------+----------------------------------------------+
912| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
913| | state |
914+====================+==============================================+
915| "cur" subdirectory | O flag |
916+--------------------+----------------------------------------------+
917| F flag | F flag |
918+--------------------+----------------------------------------------+
919| R flag | A flag |
920+--------------------+----------------------------------------------+
921| S flag | R flag |
922+--------------------+----------------------------------------------+
923| T flag | D flag |
924+--------------------+----------------------------------------------+
925
926When a :class:`MaildirMessage` instance is created based upon an
927:class:`MHMessage` instance, the following conversions take place:
928
929+-------------------------------+--------------------------+
930| Resulting state | :class:`MHMessage` state |
931+===============================+==========================+
932| "cur" subdirectory | "unseen" sequence |
933+-------------------------------+--------------------------+
934| "cur" subdirectory and S flag | no "unseen" sequence |
935+-------------------------------+--------------------------+
936| F flag | "flagged" sequence |
937+-------------------------------+--------------------------+
938| R flag | "replied" sequence |
939+-------------------------------+--------------------------+
940
941When a :class:`MaildirMessage` instance is created based upon a
942:class:`BabylMessage` instance, the following conversions take place:
943
944+-------------------------------+-------------------------------+
945| Resulting state | :class:`BabylMessage` state |
946+===============================+===============================+
947| "cur" subdirectory | "unseen" label |
948+-------------------------------+-------------------------------+
949| "cur" subdirectory and S flag | no "unseen" label |
950+-------------------------------+-------------------------------+
951| P flag | "forwarded" or "resent" label |
952+-------------------------------+-------------------------------+
953| R flag | "answered" label |
954+-------------------------------+-------------------------------+
955| T flag | "deleted" label |
956+-------------------------------+-------------------------------+
957
958
959.. _mailbox-mboxmessage:
960
961:class:`mboxMessage`
962^^^^^^^^^^^^^^^^^^^^
963
964
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000965.. class:: mboxMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000966
967 A message with mbox-specific behaviors. Parameter *message* has the same meaning
968 as with the :class:`Message` constructor.
969
Benjamin Petersone41251e2008-04-25 01:59:09 +0000970 Messages in an mbox mailbox are stored together in a single file. The
971 sender's envelope address and the time of delivery are typically stored in a
972 line beginning with "From " that is used to indicate the start of a message,
973 though there is considerable variation in the exact format of this data among
974 mbox implementations. Flags that indicate the state of the message, such as
975 whether it has been read or marked as important, are typically stored in
976 :mailheader:`Status` and :mailheader:`X-Status` headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000977
Benjamin Petersone41251e2008-04-25 01:59:09 +0000978 Conventional flags for mbox messages are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000979
Benjamin Petersone41251e2008-04-25 01:59:09 +0000980 +------+----------+--------------------------------+
981 | Flag | Meaning | Explanation |
982 +======+==========+================================+
983 | R | Read | Read |
984 +------+----------+--------------------------------+
985 | O | Old | Previously detected by MUA |
986 +------+----------+--------------------------------+
987 | D | Deleted | Marked for subsequent deletion |
988 +------+----------+--------------------------------+
989 | F | Flagged | Marked as important |
990 +------+----------+--------------------------------+
991 | A | Answered | Replied to |
992 +------+----------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000993
Benjamin Petersone41251e2008-04-25 01:59:09 +0000994 The "R" and "O" flags are stored in the :mailheader:`Status` header, and the
995 "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The
996 flags and headers typically appear in the order mentioned.
Georg Brandl116aa622007-08-15 14:28:22 +0000997
Benjamin Petersone41251e2008-04-25 01:59:09 +0000998 :class:`mboxMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000999
1000
Benjamin Petersone41251e2008-04-25 01:59:09 +00001001 .. method:: get_from()
Georg Brandl116aa622007-08-15 14:28:22 +00001002
Benjamin Petersone41251e2008-04-25 01:59:09 +00001003 Return a string representing the "From " line that marks the start of the
1004 message in an mbox mailbox. The leading "From " and the trailing newline
1005 are excluded.
Georg Brandl116aa622007-08-15 14:28:22 +00001006
1007
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001008 .. method:: set_from(from_, time_=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001009
Benjamin Petersone41251e2008-04-25 01:59:09 +00001010 Set the "From " line to *from_*, which should be specified without a
1011 leading "From " or trailing newline. For convenience, *time_* may be
1012 specified and will be formatted appropriately and appended to *from_*. If
Serhiy Storchakabfdcd432013-10-13 23:09:14 +03001013 *time_* is specified, it should be a :class:`time.struct_time` instance, a
Benjamin Petersone41251e2008-04-25 01:59:09 +00001014 tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use
1015 :meth:`time.gmtime`).
Georg Brandl116aa622007-08-15 14:28:22 +00001016
1017
Benjamin Petersone41251e2008-04-25 01:59:09 +00001018 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +00001019
Benjamin Petersone41251e2008-04-25 01:59:09 +00001020 Return a string specifying the flags that are currently set. If the
1021 message complies with the conventional format, the result is the
1022 concatenation in the following order of zero or one occurrence of each of
1023 ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001024
1025
Benjamin Petersone41251e2008-04-25 01:59:09 +00001026 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +00001027
Benjamin Petersone41251e2008-04-25 01:59:09 +00001028 Set the flags specified by *flags* and unset all others. Parameter *flags*
1029 should be the concatenation in any order of zero or more occurrences of
1030 each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001031
1032
Benjamin Petersone41251e2008-04-25 01:59:09 +00001033 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001034
Benjamin Petersone41251e2008-04-25 01:59:09 +00001035 Set the flag(s) specified by *flag* without changing other flags. To add
1036 more than one flag at a time, *flag* may be a string of more than one
1037 character.
Georg Brandl116aa622007-08-15 14:28:22 +00001038
1039
Benjamin Petersone41251e2008-04-25 01:59:09 +00001040 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001041
Benjamin Petersone41251e2008-04-25 01:59:09 +00001042 Unset the flag(s) specified by *flag* without changing other flags. To
1043 remove more than one flag at a time, *flag* maybe a string of more than
1044 one character.
Georg Brandl116aa622007-08-15 14:28:22 +00001045
1046When an :class:`mboxMessage` instance is created based upon a
1047:class:`MaildirMessage` instance, a "From " line is generated based upon the
1048:class:`MaildirMessage` instance's delivery date, and the following conversions
1049take place:
1050
1051+-----------------+-------------------------------+
1052| Resulting state | :class:`MaildirMessage` state |
1053+=================+===============================+
1054| R flag | S flag |
1055+-----------------+-------------------------------+
1056| O flag | "cur" subdirectory |
1057+-----------------+-------------------------------+
1058| D flag | T flag |
1059+-----------------+-------------------------------+
1060| F flag | F flag |
1061+-----------------+-------------------------------+
1062| A flag | R flag |
1063+-----------------+-------------------------------+
1064
1065When an :class:`mboxMessage` instance is created based upon an
1066:class:`MHMessage` instance, the following conversions take place:
1067
1068+-------------------+--------------------------+
1069| Resulting state | :class:`MHMessage` state |
1070+===================+==========================+
1071| R flag and O flag | no "unseen" sequence |
1072+-------------------+--------------------------+
1073| O flag | "unseen" sequence |
1074+-------------------+--------------------------+
1075| F flag | "flagged" sequence |
1076+-------------------+--------------------------+
1077| A flag | "replied" sequence |
1078+-------------------+--------------------------+
1079
1080When an :class:`mboxMessage` instance is created based upon a
1081:class:`BabylMessage` instance, the following conversions take place:
1082
1083+-------------------+-----------------------------+
1084| Resulting state | :class:`BabylMessage` state |
1085+===================+=============================+
1086| R flag and O flag | no "unseen" label |
1087+-------------------+-----------------------------+
1088| O flag | "unseen" label |
1089+-------------------+-----------------------------+
1090| D flag | "deleted" label |
1091+-------------------+-----------------------------+
1092| A flag | "answered" label |
1093+-------------------+-----------------------------+
1094
1095When a :class:`Message` instance is created based upon an :class:`MMDFMessage`
1096instance, the "From " line is copied and all flags directly correspond:
1097
1098+-----------------+----------------------------+
1099| Resulting state | :class:`MMDFMessage` state |
1100+=================+============================+
1101| R flag | R flag |
1102+-----------------+----------------------------+
1103| O flag | O flag |
1104+-----------------+----------------------------+
1105| D flag | D flag |
1106+-----------------+----------------------------+
1107| F flag | F flag |
1108+-----------------+----------------------------+
1109| A flag | A flag |
1110+-----------------+----------------------------+
1111
1112
1113.. _mailbox-mhmessage:
1114
1115:class:`MHMessage`
1116^^^^^^^^^^^^^^^^^^
1117
1118
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001119.. class:: MHMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001120
1121 A message with MH-specific behaviors. Parameter *message* has the same meaning
1122 as with the :class:`Message` constructor.
1123
Benjamin Petersone41251e2008-04-25 01:59:09 +00001124 MH messages do not support marks or flags in the traditional sense, but they
1125 do support sequences, which are logical groupings of arbitrary messages. Some
1126 mail reading programs (although not the standard :program:`mh` and
1127 :program:`nmh`) use sequences in much the same way flags are used with other
1128 formats, as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001129
Benjamin Petersone41251e2008-04-25 01:59:09 +00001130 +----------+------------------------------------------+
1131 | Sequence | Explanation |
1132 +==========+==========================================+
1133 | unseen | Not read, but previously detected by MUA |
1134 +----------+------------------------------------------+
1135 | replied | Replied to |
1136 +----------+------------------------------------------+
1137 | flagged | Marked as important |
1138 +----------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001139
Benjamin Petersone41251e2008-04-25 01:59:09 +00001140 :class:`MHMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001141
1142
Benjamin Petersone41251e2008-04-25 01:59:09 +00001143 .. method:: get_sequences()
Georg Brandl116aa622007-08-15 14:28:22 +00001144
Benjamin Petersone41251e2008-04-25 01:59:09 +00001145 Return a list of the names of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001146
1147
Benjamin Petersone41251e2008-04-25 01:59:09 +00001148 .. method:: set_sequences(sequences)
Georg Brandl116aa622007-08-15 14:28:22 +00001149
Benjamin Petersone41251e2008-04-25 01:59:09 +00001150 Set the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001151
1152
Benjamin Petersone41251e2008-04-25 01:59:09 +00001153 .. method:: add_sequence(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +00001154
Benjamin Petersone41251e2008-04-25 01:59:09 +00001155 Add *sequence* to the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001156
1157
Benjamin Petersone41251e2008-04-25 01:59:09 +00001158 .. method:: remove_sequence(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +00001159
Benjamin Petersone41251e2008-04-25 01:59:09 +00001160 Remove *sequence* from the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001161
1162When an :class:`MHMessage` instance is created based upon a
1163:class:`MaildirMessage` instance, the following conversions take place:
1164
1165+--------------------+-------------------------------+
1166| Resulting state | :class:`MaildirMessage` state |
1167+====================+===============================+
1168| "unseen" sequence | no S flag |
1169+--------------------+-------------------------------+
1170| "replied" sequence | R flag |
1171+--------------------+-------------------------------+
1172| "flagged" sequence | F flag |
1173+--------------------+-------------------------------+
1174
1175When an :class:`MHMessage` instance is created based upon an
1176:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
1177and :mailheader:`X-Status` headers are omitted and the following conversions
1178take place:
1179
1180+--------------------+----------------------------------------------+
1181| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
1182| | state |
1183+====================+==============================================+
1184| "unseen" sequence | no R flag |
1185+--------------------+----------------------------------------------+
1186| "replied" sequence | A flag |
1187+--------------------+----------------------------------------------+
1188| "flagged" sequence | F flag |
1189+--------------------+----------------------------------------------+
1190
1191When an :class:`MHMessage` instance is created based upon a
1192:class:`BabylMessage` instance, the following conversions take place:
1193
1194+--------------------+-----------------------------+
1195| Resulting state | :class:`BabylMessage` state |
1196+====================+=============================+
1197| "unseen" sequence | "unseen" label |
1198+--------------------+-----------------------------+
1199| "replied" sequence | "answered" label |
1200+--------------------+-----------------------------+
1201
1202
1203.. _mailbox-babylmessage:
1204
1205:class:`BabylMessage`
1206^^^^^^^^^^^^^^^^^^^^^
1207
1208
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001209.. class:: BabylMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001210
1211 A message with Babyl-specific behaviors. Parameter *message* has the same
1212 meaning as with the :class:`Message` constructor.
1213
Benjamin Petersone41251e2008-04-25 01:59:09 +00001214 Certain message labels, called :dfn:`attributes`, are defined by convention
1215 to have special meanings. The attributes are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001216
Benjamin Petersone41251e2008-04-25 01:59:09 +00001217 +-----------+------------------------------------------+
1218 | Label | Explanation |
1219 +===========+==========================================+
1220 | unseen | Not read, but previously detected by MUA |
1221 +-----------+------------------------------------------+
1222 | deleted | Marked for subsequent deletion |
1223 +-----------+------------------------------------------+
1224 | filed | Copied to another file or mailbox |
1225 +-----------+------------------------------------------+
1226 | answered | Replied to |
1227 +-----------+------------------------------------------+
1228 | forwarded | Forwarded |
1229 +-----------+------------------------------------------+
1230 | edited | Modified by the user |
1231 +-----------+------------------------------------------+
1232 | resent | Resent |
1233 +-----------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001234
Benjamin Petersone41251e2008-04-25 01:59:09 +00001235 By default, Rmail displays only visible headers. The :class:`BabylMessage`
1236 class, though, uses the original headers because they are more
1237 complete. Visible headers may be accessed explicitly if desired.
Georg Brandl116aa622007-08-15 14:28:22 +00001238
Benjamin Petersone41251e2008-04-25 01:59:09 +00001239 :class:`BabylMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001240
1241
Benjamin Petersone41251e2008-04-25 01:59:09 +00001242 .. method:: get_labels()
Georg Brandl116aa622007-08-15 14:28:22 +00001243
Benjamin Petersone41251e2008-04-25 01:59:09 +00001244 Return a list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001245
1246
Benjamin Petersone41251e2008-04-25 01:59:09 +00001247 .. method:: set_labels(labels)
Georg Brandl116aa622007-08-15 14:28:22 +00001248
Benjamin Petersone41251e2008-04-25 01:59:09 +00001249 Set the list of labels on the message to *labels*.
Georg Brandl116aa622007-08-15 14:28:22 +00001250
1251
Benjamin Petersone41251e2008-04-25 01:59:09 +00001252 .. method:: add_label(label)
Georg Brandl116aa622007-08-15 14:28:22 +00001253
Benjamin Petersone41251e2008-04-25 01:59:09 +00001254 Add *label* to the list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001255
1256
Benjamin Petersone41251e2008-04-25 01:59:09 +00001257 .. method:: remove_label(label)
Georg Brandl116aa622007-08-15 14:28:22 +00001258
Benjamin Petersone41251e2008-04-25 01:59:09 +00001259 Remove *label* from the list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001260
1261
Benjamin Petersone41251e2008-04-25 01:59:09 +00001262 .. method:: get_visible()
Georg Brandl116aa622007-08-15 14:28:22 +00001263
Benjamin Petersone41251e2008-04-25 01:59:09 +00001264 Return an :class:`Message` instance whose headers are the message's
1265 visible headers and whose body is empty.
Georg Brandl116aa622007-08-15 14:28:22 +00001266
1267
Benjamin Petersone41251e2008-04-25 01:59:09 +00001268 .. method:: set_visible(visible)
Georg Brandl116aa622007-08-15 14:28:22 +00001269
Benjamin Petersone41251e2008-04-25 01:59:09 +00001270 Set the message's visible headers to be the same as the headers in
1271 *message*. Parameter *visible* should be a :class:`Message` instance, an
R David Murray53202502012-09-28 15:19:16 -04001272 :class:`email.message.Message` instance, a string, or a file-like object
Benjamin Petersone41251e2008-04-25 01:59:09 +00001273 (which should be open in text mode).
Georg Brandl116aa622007-08-15 14:28:22 +00001274
1275
Benjamin Petersone41251e2008-04-25 01:59:09 +00001276 .. method:: update_visible()
Georg Brandl116aa622007-08-15 14:28:22 +00001277
Benjamin Petersone41251e2008-04-25 01:59:09 +00001278 When a :class:`BabylMessage` instance's original headers are modified, the
1279 visible headers are not automatically modified to correspond. This method
1280 updates the visible headers as follows: each visible header with a
1281 corresponding original header is set to the value of the original header,
1282 each visible header without a corresponding original header is removed,
1283 and any of :mailheader:`Date`, :mailheader:`From`, :mailheader:`Reply-To`,
1284 :mailheader:`To`, :mailheader:`CC`, and :mailheader:`Subject` that are
1285 present in the original headers but not the visible headers are added to
1286 the visible headers.
Georg Brandl116aa622007-08-15 14:28:22 +00001287
1288When a :class:`BabylMessage` instance is created based upon a
1289:class:`MaildirMessage` instance, the following conversions take place:
1290
1291+-------------------+-------------------------------+
1292| Resulting state | :class:`MaildirMessage` state |
1293+===================+===============================+
1294| "unseen" label | no S flag |
1295+-------------------+-------------------------------+
1296| "deleted" label | T flag |
1297+-------------------+-------------------------------+
1298| "answered" label | R flag |
1299+-------------------+-------------------------------+
1300| "forwarded" label | P flag |
1301+-------------------+-------------------------------+
1302
1303When a :class:`BabylMessage` instance is created based upon an
1304:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
1305and :mailheader:`X-Status` headers are omitted and the following conversions
1306take place:
1307
1308+------------------+----------------------------------------------+
1309| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
1310| | state |
1311+==================+==============================================+
1312| "unseen" label | no R flag |
1313+------------------+----------------------------------------------+
1314| "deleted" label | D flag |
1315+------------------+----------------------------------------------+
1316| "answered" label | A flag |
1317+------------------+----------------------------------------------+
1318
1319When a :class:`BabylMessage` instance is created based upon an
1320:class:`MHMessage` instance, the following conversions take place:
1321
1322+------------------+--------------------------+
1323| Resulting state | :class:`MHMessage` state |
1324+==================+==========================+
1325| "unseen" label | "unseen" sequence |
1326+------------------+--------------------------+
1327| "answered" label | "replied" sequence |
1328+------------------+--------------------------+
1329
1330
1331.. _mailbox-mmdfmessage:
1332
1333:class:`MMDFMessage`
1334^^^^^^^^^^^^^^^^^^^^
1335
1336
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001337.. class:: MMDFMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001338
1339 A message with MMDF-specific behaviors. Parameter *message* has the same meaning
1340 as with the :class:`Message` constructor.
1341
Benjamin Petersone41251e2008-04-25 01:59:09 +00001342 As with message in an mbox mailbox, MMDF messages are stored with the
1343 sender's address and the delivery date in an initial line beginning with
1344 "From ". Likewise, flags that indicate the state of the message are
1345 typically stored in :mailheader:`Status` and :mailheader:`X-Status` headers.
Georg Brandl116aa622007-08-15 14:28:22 +00001346
Benjamin Petersone41251e2008-04-25 01:59:09 +00001347 Conventional flags for MMDF messages are identical to those of mbox message
1348 and are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001349
Benjamin Petersone41251e2008-04-25 01:59:09 +00001350 +------+----------+--------------------------------+
1351 | Flag | Meaning | Explanation |
1352 +======+==========+================================+
1353 | R | Read | Read |
1354 +------+----------+--------------------------------+
1355 | O | Old | Previously detected by MUA |
1356 +------+----------+--------------------------------+
1357 | D | Deleted | Marked for subsequent deletion |
1358 +------+----------+--------------------------------+
1359 | F | Flagged | Marked as important |
1360 +------+----------+--------------------------------+
1361 | A | Answered | Replied to |
1362 +------+----------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001363
Benjamin Petersone41251e2008-04-25 01:59:09 +00001364 The "R" and "O" flags are stored in the :mailheader:`Status` header, and the
1365 "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The
1366 flags and headers typically appear in the order mentioned.
Georg Brandl116aa622007-08-15 14:28:22 +00001367
Benjamin Petersone41251e2008-04-25 01:59:09 +00001368 :class:`MMDFMessage` instances offer the following methods, which are
1369 identical to those offered by :class:`mboxMessage`:
Georg Brandl116aa622007-08-15 14:28:22 +00001370
1371
Benjamin Petersone41251e2008-04-25 01:59:09 +00001372 .. method:: get_from()
Georg Brandl116aa622007-08-15 14:28:22 +00001373
Benjamin Petersone41251e2008-04-25 01:59:09 +00001374 Return a string representing the "From " line that marks the start of the
1375 message in an mbox mailbox. The leading "From " and the trailing newline
1376 are excluded.
Georg Brandl116aa622007-08-15 14:28:22 +00001377
1378
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001379 .. method:: set_from(from_, time_=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001380
Benjamin Petersone41251e2008-04-25 01:59:09 +00001381 Set the "From " line to *from_*, which should be specified without a
1382 leading "From " or trailing newline. For convenience, *time_* may be
1383 specified and will be formatted appropriately and appended to *from_*. If
Serhiy Storchakabfdcd432013-10-13 23:09:14 +03001384 *time_* is specified, it should be a :class:`time.struct_time` instance, a
Benjamin Petersone41251e2008-04-25 01:59:09 +00001385 tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use
1386 :meth:`time.gmtime`).
Georg Brandl116aa622007-08-15 14:28:22 +00001387
1388
Benjamin Petersone41251e2008-04-25 01:59:09 +00001389 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +00001390
Benjamin Petersone41251e2008-04-25 01:59:09 +00001391 Return a string specifying the flags that are currently set. If the
1392 message complies with the conventional format, the result is the
1393 concatenation in the following order of zero or one occurrence of each of
1394 ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001395
1396
Benjamin Petersone41251e2008-04-25 01:59:09 +00001397 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +00001398
Benjamin Petersone41251e2008-04-25 01:59:09 +00001399 Set the flags specified by *flags* and unset all others. Parameter *flags*
1400 should be the concatenation in any order of zero or more occurrences of
1401 each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001402
1403
Benjamin Petersone41251e2008-04-25 01:59:09 +00001404 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001405
Benjamin Petersone41251e2008-04-25 01:59:09 +00001406 Set the flag(s) specified by *flag* without changing other flags. To add
1407 more than one flag at a time, *flag* may be a string of more than one
1408 character.
Georg Brandl116aa622007-08-15 14:28:22 +00001409
1410
Benjamin Petersone41251e2008-04-25 01:59:09 +00001411 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001412
Benjamin Petersone41251e2008-04-25 01:59:09 +00001413 Unset the flag(s) specified by *flag* without changing other flags. To
1414 remove more than one flag at a time, *flag* maybe a string of more than
1415 one character.
Georg Brandl116aa622007-08-15 14:28:22 +00001416
1417When an :class:`MMDFMessage` instance is created based upon a
1418:class:`MaildirMessage` instance, a "From " line is generated based upon the
1419:class:`MaildirMessage` instance's delivery date, and the following conversions
1420take place:
1421
1422+-----------------+-------------------------------+
1423| Resulting state | :class:`MaildirMessage` state |
1424+=================+===============================+
1425| R flag | S flag |
1426+-----------------+-------------------------------+
1427| O flag | "cur" subdirectory |
1428+-----------------+-------------------------------+
1429| D flag | T flag |
1430+-----------------+-------------------------------+
1431| F flag | F flag |
1432+-----------------+-------------------------------+
1433| A flag | R flag |
1434+-----------------+-------------------------------+
1435
1436When an :class:`MMDFMessage` instance is created based upon an
1437:class:`MHMessage` instance, the following conversions take place:
1438
1439+-------------------+--------------------------+
1440| Resulting state | :class:`MHMessage` state |
1441+===================+==========================+
1442| R flag and O flag | no "unseen" sequence |
1443+-------------------+--------------------------+
1444| O flag | "unseen" sequence |
1445+-------------------+--------------------------+
1446| F flag | "flagged" sequence |
1447+-------------------+--------------------------+
1448| A flag | "replied" sequence |
1449+-------------------+--------------------------+
1450
1451When an :class:`MMDFMessage` instance is created based upon a
1452:class:`BabylMessage` instance, the following conversions take place:
1453
1454+-------------------+-----------------------------+
1455| Resulting state | :class:`BabylMessage` state |
1456+===================+=============================+
1457| R flag and O flag | no "unseen" label |
1458+-------------------+-----------------------------+
1459| O flag | "unseen" label |
1460+-------------------+-----------------------------+
1461| D flag | "deleted" label |
1462+-------------------+-----------------------------+
1463| A flag | "answered" label |
1464+-------------------+-----------------------------+
1465
1466When an :class:`MMDFMessage` instance is created based upon an
1467:class:`mboxMessage` instance, the "From " line is copied and all flags directly
1468correspond:
1469
1470+-----------------+----------------------------+
1471| Resulting state | :class:`mboxMessage` state |
1472+=================+============================+
1473| R flag | R flag |
1474+-----------------+----------------------------+
1475| O flag | O flag |
1476+-----------------+----------------------------+
1477| D flag | D flag |
1478+-----------------+----------------------------+
1479| F flag | F flag |
1480+-----------------+----------------------------+
1481| A flag | A flag |
1482+-----------------+----------------------------+
1483
1484
1485Exceptions
1486----------
1487
1488The following exception classes are defined in the :mod:`mailbox` module:
1489
1490
Benjamin Petersone41251e2008-04-25 01:59:09 +00001491.. exception:: Error()
Georg Brandl116aa622007-08-15 14:28:22 +00001492
1493 The based class for all other module-specific exceptions.
1494
1495
Benjamin Petersone41251e2008-04-25 01:59:09 +00001496.. exception:: NoSuchMailboxError()
Georg Brandl116aa622007-08-15 14:28:22 +00001497
1498 Raised when a mailbox is expected but is not found, such as when instantiating a
1499 :class:`Mailbox` subclass with a path that does not exist (and with the *create*
1500 parameter set to ``False``), or when opening a folder that does not exist.
1501
1502
Georg Brandl734e2682008-08-12 08:18:18 +00001503.. exception:: NotEmptyError()
Georg Brandl116aa622007-08-15 14:28:22 +00001504
1505 Raised when a mailbox is not empty but is expected to be, such as when deleting
1506 a folder that contains messages.
1507
1508
Benjamin Petersone41251e2008-04-25 01:59:09 +00001509.. exception:: ExternalClashError()
Georg Brandl116aa622007-08-15 14:28:22 +00001510
1511 Raised when some mailbox-related condition beyond the control of the program
1512 causes it to be unable to proceed, such as when failing to acquire a lock that
1513 another program already holds a lock, or when a uniquely-generated file name
1514 already exists.
1515
1516
Benjamin Petersone41251e2008-04-25 01:59:09 +00001517.. exception:: FormatError()
Georg Brandl116aa622007-08-15 14:28:22 +00001518
1519 Raised when the data in a file cannot be parsed, such as when an :class:`MH`
1520 instance attempts to read a corrupted :file:`.mh_sequences` file.
1521
1522
Georg Brandl116aa622007-08-15 14:28:22 +00001523.. _mailbox-examples:
1524
1525Examples
1526--------
1527
1528A simple example of printing the subjects of all messages in a mailbox that seem
1529interesting::
1530
1531 import mailbox
1532 for message in mailbox.mbox('~/mbox'):
1533 subject = message['subject'] # Could possibly be None.
1534 if subject and 'python' in subject.lower():
Georg Brandl6911e3c2007-09-04 07:15:32 +00001535 print(subject)
Georg Brandl116aa622007-08-15 14:28:22 +00001536
1537To copy all mail from a Babyl mailbox to an MH mailbox, converting all of the
1538format-specific information that can be converted::
1539
1540 import mailbox
1541 destination = mailbox.MH('~/Mail')
1542 destination.lock()
1543 for message in mailbox.Babyl('~/RMAIL'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001544 destination.add(mailbox.MHMessage(message))
Georg Brandl116aa622007-08-15 14:28:22 +00001545 destination.flush()
1546 destination.unlock()
1547
1548This example sorts mail from several mailing lists into different mailboxes,
1549being careful to avoid mail corruption due to concurrent modification by other
1550programs, mail loss due to interruption of the program, or premature termination
1551due to malformed messages in the mailbox::
1552
1553 import mailbox
Ezio Melotti956040a2013-12-14 12:42:29 +02001554 import email.errors
Georg Brandl116aa622007-08-15 14:28:22 +00001555
1556 list_names = ('python-list', 'python-dev', 'python-bugs')
1557
Georg Brandlf6945182008-02-01 11:56:49 +00001558 boxes = {name: mailbox.mbox('~/email/%s' % name) for name in list_names}
Georg Brandl116aa622007-08-15 14:28:22 +00001559 inbox = mailbox.Maildir('~/Maildir', factory=None)
1560
1561 for key in inbox.iterkeys():
1562 try:
1563 message = inbox[key]
Ezio Melotti956040a2013-12-14 12:42:29 +02001564 except email.errors.MessageParseError:
Georg Brandl116aa622007-08-15 14:28:22 +00001565 continue # The message is malformed. Just leave it.
1566
1567 for name in list_names:
1568 list_id = message['list-id']
1569 if list_id and name in list_id:
1570 # Get mailbox to use
1571 box = boxes[name]
1572
1573 # Write copy to disk before removing original.
1574 # If there's a crash, you might duplicate a message, but
1575 # that's better than losing a message completely.
1576 box.lock()
1577 box.add(message)
Georg Brandl48310cd2009-01-03 21:18:54 +00001578 box.flush()
Georg Brandl116aa622007-08-15 14:28:22 +00001579 box.unlock()
1580
1581 # Remove original message
1582 inbox.lock()
1583 inbox.discard(key)
1584 inbox.flush()
1585 inbox.unlock()
1586 break # Found destination, so stop looking.
1587
1588 for box in boxes.itervalues():
1589 box.close()
1590