blob: f76c97ae19ea231ba2fad35bea735dc502463ea5 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`mailbox` --- Manipulate mailboxes in various formats
3==========================================================
4
5.. module:: mailbox
6 :synopsis: Manipulate mailboxes in various formats
7.. moduleauthor:: Gregory K. Johnson <gkj@gregorykjohnson.com>
8.. sectionauthor:: Gregory K. Johnson <gkj@gregorykjohnson.com>
9
10
11This module defines two classes, :class:`Mailbox` and :class:`Message`, for
12accessing and manipulating on-disk mailboxes and the messages they contain.
13:class:`Mailbox` offers a dictionary-like mapping from keys to messages.
14:class:`Message` extends the :mod:`email.Message` module's :class:`Message`
15class with format-specific state and behavior. Supported mailbox formats are
16Maildir, mbox, MH, Babyl, and MMDF.
17
18
19.. seealso::
20
21 Module :mod:`email`
22 Represent and manipulate messages.
23
24
25.. _mailbox-objects:
26
27:class:`Mailbox` objects
28------------------------
29
30
31.. class:: Mailbox
32
33 A mailbox, which may be inspected and modified.
34
Benjamin Petersone41251e2008-04-25 01:59:09 +000035 The :class:`Mailbox` class defines an interface and is not intended to be
36 instantiated. Instead, format-specific subclasses should inherit from
37 :class:`Mailbox` and your code should instantiate a particular subclass.
Georg Brandl116aa622007-08-15 14:28:22 +000038
Benjamin Petersone41251e2008-04-25 01:59:09 +000039 The :class:`Mailbox` interface is dictionary-like, with small keys
40 corresponding to messages. Keys are issued by the :class:`Mailbox` instance
41 with which they will be used and are only meaningful to that :class:`Mailbox`
42 instance. A key continues to identify a message even if the corresponding
43 message is modified, such as by replacing it with another message.
Georg Brandl116aa622007-08-15 14:28:22 +000044
Benjamin Petersone41251e2008-04-25 01:59:09 +000045 Messages may be added to a :class:`Mailbox` instance using the set-like
46 method :meth:`add` and removed using a ``del`` statement or the set-like
47 methods :meth:`remove` and :meth:`discard`.
Georg Brandl116aa622007-08-15 14:28:22 +000048
Benjamin Petersone41251e2008-04-25 01:59:09 +000049 :class:`Mailbox` interface semantics differ from dictionary semantics in some
50 noteworthy ways. Each time a message is requested, a new representation
51 (typically a :class:`Message` instance) is generated based upon the current
52 state of the mailbox. Similarly, when a message is added to a
53 :class:`Mailbox` instance, the provided message representation's contents are
54 copied. In neither case is a reference to the message representation kept by
55 the :class:`Mailbox` instance.
Georg Brandl116aa622007-08-15 14:28:22 +000056
Benjamin Petersone41251e2008-04-25 01:59:09 +000057 The default :class:`Mailbox` iterator iterates over message representations,
58 not keys as the default dictionary iterator does. Moreover, modification of a
59 mailbox during iteration is safe and well-defined. Messages added to the
60 mailbox after an iterator is created will not be seen by the
61 iterator. Messages removed from the mailbox before the iterator yields them
62 will be silently skipped, though using a key from an iterator may result in a
63 :exc:`KeyError` exception if the corresponding message is subsequently
64 removed.
Georg Brandl116aa622007-08-15 14:28:22 +000065
Benjamin Petersone41251e2008-04-25 01:59:09 +000066 .. warning::
Georg Brandl116aa622007-08-15 14:28:22 +000067
Benjamin Petersone41251e2008-04-25 01:59:09 +000068 Be very cautious when modifying mailboxes that might be simultaneously
69 changed by some other process. The safest mailbox format to use for such
70 tasks is Maildir; try to avoid using single-file formats such as mbox for
71 concurrent writing. If you're modifying a mailbox, you *must* lock it by
72 calling the :meth:`lock` and :meth:`unlock` methods *before* reading any
73 messages in the file or making any changes by adding or deleting a
74 message. Failing to lock the mailbox runs the risk of losing messages or
75 corrupting the entire mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +000076
Benjamin Petersone41251e2008-04-25 01:59:09 +000077 :class:`Mailbox` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000078
79
Benjamin Petersone41251e2008-04-25 01:59:09 +000080 .. method:: add(message)
Georg Brandl116aa622007-08-15 14:28:22 +000081
Benjamin Petersone41251e2008-04-25 01:59:09 +000082 Add *message* to the mailbox and return the key that has been assigned to
83 it.
Georg Brandl116aa622007-08-15 14:28:22 +000084
Benjamin Petersone41251e2008-04-25 01:59:09 +000085 Parameter *message* may be a :class:`Message` instance, an
86 :class:`email.Message.Message` instance, a string, or a file-like object
87 (which should be open in text mode). If *message* is an instance of the
88 appropriate format-specific :class:`Message` subclass (e.g., if it's an
89 :class:`mboxMessage` instance and this is an :class:`mbox` instance), its
90 format-specific information is used. Otherwise, reasonable defaults for
91 format-specific information are used.
Georg Brandl116aa622007-08-15 14:28:22 +000092
93
Benjamin Petersone41251e2008-04-25 01:59:09 +000094 .. method:: remove(key)
95 __delitem__(key)
96 discard(key)
Georg Brandl116aa622007-08-15 14:28:22 +000097
Benjamin Petersone41251e2008-04-25 01:59:09 +000098 Delete the message corresponding to *key* from the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +000099
Benjamin Petersone41251e2008-04-25 01:59:09 +0000100 If no such message exists, a :exc:`KeyError` exception is raised if the
101 method was called as :meth:`remove` or :meth:`__delitem__` but no
102 exception is raised if the method was called as :meth:`discard`. The
103 behavior of :meth:`discard` may be preferred if the underlying mailbox
104 format supports concurrent modification by other processes.
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106
Benjamin Petersone41251e2008-04-25 01:59:09 +0000107 .. method:: __setitem__(key, message)
Georg Brandl116aa622007-08-15 14:28:22 +0000108
Benjamin Petersone41251e2008-04-25 01:59:09 +0000109 Replace the message corresponding to *key* with *message*. Raise a
110 :exc:`KeyError` exception if no message already corresponds to *key*.
Georg Brandl116aa622007-08-15 14:28:22 +0000111
Benjamin Petersone41251e2008-04-25 01:59:09 +0000112 As with :meth:`add`, parameter *message* may be a :class:`Message`
113 instance, an :class:`email.Message.Message` instance, a string, or a
114 file-like object (which should be open in text mode). If *message* is an
115 instance of the appropriate format-specific :class:`Message` subclass
116 (e.g., if it's an :class:`mboxMessage` instance and this is an
117 :class:`mbox` instance), its format-specific information is
118 used. Otherwise, the format-specific information of the message that
119 currently corresponds to *key* is left unchanged.
Georg Brandl116aa622007-08-15 14:28:22 +0000120
121
Benjamin Petersone41251e2008-04-25 01:59:09 +0000122 .. method:: iterkeys()
123 keys()
Georg Brandl116aa622007-08-15 14:28:22 +0000124
Benjamin Petersone41251e2008-04-25 01:59:09 +0000125 Return an iterator over all keys if called as :meth:`iterkeys` or return a
126 list of keys if called as :meth:`keys`.
Georg Brandl116aa622007-08-15 14:28:22 +0000127
128
Benjamin Petersone41251e2008-04-25 01:59:09 +0000129 .. method:: itervalues()
130 __iter__()
131 values()
Georg Brandl116aa622007-08-15 14:28:22 +0000132
Benjamin Petersone41251e2008-04-25 01:59:09 +0000133 Return an iterator over representations of all messages if called as
134 :meth:`itervalues` or :meth:`__iter__` or return a list of such
135 representations if called as :meth:`values`. The messages are represented
136 as instances of the appropriate format-specific :class:`Message` subclass
137 unless a custom message factory was specified when the :class:`Mailbox`
138 instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000139
Benjamin Petersone41251e2008-04-25 01:59:09 +0000140 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000141
Benjamin Petersone41251e2008-04-25 01:59:09 +0000142 The behavior of :meth:`__iter__` is unlike that of dictionaries, which
143 iterate over keys.
Georg Brandl116aa622007-08-15 14:28:22 +0000144
145
Benjamin Petersone41251e2008-04-25 01:59:09 +0000146 .. method:: iteritems()
147 items()
Georg Brandl116aa622007-08-15 14:28:22 +0000148
Benjamin Petersone41251e2008-04-25 01:59:09 +0000149 Return an iterator over (*key*, *message*) pairs, where *key* is a key and
150 *message* is a message representation, if called as :meth:`iteritems` or
151 return a list of such pairs if called as :meth:`items`. The messages are
152 represented as instances of the appropriate format-specific
153 :class:`Message` subclass unless a custom message factory was specified
154 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000155
156
Benjamin Petersone41251e2008-04-25 01:59:09 +0000157 .. method:: get(key[, default=None])
158 __getitem__(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000159
Benjamin Petersone41251e2008-04-25 01:59:09 +0000160 Return a representation of the message corresponding to *key*. If no such
161 message exists, *default* is returned if the method was called as
162 :meth:`get` and a :exc:`KeyError` exception is raised if the method was
163 called as :meth:`__getitem__`. The message is represented as an instance
164 of the appropriate format-specific :class:`Message` subclass unless a
165 custom message factory was specified when the :class:`Mailbox` instance
166 was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000167
168
Benjamin Petersone41251e2008-04-25 01:59:09 +0000169 .. method:: get_message(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000170
Benjamin Petersone41251e2008-04-25 01:59:09 +0000171 Return a representation of the message corresponding to *key* as an
172 instance of the appropriate format-specific :class:`Message` subclass, or
173 raise a :exc:`KeyError` exception if no such message exists.
Georg Brandl116aa622007-08-15 14:28:22 +0000174
175
Benjamin Petersone41251e2008-04-25 01:59:09 +0000176 .. method:: get_string(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000177
Benjamin Petersone41251e2008-04-25 01:59:09 +0000178 Return a string representation of the message corresponding to *key*, or
179 raise a :exc:`KeyError` exception if no such message exists.
Georg Brandl116aa622007-08-15 14:28:22 +0000180
181
Benjamin Petersone41251e2008-04-25 01:59:09 +0000182 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000183
Benjamin Petersone41251e2008-04-25 01:59:09 +0000184 Return a file-like representation of the message corresponding to *key*,
185 or raise a :exc:`KeyError` exception if no such message exists. The
186 file-like object behaves as if open in binary mode. This file should be
187 closed once it is no longer needed.
Georg Brandl116aa622007-08-15 14:28:22 +0000188
Benjamin Petersone41251e2008-04-25 01:59:09 +0000189 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000190
Benjamin Petersone41251e2008-04-25 01:59:09 +0000191 Unlike other representations of messages, file-like representations are
192 not necessarily independent of the :class:`Mailbox` instance that
193 created them or of the underlying mailbox. More specific documentation
194 is provided by each subclass.
Georg Brandl116aa622007-08-15 14:28:22 +0000195
196
Benjamin Petersone41251e2008-04-25 01:59:09 +0000197 .. method:: __contains__(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000198
Benjamin Petersone41251e2008-04-25 01:59:09 +0000199 Return ``True`` if *key* corresponds to a message, ``False`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000200
201
Benjamin Petersone41251e2008-04-25 01:59:09 +0000202 .. method:: __len__()
Georg Brandl116aa622007-08-15 14:28:22 +0000203
Benjamin Petersone41251e2008-04-25 01:59:09 +0000204 Return a count of messages in the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000205
206
Benjamin Petersone41251e2008-04-25 01:59:09 +0000207 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +0000208
Benjamin Petersone41251e2008-04-25 01:59:09 +0000209 Delete all messages from the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000210
211
Benjamin Petersone41251e2008-04-25 01:59:09 +0000212 .. method:: pop(key[, default])
Georg Brandl116aa622007-08-15 14:28:22 +0000213
Benjamin Petersone41251e2008-04-25 01:59:09 +0000214 Return a representation of the message corresponding to *key* and delete
215 the message. If no such message exists, return *default* if it was
216 supplied or else raise a :exc:`KeyError` exception. The message is
217 represented as an instance of the appropriate format-specific
218 :class:`Message` subclass unless a custom message factory was specified
219 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000220
221
Benjamin Petersone41251e2008-04-25 01:59:09 +0000222 .. method:: popitem()
Georg Brandl116aa622007-08-15 14:28:22 +0000223
Benjamin Petersone41251e2008-04-25 01:59:09 +0000224 Return an arbitrary (*key*, *message*) pair, where *key* is a key and
225 *message* is a message representation, and delete the corresponding
226 message. If the mailbox is empty, raise a :exc:`KeyError` exception. The
227 message is represented as an instance of the appropriate format-specific
228 :class:`Message` subclass unless a custom message factory was specified
229 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000230
231
Benjamin Petersone41251e2008-04-25 01:59:09 +0000232 .. method:: update(arg)
Georg Brandl116aa622007-08-15 14:28:22 +0000233
Benjamin Petersone41251e2008-04-25 01:59:09 +0000234 Parameter *arg* should be a *key*-to-*message* mapping or an iterable of
235 (*key*, *message*) pairs. Updates the mailbox so that, for each given
236 *key* and *message*, the message corresponding to *key* is set to
237 *message* as if by using :meth:`__setitem__`. As with :meth:`__setitem__`,
238 each *key* must already correspond to a message in the mailbox or else a
239 :exc:`KeyError` exception will be raised, so in general it is incorrect
240 for *arg* to be a :class:`Mailbox` instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000241
Benjamin Petersone41251e2008-04-25 01:59:09 +0000242 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000243
Benjamin Petersone41251e2008-04-25 01:59:09 +0000244 Unlike with dictionaries, keyword arguments are not supported.
Georg Brandl116aa622007-08-15 14:28:22 +0000245
246
Benjamin Petersone41251e2008-04-25 01:59:09 +0000247 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000248
Benjamin Petersone41251e2008-04-25 01:59:09 +0000249 Write any pending changes to the filesystem. For some :class:`Mailbox`
250 subclasses, changes are always written immediately and :meth:`flush` does
251 nothing, but you should still make a habit of calling this method.
Georg Brandl116aa622007-08-15 14:28:22 +0000252
253
Benjamin Petersone41251e2008-04-25 01:59:09 +0000254 .. method:: lock()
Georg Brandl116aa622007-08-15 14:28:22 +0000255
Benjamin Petersone41251e2008-04-25 01:59:09 +0000256 Acquire an exclusive advisory lock on the mailbox so that other processes
257 know not to modify it. An :exc:`ExternalClashError` is raised if the lock
258 is not available. The particular locking mechanisms used depend upon the
259 mailbox format. You should *always* lock the mailbox before making any
260 modifications to its contents.
Georg Brandl116aa622007-08-15 14:28:22 +0000261
262
Benjamin Petersone41251e2008-04-25 01:59:09 +0000263 .. method:: unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000264
Benjamin Petersone41251e2008-04-25 01:59:09 +0000265 Release the lock on the mailbox, if any.
Georg Brandl116aa622007-08-15 14:28:22 +0000266
267
Benjamin Petersone41251e2008-04-25 01:59:09 +0000268 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000269
Benjamin Petersone41251e2008-04-25 01:59:09 +0000270 Flush the mailbox, unlock it if necessary, and close any open files. For
271 some :class:`Mailbox` subclasses, this method does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000272
273
274.. _mailbox-maildir:
275
276:class:`Maildir`
277^^^^^^^^^^^^^^^^
278
279
280.. class:: Maildir(dirname[, factory=rfc822.Message[, create=True]])
281
282 A subclass of :class:`Mailbox` for mailboxes in Maildir format. Parameter
283 *factory* is a callable object that accepts a file-like message representation
284 (which behaves as if opened in binary mode) and returns a custom representation.
285 If *factory* is ``None``, :class:`MaildirMessage` is used as the default message
286 representation. If *create* is ``True``, the mailbox is created if it does not
287 exist.
288
289 It is for historical reasons that *factory* defaults to :class:`rfc822.Message`
290 and that *dirname* is named as such rather than *path*. For a :class:`Maildir`
291 instance that behaves like instances of other :class:`Mailbox` subclasses, set
292 *factory* to ``None``.
293
Benjamin Petersone41251e2008-04-25 01:59:09 +0000294 Maildir is a directory-based mailbox format invented for the qmail mail
295 transfer agent and now widely supported by other programs. Messages in a
296 Maildir mailbox are stored in separate files within a common directory
297 structure. This design allows Maildir mailboxes to be accessed and modified
298 by multiple unrelated programs without data corruption, so file locking is
299 unnecessary.
Georg Brandl116aa622007-08-15 14:28:22 +0000300
Benjamin Petersone41251e2008-04-25 01:59:09 +0000301 Maildir mailboxes contain three subdirectories, namely: :file:`tmp`,
302 :file:`new`, and :file:`cur`. Messages are created momentarily in the
303 :file:`tmp` subdirectory and then moved to the :file:`new` subdirectory to
304 finalize delivery. A mail user agent may subsequently move the message to the
305 :file:`cur` subdirectory and store information about the state of the message
306 in a special "info" section appended to its file name.
Georg Brandl116aa622007-08-15 14:28:22 +0000307
Benjamin Petersone41251e2008-04-25 01:59:09 +0000308 Folders of the style introduced by the Courier mail transfer agent are also
309 supported. Any subdirectory of the main mailbox is considered a folder if
310 ``'.'`` is the first character in its name. Folder names are represented by
311 :class:`Maildir` without the leading ``'.'``. Each folder is itself a Maildir
312 mailbox but should not contain other folders. Instead, a logical nesting is
313 indicated using ``'.'`` to delimit levels, e.g., "Archived.2005.07".
Georg Brandl116aa622007-08-15 14:28:22 +0000314
Benjamin Petersone41251e2008-04-25 01:59:09 +0000315 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000316
Benjamin Petersone41251e2008-04-25 01:59:09 +0000317 The Maildir specification requires the use of a colon (``':'``) in certain
318 message file names. However, some operating systems do not permit this
319 character in file names, If you wish to use a Maildir-like format on such
320 an operating system, you should specify another character to use
321 instead. The exclamation point (``'!'``) is a popular choice. For
322 example::
Georg Brandl116aa622007-08-15 14:28:22 +0000323
Benjamin Petersone41251e2008-04-25 01:59:09 +0000324 import mailbox
325 mailbox.Maildir.colon = '!'
Georg Brandl116aa622007-08-15 14:28:22 +0000326
Benjamin Petersone41251e2008-04-25 01:59:09 +0000327 The :attr:`colon` attribute may also be set on a per-instance basis.
Georg Brandl116aa622007-08-15 14:28:22 +0000328
Benjamin Petersone41251e2008-04-25 01:59:09 +0000329 :class:`Maildir` instances have all of the methods of :class:`Mailbox` in
330 addition to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000331
332
Benjamin Petersone41251e2008-04-25 01:59:09 +0000333 .. method:: list_folders()
Georg Brandl116aa622007-08-15 14:28:22 +0000334
Benjamin Petersone41251e2008-04-25 01:59:09 +0000335 Return a list of the names of all folders.
Georg Brandl116aa622007-08-15 14:28:22 +0000336
337
Benjamin Petersone41251e2008-04-25 01:59:09 +0000338 .. method:: .et_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000339
Benjamin Petersone41251e2008-04-25 01:59:09 +0000340 Return a :class:`Maildir` instance representing the folder whose name is
341 *folder*. A :exc:`NoSuchMailboxError` exception is raised if the folder
342 does not exist.
Georg Brandl116aa622007-08-15 14:28:22 +0000343
344
Benjamin Petersone41251e2008-04-25 01:59:09 +0000345 .. method:: add_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000346
Benjamin Petersone41251e2008-04-25 01:59:09 +0000347 Create a folder whose name is *folder* and return a :class:`Maildir`
348 instance representing it.
Georg Brandl116aa622007-08-15 14:28:22 +0000349
350
Benjamin Petersone41251e2008-04-25 01:59:09 +0000351 .. method:: remove_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000352
Benjamin Petersone41251e2008-04-25 01:59:09 +0000353 Delete the folder whose name is *folder*. If the folder contains any
354 messages, a :exc:`NotEmptyError` exception will be raised and the folder
355 will not be deleted.
Georg Brandl116aa622007-08-15 14:28:22 +0000356
357
Benjamin Petersone41251e2008-04-25 01:59:09 +0000358 .. method:: clean()
Georg Brandl116aa622007-08-15 14:28:22 +0000359
Benjamin Petersone41251e2008-04-25 01:59:09 +0000360 Delete temporary files from the mailbox that have not been accessed in the
361 last 36 hours. The Maildir specification says that mail-reading programs
362 should do this occasionally.
Georg Brandl116aa622007-08-15 14:28:22 +0000363
Benjamin Petersone41251e2008-04-25 01:59:09 +0000364 Some :class:`Mailbox` methods implemented by :class:`Maildir` deserve special
365 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000366
367
Benjamin Petersone41251e2008-04-25 01:59:09 +0000368 .. method:: add(message)
369 __setitem__(key, message)
370 update(arg)
Georg Brandl116aa622007-08-15 14:28:22 +0000371
Benjamin Petersone41251e2008-04-25 01:59:09 +0000372 .. warning::
Georg Brandl116aa622007-08-15 14:28:22 +0000373
Benjamin Petersone41251e2008-04-25 01:59:09 +0000374 These methods generate unique file names based upon the current process
375 ID. When using multiple threads, undetected name clashes may occur and
376 cause corruption of the mailbox unless threads are coordinated to avoid
377 using these methods to manipulate the same mailbox simultaneously.
Georg Brandl116aa622007-08-15 14:28:22 +0000378
379
Benjamin Petersone41251e2008-04-25 01:59:09 +0000380 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000381
Benjamin Petersone41251e2008-04-25 01:59:09 +0000382 All changes to Maildir mailboxes are immediately applied, so this method
383 does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000384
385
Benjamin Petersone41251e2008-04-25 01:59:09 +0000386 .. method:: lock()
387 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000388
Benjamin Petersone41251e2008-04-25 01:59:09 +0000389 Maildir mailboxes do not support (or require) locking, so these methods do
390 nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000391
392
Benjamin Petersone41251e2008-04-25 01:59:09 +0000393 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000394
Benjamin Petersone41251e2008-04-25 01:59:09 +0000395 :class:`Maildir` instances do not keep any open files and the underlying
396 mailboxes do not support locking, so this method does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000397
398
Benjamin Petersone41251e2008-04-25 01:59:09 +0000399 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000400
Benjamin Petersone41251e2008-04-25 01:59:09 +0000401 Depending upon the host platform, it may not be possible to modify or
402 remove the underlying message while the returned file remains open.
Georg Brandl116aa622007-08-15 14:28:22 +0000403
404
405.. seealso::
406
407 `maildir man page from qmail <http://www.qmail.org/man/man5/maildir.html>`_
408 The original specification of the format.
409
410 `Using maildir format <http://cr.yp.to/proto/maildir.html>`_
411 Notes on Maildir by its inventor. Includes an updated name-creation scheme and
412 details on "info" semantics.
413
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000414 `maildir man page from Courier <http://www.courier-mta.org/maildir.html>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000415 Another specification of the format. Describes a common extension for supporting
416 folders.
417
418
419.. _mailbox-mbox:
420
421:class:`mbox`
422^^^^^^^^^^^^^
423
424
425.. class:: mbox(path[, factory=None[, create=True]])
426
427 A subclass of :class:`Mailbox` for mailboxes in mbox format. Parameter *factory*
428 is a callable object that accepts a file-like message representation (which
429 behaves as if opened in binary mode) and returns a custom representation. If
430 *factory* is ``None``, :class:`mboxMessage` is used as the default message
431 representation. If *create* is ``True``, the mailbox is created if it does not
432 exist.
433
Benjamin Petersone41251e2008-04-25 01:59:09 +0000434 The mbox format is the classic format for storing mail on Unix systems. All
435 messages in an mbox mailbox are stored in a single file with the beginning of
436 each message indicated by a line whose first five characters are "From ".
Georg Brandl116aa622007-08-15 14:28:22 +0000437
Benjamin Petersone41251e2008-04-25 01:59:09 +0000438 Several variations of the mbox format exist to address perceived shortcomings in
439 the original. In the interest of compatibility, :class:`mbox` implements the
440 original format, which is sometimes referred to as :dfn:`mboxo`. This means that
441 the :mailheader:`Content-Length` header, if present, is ignored and that any
442 occurrences of "From " at the beginning of a line in a message body are
443 transformed to ">From " when storing the message, although occurrences of ">From
444 " are not transformed to "From " when reading the message.
Georg Brandl116aa622007-08-15 14:28:22 +0000445
Benjamin Petersone41251e2008-04-25 01:59:09 +0000446 Some :class:`Mailbox` methods implemented by :class:`mbox` deserve special
447 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000448
449
Benjamin Petersone41251e2008-04-25 01:59:09 +0000450 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000451
Benjamin Petersone41251e2008-04-25 01:59:09 +0000452 Using the file after calling :meth:`flush` or :meth:`close` on the
453 :class:`mbox` instance may yield unpredictable results or raise an
454 exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000455
456
Benjamin Petersone41251e2008-04-25 01:59:09 +0000457 .. method:: lock()
458 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000459
Benjamin Petersone41251e2008-04-25 01:59:09 +0000460 Three locking mechanisms are used---dot locking and, if available, the
461 :cfunc:`flock` and :cfunc:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000462
463
464.. seealso::
465
466 `mbox man page from qmail <http://www.qmail.org/man/man5/mbox.html>`_
467 A specification of the format and its variations.
468
469 `mbox man page from tin <http://www.tin.org/bin/man.cgi?section=5&topic=mbox>`_
470 Another specification of the format, with details on locking.
471
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000472 `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 +0000473 An argument for using the original mbox format rather than a variation.
474
475 `"mbox" is a family of several mutually incompatible mailbox formats <http://homepages.tesco.net./~J.deBoynePollard/FGA/mail-mbox-formats.html>`_
476 A history of mbox variations.
477
478
479.. _mailbox-mh:
480
481:class:`MH`
482^^^^^^^^^^^
483
484
485.. class:: MH(path[, factory=None[, create=True]])
486
487 A subclass of :class:`Mailbox` for mailboxes in MH format. Parameter *factory*
488 is a callable object that accepts a file-like message representation (which
489 behaves as if opened in binary mode) and returns a custom representation. If
490 *factory* is ``None``, :class:`MHMessage` is used as the default message
491 representation. If *create* is ``True``, the mailbox is created if it does not
492 exist.
493
Benjamin Petersone41251e2008-04-25 01:59:09 +0000494 MH is a directory-based mailbox format invented for the MH Message Handling
495 System, a mail user agent. Each message in an MH mailbox resides in its own
496 file. An MH mailbox may contain other MH mailboxes (called :dfn:`folders`) in
497 addition to messages. Folders may be nested indefinitely. MH mailboxes also
498 support :dfn:`sequences`, which are named lists used to logically group
499 messages without moving them to sub-folders. Sequences are defined in a file
500 called :file:`.mh_sequences` in each folder.
Georg Brandl116aa622007-08-15 14:28:22 +0000501
Benjamin Petersone41251e2008-04-25 01:59:09 +0000502 The :class:`MH` class manipulates MH mailboxes, but it does not attempt to
503 emulate all of :program:`mh`'s behaviors. In particular, it does not modify
504 and is not affected by the :file:`context` or :file:`.mh_profile` files that
505 are used by :program:`mh` to store its state and configuration.
Georg Brandl116aa622007-08-15 14:28:22 +0000506
Benjamin Petersone41251e2008-04-25 01:59:09 +0000507 :class:`MH` instances have all of the methods of :class:`Mailbox` in addition
508 to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000509
510
Benjamin Petersone41251e2008-04-25 01:59:09 +0000511 .. method:: list_folders()
Georg Brandl116aa622007-08-15 14:28:22 +0000512
Benjamin Petersone41251e2008-04-25 01:59:09 +0000513 Return a list of the names of all folders.
Georg Brandl116aa622007-08-15 14:28:22 +0000514
515
Benjamin Petersone41251e2008-04-25 01:59:09 +0000516 .. method:: get_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000517
Benjamin Petersone41251e2008-04-25 01:59:09 +0000518 Return an :class:`MH` instance representing the folder whose name is
519 *folder*. A :exc:`NoSuchMailboxError` exception is raised if the folder
520 does not exist.
Georg Brandl116aa622007-08-15 14:28:22 +0000521
522
Benjamin Petersone41251e2008-04-25 01:59:09 +0000523 .. method:: add_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000524
Benjamin Petersone41251e2008-04-25 01:59:09 +0000525 Create a folder whose name is *folder* and return an :class:`MH` instance
526 representing it.
Georg Brandl116aa622007-08-15 14:28:22 +0000527
528
Benjamin Petersone41251e2008-04-25 01:59:09 +0000529 .. method:: remove_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000530
Benjamin Petersone41251e2008-04-25 01:59:09 +0000531 Delete the folder whose name is *folder*. If the folder contains any
532 messages, a :exc:`NotEmptyError` exception will be raised and the folder
533 will not be deleted.
Georg Brandl116aa622007-08-15 14:28:22 +0000534
535
Benjamin Petersone41251e2008-04-25 01:59:09 +0000536 .. method:: get_sequences()
Georg Brandl116aa622007-08-15 14:28:22 +0000537
Benjamin Petersone41251e2008-04-25 01:59:09 +0000538 Return a dictionary of sequence names mapped to key lists. If there are no
539 sequences, the empty dictionary is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000540
541
Benjamin Petersone41251e2008-04-25 01:59:09 +0000542 .. method:: set_sequences(sequences)
Georg Brandl116aa622007-08-15 14:28:22 +0000543
Benjamin Petersone41251e2008-04-25 01:59:09 +0000544 Re-define the sequences that exist in the mailbox based upon *sequences*,
545 a dictionary of names mapped to key lists, like returned by
546 :meth:`get_sequences`.
Georg Brandl116aa622007-08-15 14:28:22 +0000547
548
Benjamin Petersone41251e2008-04-25 01:59:09 +0000549 .. method:: pack()
Georg Brandl116aa622007-08-15 14:28:22 +0000550
Benjamin Petersone41251e2008-04-25 01:59:09 +0000551 Rename messages in the mailbox as necessary to eliminate gaps in
552 numbering. Entries in the sequences list are updated correspondingly.
Georg Brandl116aa622007-08-15 14:28:22 +0000553
Benjamin Petersone41251e2008-04-25 01:59:09 +0000554 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000555
Benjamin Petersone41251e2008-04-25 01:59:09 +0000556 Already-issued keys are invalidated by this operation and should not be
557 subsequently used.
Georg Brandl116aa622007-08-15 14:28:22 +0000558
Benjamin Petersone41251e2008-04-25 01:59:09 +0000559 Some :class:`Mailbox` methods implemented by :class:`MH` deserve special
560 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000561
562
Benjamin Petersone41251e2008-04-25 01:59:09 +0000563 .. method:: remove(key)
564 __delitem__(key)
565 discard(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000566
Benjamin Petersone41251e2008-04-25 01:59:09 +0000567 These methods immediately delete the message. The MH convention of marking
568 a message for deletion by prepending a comma to its name is not used.
Georg Brandl116aa622007-08-15 14:28:22 +0000569
570
Benjamin Petersone41251e2008-04-25 01:59:09 +0000571 .. method:: lock()
572 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000573
Benjamin Petersone41251e2008-04-25 01:59:09 +0000574 Three locking mechanisms are used---dot locking and, if available, the
575 :cfunc:`flock` and :cfunc:`lockf` system calls. For MH mailboxes, locking
576 the mailbox means locking the :file:`.mh_sequences` file and, only for the
577 duration of any operations that affect them, locking individual message
578 files.
Georg Brandl116aa622007-08-15 14:28:22 +0000579
580
Benjamin Petersone41251e2008-04-25 01:59:09 +0000581 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000582
Benjamin Petersone41251e2008-04-25 01:59:09 +0000583 Depending upon the host platform, it may not be possible to remove the
584 underlying message while the returned file remains open.
Georg Brandl116aa622007-08-15 14:28:22 +0000585
586
Benjamin Petersone41251e2008-04-25 01:59:09 +0000587 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000588
Benjamin Petersone41251e2008-04-25 01:59:09 +0000589 All changes to MH mailboxes are immediately applied, so this method does
590 nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000591
592
Benjamin Petersone41251e2008-04-25 01:59:09 +0000593 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000594
Benjamin Petersone41251e2008-04-25 01:59:09 +0000595 :class:`MH` instances do not keep any open files, so this method is
596 equivalent to :meth:`unlock`.
Georg Brandl116aa622007-08-15 14:28:22 +0000597
598
599.. seealso::
600
601 `nmh - Message Handling System <http://www.nongnu.org/nmh/>`_
602 Home page of :program:`nmh`, an updated version of the original :program:`mh`.
603
604 `MH & nmh: Email for Users & Programmers <http://www.ics.uci.edu/~mh/book/>`_
605 A GPL-licensed book on :program:`mh` and :program:`nmh`, with some information
606 on the mailbox format.
607
608
609.. _mailbox-babyl:
610
611:class:`Babyl`
612^^^^^^^^^^^^^^
613
614
615.. class:: Babyl(path[, factory=None[, create=True]])
616
617 A subclass of :class:`Mailbox` for mailboxes in Babyl format. Parameter
618 *factory* is a callable object that accepts a file-like message representation
619 (which behaves as if opened in binary mode) and returns a custom representation.
620 If *factory* is ``None``, :class:`BabylMessage` is used as the default message
621 representation. If *create* is ``True``, the mailbox is created if it does not
622 exist.
623
Benjamin Petersone41251e2008-04-25 01:59:09 +0000624 Babyl is a single-file mailbox format used by the Rmail mail user agent
625 included with Emacs. The beginning of a message is indicated by a line
626 containing the two characters Control-Underscore (``'\037'``) and Control-L
627 (``'\014'``). The end of a message is indicated by the start of the next
628 message or, in the case of the last message, a line containing a
629 Control-Underscore (``'\037'``) character.
Georg Brandl116aa622007-08-15 14:28:22 +0000630
Benjamin Petersone41251e2008-04-25 01:59:09 +0000631 Messages in a Babyl mailbox have two sets of headers, original headers and
632 so-called visible headers. Visible headers are typically a subset of the
633 original headers that have been reformatted or abridged to be more
634 attractive. Each message in a Babyl mailbox also has an accompanying list of
635 :dfn:`labels`, or short strings that record extra information about the
636 message, and a list of all user-defined labels found in the mailbox is kept
637 in the Babyl options section.
Georg Brandl116aa622007-08-15 14:28:22 +0000638
Benjamin Petersone41251e2008-04-25 01:59:09 +0000639 :class:`Babyl` instances have all of the methods of :class:`Mailbox` in
640 addition to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000641
642
Benjamin Petersone41251e2008-04-25 01:59:09 +0000643 .. method:: get_labels()
Georg Brandl116aa622007-08-15 14:28:22 +0000644
Benjamin Petersone41251e2008-04-25 01:59:09 +0000645 Return a list of the names of all user-defined labels used in the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000646
Benjamin Petersone41251e2008-04-25 01:59:09 +0000647 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000648
Benjamin Petersone41251e2008-04-25 01:59:09 +0000649 The actual messages are inspected to determine which labels exist in
650 the mailbox rather than consulting the list of labels in the Babyl
651 options section, but the Babyl section is updated whenever the mailbox
652 is modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000653
Benjamin Petersone41251e2008-04-25 01:59:09 +0000654 Some :class:`Mailbox` methods implemented by :class:`Babyl` deserve special
655 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000656
657
Benjamin Petersone41251e2008-04-25 01:59:09 +0000658 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000659
Benjamin Petersone41251e2008-04-25 01:59:09 +0000660 In Babyl mailboxes, the headers of a message are not stored contiguously
661 with the body of the message. To generate a file-like representation, the
662 headers and body are copied together into a :class:`StringIO` instance
663 (from the :mod:`StringIO` module), which has an API identical to that of a
664 file. As a result, the file-like object is truly independent of the
665 underlying mailbox but does not save memory compared to a string
666 representation.
Georg Brandl116aa622007-08-15 14:28:22 +0000667
668
Benjamin Petersone41251e2008-04-25 01:59:09 +0000669 .. method:: lock()
670 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000671
Benjamin Petersone41251e2008-04-25 01:59:09 +0000672 Three locking mechanisms are used---dot locking and, if available, the
673 :cfunc:`flock` and :cfunc:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000674
675
676.. seealso::
677
678 `Format of Version 5 Babyl Files <http://quimby.gnus.org/notes/BABYL>`_
679 A specification of the Babyl format.
680
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000681 `Reading Mail with Rmail <http://www.gnu.org/software/emacs/manual/html_node/emacs/Rmail.html>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000682 The Rmail manual, with some information on Babyl semantics.
683
684
685.. _mailbox-mmdf:
686
687:class:`MMDF`
688^^^^^^^^^^^^^
689
690
691.. class:: MMDF(path[, factory=None[, create=True]])
692
693 A subclass of :class:`Mailbox` for mailboxes in MMDF format. Parameter *factory*
694 is a callable object that accepts a file-like message representation (which
695 behaves as if opened in binary mode) and returns a custom representation. If
696 *factory* is ``None``, :class:`MMDFMessage` is used as the default message
697 representation. If *create* is ``True``, the mailbox is created if it does not
698 exist.
699
Benjamin Petersone41251e2008-04-25 01:59:09 +0000700 MMDF is a single-file mailbox format invented for the Multichannel Memorandum
701 Distribution Facility, a mail transfer agent. Each message is in the same
702 form as an mbox message but is bracketed before and after by lines containing
703 four Control-A (``'\001'``) characters. As with the mbox format, the
704 beginning of each message is indicated by a line whose first five characters
705 are "From ", but additional occurrences of "From " are not transformed to
706 ">From " when storing messages because the extra message separator lines
707 prevent mistaking such occurrences for the starts of subsequent messages.
Georg Brandl116aa622007-08-15 14:28:22 +0000708
Benjamin Petersone41251e2008-04-25 01:59:09 +0000709 Some :class:`Mailbox` methods implemented by :class:`MMDF` deserve special
710 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000711
712
Benjamin Petersone41251e2008-04-25 01:59:09 +0000713 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000714
Benjamin Petersone41251e2008-04-25 01:59:09 +0000715 Using the file after calling :meth:`flush` or :meth:`close` on the
716 :class:`MMDF` instance may yield unpredictable results or raise an
717 exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000718
719
Benjamin Petersone41251e2008-04-25 01:59:09 +0000720 .. method:: lock()
721 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000722
Benjamin Petersone41251e2008-04-25 01:59:09 +0000723 Three locking mechanisms are used---dot locking and, if available, the
724 :cfunc:`flock` and :cfunc:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000725
726
727.. seealso::
728
729 `mmdf man page from tin <http://www.tin.org/bin/man.cgi?section=5&topic=mmdf>`_
730 A specification of MMDF format from the documentation of tin, a newsreader.
731
732 `MMDF <http://en.wikipedia.org/wiki/MMDF>`_
733 A Wikipedia article describing the Multichannel Memorandum Distribution
734 Facility.
735
736
737.. _mailbox-message-objects:
738
739:class:`Message` objects
740------------------------
741
742
743.. class:: Message([message])
744
745 A subclass of the :mod:`email.Message` module's :class:`Message`. Subclasses of
746 :class:`mailbox.Message` add mailbox-format-specific state and behavior.
747
748 If *message* is omitted, the new instance is created in a default, empty state.
749 If *message* is an :class:`email.Message.Message` instance, its contents are
750 copied; furthermore, any format-specific information is converted insofar as
751 possible if *message* is a :class:`Message` instance. If *message* is a string
752 or a file, it should contain an :rfc:`2822`\ -compliant message, which is read
753 and parsed.
754
Benjamin Petersone41251e2008-04-25 01:59:09 +0000755 The format-specific state and behaviors offered by subclasses vary, but in
756 general it is only the properties that are not specific to a particular
757 mailbox that are supported (although presumably the properties are specific
758 to a particular mailbox format). For example, file offsets for single-file
759 mailbox formats and file names for directory-based mailbox formats are not
760 retained, because they are only applicable to the original mailbox. But state
761 such as whether a message has been read by the user or marked as important is
762 retained, because it applies to the message itself.
Georg Brandl116aa622007-08-15 14:28:22 +0000763
Benjamin Petersone41251e2008-04-25 01:59:09 +0000764 There is no requirement that :class:`Message` instances be used to represent
765 messages retrieved using :class:`Mailbox` instances. In some situations, the
766 time and memory required to generate :class:`Message` representations might
767 not not acceptable. For such situations, :class:`Mailbox` instances also
768 offer string and file-like representations, and a custom message factory may
769 be specified when a :class:`Mailbox` instance is initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000770
771
772.. _mailbox-maildirmessage:
773
774:class:`MaildirMessage`
775^^^^^^^^^^^^^^^^^^^^^^^
776
777
778.. class:: MaildirMessage([message])
779
780 A message with Maildir-specific behaviors. Parameter *message* has the same
781 meaning as with the :class:`Message` constructor.
782
Benjamin Petersone41251e2008-04-25 01:59:09 +0000783 Typically, a mail user agent application moves all of the messages in the
784 :file:`new` subdirectory to the :file:`cur` subdirectory after the first time
785 the user opens and closes the mailbox, recording that the messages are old
786 whether or not they've actually been read. Each message in :file:`cur` has an
787 "info" section added to its file name to store information about its state.
788 (Some mail readers may also add an "info" section to messages in
789 :file:`new`.) The "info" section may take one of two forms: it may contain
790 "2," followed by a list of standardized flags (e.g., "2,FR") or it may
791 contain "1," followed by so-called experimental information. Standard flags
792 for Maildir messages are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000793
Benjamin Petersone41251e2008-04-25 01:59:09 +0000794 +------+---------+--------------------------------+
795 | Flag | Meaning | Explanation |
796 +======+=========+================================+
797 | D | Draft | Under composition |
798 +------+---------+--------------------------------+
799 | F | Flagged | Marked as important |
800 +------+---------+--------------------------------+
801 | P | Passed | Forwarded, resent, or bounced |
802 +------+---------+--------------------------------+
803 | R | Replied | Replied to |
804 +------+---------+--------------------------------+
805 | S | Seen | Read |
806 +------+---------+--------------------------------+
807 | T | Trashed | Marked for subsequent deletion |
808 +------+---------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000809
Benjamin Petersone41251e2008-04-25 01:59:09 +0000810 :class:`MaildirMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000811
812
Benjamin Petersone41251e2008-04-25 01:59:09 +0000813 .. method:: get_subdir()
Georg Brandl116aa622007-08-15 14:28:22 +0000814
Benjamin Petersone41251e2008-04-25 01:59:09 +0000815 Return either "new" (if the message should be stored in the :file:`new`
816 subdirectory) or "cur" (if the message should be stored in the :file:`cur`
817 subdirectory).
Georg Brandl116aa622007-08-15 14:28:22 +0000818
Benjamin Petersone41251e2008-04-25 01:59:09 +0000819 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000820
Benjamin Petersone41251e2008-04-25 01:59:09 +0000821 A message is typically moved from :file:`new` to :file:`cur` after its
822 mailbox has been accessed, whether or not the message is has been
823 read. A message ``msg`` has been read if ``"S" in msg.get_flags()`` is
824 ``True``.
Georg Brandl116aa622007-08-15 14:28:22 +0000825
826
Benjamin Petersone41251e2008-04-25 01:59:09 +0000827 .. method:: set_subdir(subdir)
Georg Brandl116aa622007-08-15 14:28:22 +0000828
Benjamin Petersone41251e2008-04-25 01:59:09 +0000829 Set the subdirectory the message should be stored in. Parameter *subdir*
830 must be either "new" or "cur".
Georg Brandl116aa622007-08-15 14:28:22 +0000831
832
Benjamin Petersone41251e2008-04-25 01:59:09 +0000833 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +0000834
Benjamin Petersone41251e2008-04-25 01:59:09 +0000835 Return a string specifying the flags that are currently set. If the
836 message complies with the standard Maildir format, the result is the
837 concatenation in alphabetical order of zero or one occurrence of each of
838 ``'D'``, ``'F'``, ``'P'``, ``'R'``, ``'S'``, and ``'T'``. The empty string
839 is returned if no flags are set or if "info" contains experimental
840 semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000841
842
Benjamin Petersone41251e2008-04-25 01:59:09 +0000843 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +0000844
Benjamin Petersone41251e2008-04-25 01:59:09 +0000845 Set the flags specified by *flags* and unset all others.
Georg Brandl116aa622007-08-15 14:28:22 +0000846
847
Benjamin Petersone41251e2008-04-25 01:59:09 +0000848 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000849
Benjamin Petersone41251e2008-04-25 01:59:09 +0000850 Set the flag(s) specified by *flag* without changing other flags. To add
851 more than one flag at a time, *flag* may be a string of more than one
852 character. The current "info" is overwritten whether or not it contains
853 experimental information rather than flags.
Georg Brandl116aa622007-08-15 14:28:22 +0000854
855
Benjamin Petersone41251e2008-04-25 01:59:09 +0000856 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000857
Benjamin Petersone41251e2008-04-25 01:59:09 +0000858 Unset the flag(s) specified by *flag* without changing other flags. To
859 remove more than one flag at a time, *flag* maybe a string of more than
860 one character. If "info" contains experimental information rather than
861 flags, the current "info" is not modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000862
863
Benjamin Petersone41251e2008-04-25 01:59:09 +0000864 .. method:: get_date()
Georg Brandl116aa622007-08-15 14:28:22 +0000865
Benjamin Petersone41251e2008-04-25 01:59:09 +0000866 Return the delivery date of the message as a floating-point number
867 representing seconds since the epoch.
Georg Brandl116aa622007-08-15 14:28:22 +0000868
869
Benjamin Petersone41251e2008-04-25 01:59:09 +0000870 .. method:: set_date(date)
Georg Brandl116aa622007-08-15 14:28:22 +0000871
Benjamin Petersone41251e2008-04-25 01:59:09 +0000872 Set the delivery date of the message to *date*, a floating-point number
873 representing seconds since the epoch.
Georg Brandl116aa622007-08-15 14:28:22 +0000874
875
Benjamin Petersone41251e2008-04-25 01:59:09 +0000876 .. method:: get_info()
Georg Brandl116aa622007-08-15 14:28:22 +0000877
Benjamin Petersone41251e2008-04-25 01:59:09 +0000878 Return a string containing the "info" for a message. This is useful for
879 accessing and modifying "info" that is experimental (i.e., not a list of
880 flags).
Georg Brandl116aa622007-08-15 14:28:22 +0000881
882
Benjamin Petersone41251e2008-04-25 01:59:09 +0000883 .. method:: set_info(info)
Georg Brandl116aa622007-08-15 14:28:22 +0000884
Benjamin Petersone41251e2008-04-25 01:59:09 +0000885 Set "info" to *info*, which should be a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000886
887When a :class:`MaildirMessage` instance is created based upon an
888:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
889and :mailheader:`X-Status` headers are omitted and the following conversions
890take place:
891
892+--------------------+----------------------------------------------+
893| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
894| | state |
895+====================+==============================================+
896| "cur" subdirectory | O flag |
897+--------------------+----------------------------------------------+
898| F flag | F flag |
899+--------------------+----------------------------------------------+
900| R flag | A flag |
901+--------------------+----------------------------------------------+
902| S flag | R flag |
903+--------------------+----------------------------------------------+
904| T flag | D flag |
905+--------------------+----------------------------------------------+
906
907When a :class:`MaildirMessage` instance is created based upon an
908:class:`MHMessage` instance, the following conversions take place:
909
910+-------------------------------+--------------------------+
911| Resulting state | :class:`MHMessage` state |
912+===============================+==========================+
913| "cur" subdirectory | "unseen" sequence |
914+-------------------------------+--------------------------+
915| "cur" subdirectory and S flag | no "unseen" sequence |
916+-------------------------------+--------------------------+
917| F flag | "flagged" sequence |
918+-------------------------------+--------------------------+
919| R flag | "replied" sequence |
920+-------------------------------+--------------------------+
921
922When a :class:`MaildirMessage` instance is created based upon a
923:class:`BabylMessage` instance, the following conversions take place:
924
925+-------------------------------+-------------------------------+
926| Resulting state | :class:`BabylMessage` state |
927+===============================+===============================+
928| "cur" subdirectory | "unseen" label |
929+-------------------------------+-------------------------------+
930| "cur" subdirectory and S flag | no "unseen" label |
931+-------------------------------+-------------------------------+
932| P flag | "forwarded" or "resent" label |
933+-------------------------------+-------------------------------+
934| R flag | "answered" label |
935+-------------------------------+-------------------------------+
936| T flag | "deleted" label |
937+-------------------------------+-------------------------------+
938
939
940.. _mailbox-mboxmessage:
941
942:class:`mboxMessage`
943^^^^^^^^^^^^^^^^^^^^
944
945
946.. class:: mboxMessage([message])
947
948 A message with mbox-specific behaviors. Parameter *message* has the same meaning
949 as with the :class:`Message` constructor.
950
Benjamin Petersone41251e2008-04-25 01:59:09 +0000951 Messages in an mbox mailbox are stored together in a single file. The
952 sender's envelope address and the time of delivery are typically stored in a
953 line beginning with "From " that is used to indicate the start of a message,
954 though there is considerable variation in the exact format of this data among
955 mbox implementations. Flags that indicate the state of the message, such as
956 whether it has been read or marked as important, are typically stored in
957 :mailheader:`Status` and :mailheader:`X-Status` headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000958
Benjamin Petersone41251e2008-04-25 01:59:09 +0000959 Conventional flags for mbox messages are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000960
Benjamin Petersone41251e2008-04-25 01:59:09 +0000961 +------+----------+--------------------------------+
962 | Flag | Meaning | Explanation |
963 +======+==========+================================+
964 | R | Read | Read |
965 +------+----------+--------------------------------+
966 | O | Old | Previously detected by MUA |
967 +------+----------+--------------------------------+
968 | D | Deleted | Marked for subsequent deletion |
969 +------+----------+--------------------------------+
970 | F | Flagged | Marked as important |
971 +------+----------+--------------------------------+
972 | A | Answered | Replied to |
973 +------+----------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000974
Benjamin Petersone41251e2008-04-25 01:59:09 +0000975 The "R" and "O" flags are stored in the :mailheader:`Status` header, and the
976 "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The
977 flags and headers typically appear in the order mentioned.
Georg Brandl116aa622007-08-15 14:28:22 +0000978
Benjamin Petersone41251e2008-04-25 01:59:09 +0000979 :class:`mboxMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000980
981
Benjamin Petersone41251e2008-04-25 01:59:09 +0000982 .. method:: get_from()
Georg Brandl116aa622007-08-15 14:28:22 +0000983
Benjamin Petersone41251e2008-04-25 01:59:09 +0000984 Return a string representing the "From " line that marks the start of the
985 message in an mbox mailbox. The leading "From " and the trailing newline
986 are excluded.
Georg Brandl116aa622007-08-15 14:28:22 +0000987
988
Benjamin Petersone41251e2008-04-25 01:59:09 +0000989 .. method:: set_from(from_[, time_=None])
Georg Brandl116aa622007-08-15 14:28:22 +0000990
Benjamin Petersone41251e2008-04-25 01:59:09 +0000991 Set the "From " line to *from_*, which should be specified without a
992 leading "From " or trailing newline. For convenience, *time_* may be
993 specified and will be formatted appropriately and appended to *from_*. If
994 *time_* is specified, it should be a :class:`struct_time` instance, a
995 tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use
996 :meth:`time.gmtime`).
Georg Brandl116aa622007-08-15 14:28:22 +0000997
998
Benjamin Petersone41251e2008-04-25 01:59:09 +0000999 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +00001000
Benjamin Petersone41251e2008-04-25 01:59:09 +00001001 Return a string specifying the flags that are currently set. If the
1002 message complies with the conventional format, the result is the
1003 concatenation in the following order of zero or one occurrence of each of
1004 ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001005
1006
Benjamin Petersone41251e2008-04-25 01:59:09 +00001007 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +00001008
Benjamin Petersone41251e2008-04-25 01:59:09 +00001009 Set the flags specified by *flags* and unset all others. Parameter *flags*
1010 should be the concatenation in any order of zero or more occurrences of
1011 each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001012
1013
Benjamin Petersone41251e2008-04-25 01:59:09 +00001014 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001015
Benjamin Petersone41251e2008-04-25 01:59:09 +00001016 Set the flag(s) specified by *flag* without changing other flags. To add
1017 more than one flag at a time, *flag* may be a string of more than one
1018 character.
Georg Brandl116aa622007-08-15 14:28:22 +00001019
1020
Benjamin Petersone41251e2008-04-25 01:59:09 +00001021 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001022
Benjamin Petersone41251e2008-04-25 01:59:09 +00001023 Unset the flag(s) specified by *flag* without changing other flags. To
1024 remove more than one flag at a time, *flag* maybe a string of more than
1025 one character.
Georg Brandl116aa622007-08-15 14:28:22 +00001026
1027When an :class:`mboxMessage` instance is created based upon a
1028:class:`MaildirMessage` instance, a "From " line is generated based upon the
1029:class:`MaildirMessage` instance's delivery date, and the following conversions
1030take place:
1031
1032+-----------------+-------------------------------+
1033| Resulting state | :class:`MaildirMessage` state |
1034+=================+===============================+
1035| R flag | S flag |
1036+-----------------+-------------------------------+
1037| O flag | "cur" subdirectory |
1038+-----------------+-------------------------------+
1039| D flag | T flag |
1040+-----------------+-------------------------------+
1041| F flag | F flag |
1042+-----------------+-------------------------------+
1043| A flag | R flag |
1044+-----------------+-------------------------------+
1045
1046When an :class:`mboxMessage` instance is created based upon an
1047:class:`MHMessage` instance, the following conversions take place:
1048
1049+-------------------+--------------------------+
1050| Resulting state | :class:`MHMessage` state |
1051+===================+==========================+
1052| R flag and O flag | no "unseen" sequence |
1053+-------------------+--------------------------+
1054| O flag | "unseen" sequence |
1055+-------------------+--------------------------+
1056| F flag | "flagged" sequence |
1057+-------------------+--------------------------+
1058| A flag | "replied" sequence |
1059+-------------------+--------------------------+
1060
1061When an :class:`mboxMessage` instance is created based upon a
1062:class:`BabylMessage` instance, the following conversions take place:
1063
1064+-------------------+-----------------------------+
1065| Resulting state | :class:`BabylMessage` state |
1066+===================+=============================+
1067| R flag and O flag | no "unseen" label |
1068+-------------------+-----------------------------+
1069| O flag | "unseen" label |
1070+-------------------+-----------------------------+
1071| D flag | "deleted" label |
1072+-------------------+-----------------------------+
1073| A flag | "answered" label |
1074+-------------------+-----------------------------+
1075
1076When a :class:`Message` instance is created based upon an :class:`MMDFMessage`
1077instance, the "From " line is copied and all flags directly correspond:
1078
1079+-----------------+----------------------------+
1080| Resulting state | :class:`MMDFMessage` state |
1081+=================+============================+
1082| R flag | R flag |
1083+-----------------+----------------------------+
1084| O flag | O flag |
1085+-----------------+----------------------------+
1086| D flag | D flag |
1087+-----------------+----------------------------+
1088| F flag | F flag |
1089+-----------------+----------------------------+
1090| A flag | A flag |
1091+-----------------+----------------------------+
1092
1093
1094.. _mailbox-mhmessage:
1095
1096:class:`MHMessage`
1097^^^^^^^^^^^^^^^^^^
1098
1099
1100.. class:: MHMessage([message])
1101
1102 A message with MH-specific behaviors. Parameter *message* has the same meaning
1103 as with the :class:`Message` constructor.
1104
Benjamin Petersone41251e2008-04-25 01:59:09 +00001105 MH messages do not support marks or flags in the traditional sense, but they
1106 do support sequences, which are logical groupings of arbitrary messages. Some
1107 mail reading programs (although not the standard :program:`mh` and
1108 :program:`nmh`) use sequences in much the same way flags are used with other
1109 formats, as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001110
Benjamin Petersone41251e2008-04-25 01:59:09 +00001111 +----------+------------------------------------------+
1112 | Sequence | Explanation |
1113 +==========+==========================================+
1114 | unseen | Not read, but previously detected by MUA |
1115 +----------+------------------------------------------+
1116 | replied | Replied to |
1117 +----------+------------------------------------------+
1118 | flagged | Marked as important |
1119 +----------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001120
Benjamin Petersone41251e2008-04-25 01:59:09 +00001121 :class:`MHMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001122
1123
Benjamin Petersone41251e2008-04-25 01:59:09 +00001124 .. method:: get_sequences()
Georg Brandl116aa622007-08-15 14:28:22 +00001125
Benjamin Petersone41251e2008-04-25 01:59:09 +00001126 Return a list of the names of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001127
1128
Benjamin Petersone41251e2008-04-25 01:59:09 +00001129 .. method:: set_sequences(sequences)
Georg Brandl116aa622007-08-15 14:28:22 +00001130
Benjamin Petersone41251e2008-04-25 01:59:09 +00001131 Set the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001132
1133
Benjamin Petersone41251e2008-04-25 01:59:09 +00001134 .. method:: add_sequence(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +00001135
Benjamin Petersone41251e2008-04-25 01:59:09 +00001136 Add *sequence* to the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001137
1138
Benjamin Petersone41251e2008-04-25 01:59:09 +00001139 .. method:: remove_sequence(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +00001140
Benjamin Petersone41251e2008-04-25 01:59:09 +00001141 Remove *sequence* from the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001142
1143When an :class:`MHMessage` instance is created based upon a
1144:class:`MaildirMessage` instance, the following conversions take place:
1145
1146+--------------------+-------------------------------+
1147| Resulting state | :class:`MaildirMessage` state |
1148+====================+===============================+
1149| "unseen" sequence | no S flag |
1150+--------------------+-------------------------------+
1151| "replied" sequence | R flag |
1152+--------------------+-------------------------------+
1153| "flagged" sequence | F flag |
1154+--------------------+-------------------------------+
1155
1156When an :class:`MHMessage` instance is created based upon an
1157:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
1158and :mailheader:`X-Status` headers are omitted and the following conversions
1159take place:
1160
1161+--------------------+----------------------------------------------+
1162| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
1163| | state |
1164+====================+==============================================+
1165| "unseen" sequence | no R flag |
1166+--------------------+----------------------------------------------+
1167| "replied" sequence | A flag |
1168+--------------------+----------------------------------------------+
1169| "flagged" sequence | F flag |
1170+--------------------+----------------------------------------------+
1171
1172When an :class:`MHMessage` instance is created based upon a
1173:class:`BabylMessage` instance, the following conversions take place:
1174
1175+--------------------+-----------------------------+
1176| Resulting state | :class:`BabylMessage` state |
1177+====================+=============================+
1178| "unseen" sequence | "unseen" label |
1179+--------------------+-----------------------------+
1180| "replied" sequence | "answered" label |
1181+--------------------+-----------------------------+
1182
1183
1184.. _mailbox-babylmessage:
1185
1186:class:`BabylMessage`
1187^^^^^^^^^^^^^^^^^^^^^
1188
1189
1190.. class:: BabylMessage([message])
1191
1192 A message with Babyl-specific behaviors. Parameter *message* has the same
1193 meaning as with the :class:`Message` constructor.
1194
Benjamin Petersone41251e2008-04-25 01:59:09 +00001195 Certain message labels, called :dfn:`attributes`, are defined by convention
1196 to have special meanings. The attributes are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001197
Benjamin Petersone41251e2008-04-25 01:59:09 +00001198 +-----------+------------------------------------------+
1199 | Label | Explanation |
1200 +===========+==========================================+
1201 | unseen | Not read, but previously detected by MUA |
1202 +-----------+------------------------------------------+
1203 | deleted | Marked for subsequent deletion |
1204 +-----------+------------------------------------------+
1205 | filed | Copied to another file or mailbox |
1206 +-----------+------------------------------------------+
1207 | answered | Replied to |
1208 +-----------+------------------------------------------+
1209 | forwarded | Forwarded |
1210 +-----------+------------------------------------------+
1211 | edited | Modified by the user |
1212 +-----------+------------------------------------------+
1213 | resent | Resent |
1214 +-----------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001215
Benjamin Petersone41251e2008-04-25 01:59:09 +00001216 By default, Rmail displays only visible headers. The :class:`BabylMessage`
1217 class, though, uses the original headers because they are more
1218 complete. Visible headers may be accessed explicitly if desired.
Georg Brandl116aa622007-08-15 14:28:22 +00001219
Benjamin Petersone41251e2008-04-25 01:59:09 +00001220 :class:`BabylMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001221
1222
Benjamin Petersone41251e2008-04-25 01:59:09 +00001223 .. method:: get_labels()
Georg Brandl116aa622007-08-15 14:28:22 +00001224
Benjamin Petersone41251e2008-04-25 01:59:09 +00001225 Return a list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001226
1227
Benjamin Petersone41251e2008-04-25 01:59:09 +00001228 .. method:: set_labels(labels)
Georg Brandl116aa622007-08-15 14:28:22 +00001229
Benjamin Petersone41251e2008-04-25 01:59:09 +00001230 Set the list of labels on the message to *labels*.
Georg Brandl116aa622007-08-15 14:28:22 +00001231
1232
Benjamin Petersone41251e2008-04-25 01:59:09 +00001233 .. method:: add_label(label)
Georg Brandl116aa622007-08-15 14:28:22 +00001234
Benjamin Petersone41251e2008-04-25 01:59:09 +00001235 Add *label* to the list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001236
1237
Benjamin Petersone41251e2008-04-25 01:59:09 +00001238 .. method:: remove_label(label)
Georg Brandl116aa622007-08-15 14:28:22 +00001239
Benjamin Petersone41251e2008-04-25 01:59:09 +00001240 Remove *label* from the list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001241
1242
Benjamin Petersone41251e2008-04-25 01:59:09 +00001243 .. method:: get_visible()
Georg Brandl116aa622007-08-15 14:28:22 +00001244
Benjamin Petersone41251e2008-04-25 01:59:09 +00001245 Return an :class:`Message` instance whose headers are the message's
1246 visible headers and whose body is empty.
Georg Brandl116aa622007-08-15 14:28:22 +00001247
1248
Benjamin Petersone41251e2008-04-25 01:59:09 +00001249 .. method:: set_visible(visible)
Georg Brandl116aa622007-08-15 14:28:22 +00001250
Benjamin Petersone41251e2008-04-25 01:59:09 +00001251 Set the message's visible headers to be the same as the headers in
1252 *message*. Parameter *visible* should be a :class:`Message` instance, an
1253 :class:`email.Message.Message` instance, a string, or a file-like object
1254 (which should be open in text mode).
Georg Brandl116aa622007-08-15 14:28:22 +00001255
1256
Benjamin Petersone41251e2008-04-25 01:59:09 +00001257 .. method:: update_visible()
Georg Brandl116aa622007-08-15 14:28:22 +00001258
Benjamin Petersone41251e2008-04-25 01:59:09 +00001259 When a :class:`BabylMessage` instance's original headers are modified, the
1260 visible headers are not automatically modified to correspond. This method
1261 updates the visible headers as follows: each visible header with a
1262 corresponding original header is set to the value of the original header,
1263 each visible header without a corresponding original header is removed,
1264 and any of :mailheader:`Date`, :mailheader:`From`, :mailheader:`Reply-To`,
1265 :mailheader:`To`, :mailheader:`CC`, and :mailheader:`Subject` that are
1266 present in the original headers but not the visible headers are added to
1267 the visible headers.
Georg Brandl116aa622007-08-15 14:28:22 +00001268
1269When a :class:`BabylMessage` instance is created based upon a
1270:class:`MaildirMessage` instance, the following conversions take place:
1271
1272+-------------------+-------------------------------+
1273| Resulting state | :class:`MaildirMessage` state |
1274+===================+===============================+
1275| "unseen" label | no S flag |
1276+-------------------+-------------------------------+
1277| "deleted" label | T flag |
1278+-------------------+-------------------------------+
1279| "answered" label | R flag |
1280+-------------------+-------------------------------+
1281| "forwarded" label | P flag |
1282+-------------------+-------------------------------+
1283
1284When a :class:`BabylMessage` instance is created based upon an
1285:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
1286and :mailheader:`X-Status` headers are omitted and the following conversions
1287take place:
1288
1289+------------------+----------------------------------------------+
1290| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
1291| | state |
1292+==================+==============================================+
1293| "unseen" label | no R flag |
1294+------------------+----------------------------------------------+
1295| "deleted" label | D flag |
1296+------------------+----------------------------------------------+
1297| "answered" label | A flag |
1298+------------------+----------------------------------------------+
1299
1300When a :class:`BabylMessage` instance is created based upon an
1301:class:`MHMessage` instance, the following conversions take place:
1302
1303+------------------+--------------------------+
1304| Resulting state | :class:`MHMessage` state |
1305+==================+==========================+
1306| "unseen" label | "unseen" sequence |
1307+------------------+--------------------------+
1308| "answered" label | "replied" sequence |
1309+------------------+--------------------------+
1310
1311
1312.. _mailbox-mmdfmessage:
1313
1314:class:`MMDFMessage`
1315^^^^^^^^^^^^^^^^^^^^
1316
1317
1318.. class:: MMDFMessage([message])
1319
1320 A message with MMDF-specific behaviors. Parameter *message* has the same meaning
1321 as with the :class:`Message` constructor.
1322
Benjamin Petersone41251e2008-04-25 01:59:09 +00001323 As with message in an mbox mailbox, MMDF messages are stored with the
1324 sender's address and the delivery date in an initial line beginning with
1325 "From ". Likewise, flags that indicate the state of the message are
1326 typically stored in :mailheader:`Status` and :mailheader:`X-Status` headers.
Georg Brandl116aa622007-08-15 14:28:22 +00001327
Benjamin Petersone41251e2008-04-25 01:59:09 +00001328 Conventional flags for MMDF messages are identical to those of mbox message
1329 and are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001330
Benjamin Petersone41251e2008-04-25 01:59:09 +00001331 +------+----------+--------------------------------+
1332 | Flag | Meaning | Explanation |
1333 +======+==========+================================+
1334 | R | Read | Read |
1335 +------+----------+--------------------------------+
1336 | O | Old | Previously detected by MUA |
1337 +------+----------+--------------------------------+
1338 | D | Deleted | Marked for subsequent deletion |
1339 +------+----------+--------------------------------+
1340 | F | Flagged | Marked as important |
1341 +------+----------+--------------------------------+
1342 | A | Answered | Replied to |
1343 +------+----------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001344
Benjamin Petersone41251e2008-04-25 01:59:09 +00001345 The "R" and "O" flags are stored in the :mailheader:`Status` header, and the
1346 "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The
1347 flags and headers typically appear in the order mentioned.
Georg Brandl116aa622007-08-15 14:28:22 +00001348
Benjamin Petersone41251e2008-04-25 01:59:09 +00001349 :class:`MMDFMessage` instances offer the following methods, which are
1350 identical to those offered by :class:`mboxMessage`:
Georg Brandl116aa622007-08-15 14:28:22 +00001351
1352
Benjamin Petersone41251e2008-04-25 01:59:09 +00001353 .. method:: get_from()
Georg Brandl116aa622007-08-15 14:28:22 +00001354
Benjamin Petersone41251e2008-04-25 01:59:09 +00001355 Return a string representing the "From " line that marks the start of the
1356 message in an mbox mailbox. The leading "From " and the trailing newline
1357 are excluded.
Georg Brandl116aa622007-08-15 14:28:22 +00001358
1359
Benjamin Petersone41251e2008-04-25 01:59:09 +00001360 .. method:: set_from(from_[, time_=None])
Georg Brandl116aa622007-08-15 14:28:22 +00001361
Benjamin Petersone41251e2008-04-25 01:59:09 +00001362 Set the "From " line to *from_*, which should be specified without a
1363 leading "From " or trailing newline. For convenience, *time_* may be
1364 specified and will be formatted appropriately and appended to *from_*. If
1365 *time_* is specified, it should be a :class:`struct_time` instance, a
1366 tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use
1367 :meth:`time.gmtime`).
Georg Brandl116aa622007-08-15 14:28:22 +00001368
1369
Benjamin Petersone41251e2008-04-25 01:59:09 +00001370 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +00001371
Benjamin Petersone41251e2008-04-25 01:59:09 +00001372 Return a string specifying the flags that are currently set. If the
1373 message complies with the conventional format, the result is the
1374 concatenation in the following order of zero or one occurrence of each of
1375 ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001376
1377
Benjamin Petersone41251e2008-04-25 01:59:09 +00001378 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +00001379
Benjamin Petersone41251e2008-04-25 01:59:09 +00001380 Set the flags specified by *flags* and unset all others. Parameter *flags*
1381 should be the concatenation in any order of zero or more occurrences of
1382 each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001383
1384
Benjamin Petersone41251e2008-04-25 01:59:09 +00001385 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001386
Benjamin Petersone41251e2008-04-25 01:59:09 +00001387 Set the flag(s) specified by *flag* without changing other flags. To add
1388 more than one flag at a time, *flag* may be a string of more than one
1389 character.
Georg Brandl116aa622007-08-15 14:28:22 +00001390
1391
Benjamin Petersone41251e2008-04-25 01:59:09 +00001392 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001393
Benjamin Petersone41251e2008-04-25 01:59:09 +00001394 Unset the flag(s) specified by *flag* without changing other flags. To
1395 remove more than one flag at a time, *flag* maybe a string of more than
1396 one character.
Georg Brandl116aa622007-08-15 14:28:22 +00001397
1398When an :class:`MMDFMessage` instance is created based upon a
1399:class:`MaildirMessage` instance, a "From " line is generated based upon the
1400:class:`MaildirMessage` instance's delivery date, and the following conversions
1401take place:
1402
1403+-----------------+-------------------------------+
1404| Resulting state | :class:`MaildirMessage` state |
1405+=================+===============================+
1406| R flag | S flag |
1407+-----------------+-------------------------------+
1408| O flag | "cur" subdirectory |
1409+-----------------+-------------------------------+
1410| D flag | T flag |
1411+-----------------+-------------------------------+
1412| F flag | F flag |
1413+-----------------+-------------------------------+
1414| A flag | R flag |
1415+-----------------+-------------------------------+
1416
1417When an :class:`MMDFMessage` instance is created based upon an
1418:class:`MHMessage` instance, the following conversions take place:
1419
1420+-------------------+--------------------------+
1421| Resulting state | :class:`MHMessage` state |
1422+===================+==========================+
1423| R flag and O flag | no "unseen" sequence |
1424+-------------------+--------------------------+
1425| O flag | "unseen" sequence |
1426+-------------------+--------------------------+
1427| F flag | "flagged" sequence |
1428+-------------------+--------------------------+
1429| A flag | "replied" sequence |
1430+-------------------+--------------------------+
1431
1432When an :class:`MMDFMessage` instance is created based upon a
1433:class:`BabylMessage` instance, the following conversions take place:
1434
1435+-------------------+-----------------------------+
1436| Resulting state | :class:`BabylMessage` state |
1437+===================+=============================+
1438| R flag and O flag | no "unseen" label |
1439+-------------------+-----------------------------+
1440| O flag | "unseen" label |
1441+-------------------+-----------------------------+
1442| D flag | "deleted" label |
1443+-------------------+-----------------------------+
1444| A flag | "answered" label |
1445+-------------------+-----------------------------+
1446
1447When an :class:`MMDFMessage` instance is created based upon an
1448:class:`mboxMessage` instance, the "From " line is copied and all flags directly
1449correspond:
1450
1451+-----------------+----------------------------+
1452| Resulting state | :class:`mboxMessage` state |
1453+=================+============================+
1454| R flag | R flag |
1455+-----------------+----------------------------+
1456| O flag | O flag |
1457+-----------------+----------------------------+
1458| D flag | D flag |
1459+-----------------+----------------------------+
1460| F flag | F flag |
1461+-----------------+----------------------------+
1462| A flag | A flag |
1463+-----------------+----------------------------+
1464
1465
1466Exceptions
1467----------
1468
1469The following exception classes are defined in the :mod:`mailbox` module:
1470
1471
Benjamin Petersone41251e2008-04-25 01:59:09 +00001472.. exception:: Error()
Georg Brandl116aa622007-08-15 14:28:22 +00001473
1474 The based class for all other module-specific exceptions.
1475
1476
Benjamin Petersone41251e2008-04-25 01:59:09 +00001477.. exception:: NoSuchMailboxError()
Georg Brandl116aa622007-08-15 14:28:22 +00001478
1479 Raised when a mailbox is expected but is not found, such as when instantiating a
1480 :class:`Mailbox` subclass with a path that does not exist (and with the *create*
1481 parameter set to ``False``), or when opening a folder that does not exist.
1482
1483
Benjamin Petersone41251e2008-04-25 01:59:09 +00001484.. exception:: NotEmptyErrorError()
Georg Brandl116aa622007-08-15 14:28:22 +00001485
1486 Raised when a mailbox is not empty but is expected to be, such as when deleting
1487 a folder that contains messages.
1488
1489
Benjamin Petersone41251e2008-04-25 01:59:09 +00001490.. exception:: ExternalClashError()
Georg Brandl116aa622007-08-15 14:28:22 +00001491
1492 Raised when some mailbox-related condition beyond the control of the program
1493 causes it to be unable to proceed, such as when failing to acquire a lock that
1494 another program already holds a lock, or when a uniquely-generated file name
1495 already exists.
1496
1497
Benjamin Petersone41251e2008-04-25 01:59:09 +00001498.. exception:: FormatError()
Georg Brandl116aa622007-08-15 14:28:22 +00001499
1500 Raised when the data in a file cannot be parsed, such as when an :class:`MH`
1501 instance attempts to read a corrupted :file:`.mh_sequences` file.
1502
1503
1504.. _mailbox-deprecated:
1505
1506Deprecated classes and methods
1507------------------------------
1508
1509Older versions of the :mod:`mailbox` module do not support modification of
1510mailboxes, such as adding or removing message, and do not provide classes to
1511represent format-specific message properties. For backward compatibility, the
1512older mailbox classes are still available, but the newer classes should be used
1513in preference to them.
1514
1515Older mailbox objects support only iteration and provide a single public method:
1516
1517
1518.. method:: oldmailbox.next()
1519
1520 Return the next message in the mailbox, created with the optional *factory*
1521 argument passed into the mailbox object's constructor. By default this is an
1522 :class:`rfc822.Message` object (see the :mod:`rfc822` module). Depending on the
1523 mailbox implementation the *fp* attribute of this object may be a true file
1524 object or a class instance simulating a file object, taking care of things like
1525 message boundaries if multiple mail messages are contained in a single file,
1526 etc. If no more messages are available, this method returns ``None``.
1527
1528Most of the older mailbox classes have names that differ from the current
1529mailbox class names, except for :class:`Maildir`. For this reason, the new
1530:class:`Maildir` class defines a :meth:`next` method and its constructor differs
1531slightly from those of the other new mailbox classes.
1532
1533The older mailbox classes whose names are not the same as their newer
1534counterparts are as follows:
1535
1536
1537.. class:: UnixMailbox(fp[, factory])
1538
1539 Access to a classic Unix-style mailbox, where all messages are contained in a
1540 single file and separated by ``From`` (a.k.a. ``From_``) lines. The file object
1541 *fp* points to the mailbox file. The optional *factory* parameter is a callable
1542 that should create new message objects. *factory* is called with one argument,
1543 *fp* by the :meth:`next` method of the mailbox object. The default is the
1544 :class:`rfc822.Message` class (see the :mod:`rfc822` module -- and the note
1545 below).
1546
1547 .. note::
1548
1549 For reasons of this module's internal implementation, you will probably want to
1550 open the *fp* object in binary mode. This is especially important on Windows.
1551
1552 For maximum portability, messages in a Unix-style mailbox are separated by any
1553 line that begins exactly with the string ``'From '`` (note the trailing space)
1554 if preceded by exactly two newlines. Because of the wide-range of variations in
1555 practice, nothing else on the ``From_`` line should be considered. However, the
1556 current implementation doesn't check for the leading two newlines. This is
1557 usually fine for most applications.
1558
1559 The :class:`UnixMailbox` class implements a more strict version of ``From_``
1560 line checking, using a regular expression that usually correctly matched
1561 ``From_`` delimiters. It considers delimiter line to be separated by ``From
1562 name time`` lines. For maximum portability, use the
1563 :class:`PortableUnixMailbox` class instead. This class is identical to
1564 :class:`UnixMailbox` except that individual messages are separated by only
1565 ``From`` lines.
1566
Georg Brandl116aa622007-08-15 14:28:22 +00001567
1568.. class:: PortableUnixMailbox(fp[, factory])
1569
1570 A less-strict version of :class:`UnixMailbox`, which considers only the ``From``
1571 at the beginning of the line separating messages. The "*name* *time*" portion
1572 of the From line is ignored, to protect against some variations that are
1573 observed in practice. This works since lines in the message which begin with
1574 ``'From '`` are quoted by mail handling software at delivery-time.
1575
1576
1577.. class:: MmdfMailbox(fp[, factory])
1578
1579 Access an MMDF-style mailbox, where all messages are contained in a single file
1580 and separated by lines consisting of 4 control-A characters. The file object
1581 *fp* points to the mailbox file. Optional *factory* is as with the
1582 :class:`UnixMailbox` class.
1583
1584
1585.. class:: MHMailbox(dirname[, factory])
1586
1587 Access an MH mailbox, a directory with each message in a separate file with a
1588 numeric name. The name of the mailbox directory is passed in *dirname*.
1589 *factory* is as with the :class:`UnixMailbox` class.
1590
1591
1592.. class:: BabylMailbox(fp[, factory])
1593
1594 Access a Babyl mailbox, which is similar to an MMDF mailbox. In Babyl format,
1595 each message has two sets of headers, the *original* headers and the *visible*
1596 headers. The original headers appear before a line containing only ``'*** EOOH
1597 ***'`` (End-Of-Original-Headers) and the visible headers appear after the
1598 ``EOOH`` line. Babyl-compliant mail readers will show you only the visible
1599 headers, and :class:`BabylMailbox` objects will return messages containing only
1600 the visible headers. You'll have to do your own parsing of the mailbox file to
1601 get at the original headers. Mail messages start with the EOOH line and end
1602 with a line containing only ``'\037\014'``. *factory* is as with the
1603 :class:`UnixMailbox` class.
1604
1605If you wish to use the older mailbox classes with the :mod:`email` module rather
1606than the deprecated :mod:`rfc822` module, you can do so as follows::
1607
1608 import email
1609 import email.Errors
1610 import mailbox
1611
1612 def msgfactory(fp):
1613 try:
1614 return email.message_from_file(fp)
1615 except email.Errors.MessageParseError:
1616 # Don't return None since that will
1617 # stop the mailbox iterator
1618 return ''
1619
1620 mbox = mailbox.UnixMailbox(fp, msgfactory)
1621
1622Alternatively, if you know your mailbox contains only well-formed MIME messages,
1623you can simplify this to::
1624
1625 import email
1626 import mailbox
1627
1628 mbox = mailbox.UnixMailbox(fp, email.message_from_file)
1629
1630
1631.. _mailbox-examples:
1632
1633Examples
1634--------
1635
1636A simple example of printing the subjects of all messages in a mailbox that seem
1637interesting::
1638
1639 import mailbox
1640 for message in mailbox.mbox('~/mbox'):
1641 subject = message['subject'] # Could possibly be None.
1642 if subject and 'python' in subject.lower():
Georg Brandl6911e3c2007-09-04 07:15:32 +00001643 print(subject)
Georg Brandl116aa622007-08-15 14:28:22 +00001644
1645To copy all mail from a Babyl mailbox to an MH mailbox, converting all of the
1646format-specific information that can be converted::
1647
1648 import mailbox
1649 destination = mailbox.MH('~/Mail')
1650 destination.lock()
1651 for message in mailbox.Babyl('~/RMAIL'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001652 destination.add(mailbox.MHMessage(message))
Georg Brandl116aa622007-08-15 14:28:22 +00001653 destination.flush()
1654 destination.unlock()
1655
1656This example sorts mail from several mailing lists into different mailboxes,
1657being careful to avoid mail corruption due to concurrent modification by other
1658programs, mail loss due to interruption of the program, or premature termination
1659due to malformed messages in the mailbox::
1660
1661 import mailbox
1662 import email.Errors
1663
1664 list_names = ('python-list', 'python-dev', 'python-bugs')
1665
Georg Brandlf6945182008-02-01 11:56:49 +00001666 boxes = {name: mailbox.mbox('~/email/%s' % name) for name in list_names}
Georg Brandl116aa622007-08-15 14:28:22 +00001667 inbox = mailbox.Maildir('~/Maildir', factory=None)
1668
1669 for key in inbox.iterkeys():
1670 try:
1671 message = inbox[key]
1672 except email.Errors.MessageParseError:
1673 continue # The message is malformed. Just leave it.
1674
1675 for name in list_names:
1676 list_id = message['list-id']
1677 if list_id and name in list_id:
1678 # Get mailbox to use
1679 box = boxes[name]
1680
1681 # Write copy to disk before removing original.
1682 # If there's a crash, you might duplicate a message, but
1683 # that's better than losing a message completely.
1684 box.lock()
1685 box.add(message)
1686 box.flush()
1687 box.unlock()
1688
1689 # Remove original message
1690 inbox.lock()
1691 inbox.discard(key)
1692 inbox.flush()
1693 inbox.unlock()
1694 break # Found destination, so stop looking.
1695
1696 for box in boxes.itervalues():
1697 box.close()
1698