blob: da01791f70e130c3cee14260adbce9e37a3547d5 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`mailbox` --- Manipulate mailboxes in various formats
2==========================================================
3
4.. module:: mailbox
5 :synopsis: Manipulate mailboxes in various formats
6.. moduleauthor:: Gregory K. Johnson <gkj@gregorykjohnson.com>
7.. sectionauthor:: Gregory K. Johnson <gkj@gregorykjohnson.com>
8
9
10This module defines two classes, :class:`Mailbox` and :class:`Message`, for
11accessing and manipulating on-disk mailboxes and the messages they contain.
12:class:`Mailbox` offers a dictionary-like mapping from keys to messages.
13:class:`Message` extends the :mod:`email.Message` module's :class:`Message`
14class with format-specific state and behavior. Supported mailbox formats are
15Maildir, mbox, MH, Babyl, and MMDF.
16
17
18.. seealso::
19
20 Module :mod:`email`
21 Represent and manipulate messages.
22
23
24.. _mailbox-objects:
25
26:class:`Mailbox` objects
27------------------------
28
Georg Brandl116aa622007-08-15 14:28:22 +000029.. class:: Mailbox
30
31 A mailbox, which may be inspected and modified.
32
Benjamin Petersone41251e2008-04-25 01:59:09 +000033 The :class:`Mailbox` class defines an interface and is not intended to be
34 instantiated. Instead, format-specific subclasses should inherit from
35 :class:`Mailbox` and your code should instantiate a particular subclass.
Georg Brandl116aa622007-08-15 14:28:22 +000036
Benjamin Petersone41251e2008-04-25 01:59:09 +000037 The :class:`Mailbox` interface is dictionary-like, with small keys
38 corresponding to messages. Keys are issued by the :class:`Mailbox` instance
39 with which they will be used and are only meaningful to that :class:`Mailbox`
40 instance. A key continues to identify a message even if the corresponding
41 message is modified, such as by replacing it with another message.
Georg Brandl116aa622007-08-15 14:28:22 +000042
Benjamin Petersone41251e2008-04-25 01:59:09 +000043 Messages may be added to a :class:`Mailbox` instance using the set-like
44 method :meth:`add` and removed using a ``del`` statement or the set-like
45 methods :meth:`remove` and :meth:`discard`.
Georg Brandl116aa622007-08-15 14:28:22 +000046
Benjamin Petersone41251e2008-04-25 01:59:09 +000047 :class:`Mailbox` interface semantics differ from dictionary semantics in some
48 noteworthy ways. Each time a message is requested, a new representation
49 (typically a :class:`Message` instance) is generated based upon the current
50 state of the mailbox. Similarly, when a message is added to a
51 :class:`Mailbox` instance, the provided message representation's contents are
52 copied. In neither case is a reference to the message representation kept by
53 the :class:`Mailbox` instance.
Georg Brandl116aa622007-08-15 14:28:22 +000054
Benjamin Petersone41251e2008-04-25 01:59:09 +000055 The default :class:`Mailbox` iterator iterates over message representations,
56 not keys as the default dictionary iterator does. Moreover, modification of a
57 mailbox during iteration is safe and well-defined. Messages added to the
58 mailbox after an iterator is created will not be seen by the
59 iterator. Messages removed from the mailbox before the iterator yields them
60 will be silently skipped, though using a key from an iterator may result in a
61 :exc:`KeyError` exception if the corresponding message is subsequently
62 removed.
Georg Brandl116aa622007-08-15 14:28:22 +000063
Benjamin Petersone41251e2008-04-25 01:59:09 +000064 .. warning::
Georg Brandl116aa622007-08-15 14:28:22 +000065
Benjamin Petersone41251e2008-04-25 01:59:09 +000066 Be very cautious when modifying mailboxes that might be simultaneously
67 changed by some other process. The safest mailbox format to use for such
68 tasks is Maildir; try to avoid using single-file formats such as mbox for
69 concurrent writing. If you're modifying a mailbox, you *must* lock it by
70 calling the :meth:`lock` and :meth:`unlock` methods *before* reading any
71 messages in the file or making any changes by adding or deleting a
72 message. Failing to lock the mailbox runs the risk of losing messages or
73 corrupting the entire mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +000074
Benjamin Petersone41251e2008-04-25 01:59:09 +000075 :class:`Mailbox` instances have the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +000076
77
Benjamin Petersone41251e2008-04-25 01:59:09 +000078 .. method:: add(message)
Georg Brandl116aa622007-08-15 14:28:22 +000079
Benjamin Petersone41251e2008-04-25 01:59:09 +000080 Add *message* to the mailbox and return the key that has been assigned to
81 it.
Georg Brandl116aa622007-08-15 14:28:22 +000082
Benjamin Petersone41251e2008-04-25 01:59:09 +000083 Parameter *message* may be a :class:`Message` instance, an
84 :class:`email.Message.Message` instance, a string, or a file-like object
85 (which should be open in text mode). If *message* is an instance of the
86 appropriate format-specific :class:`Message` subclass (e.g., if it's an
87 :class:`mboxMessage` instance and this is an :class:`mbox` instance), its
88 format-specific information is used. Otherwise, reasonable defaults for
89 format-specific information are used.
Georg Brandl116aa622007-08-15 14:28:22 +000090
91
Benjamin Petersone41251e2008-04-25 01:59:09 +000092 .. method:: remove(key)
93 __delitem__(key)
94 discard(key)
Georg Brandl116aa622007-08-15 14:28:22 +000095
Benjamin Petersone41251e2008-04-25 01:59:09 +000096 Delete the message corresponding to *key* from the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +000097
Benjamin Petersone41251e2008-04-25 01:59:09 +000098 If no such message exists, a :exc:`KeyError` exception is raised if the
99 method was called as :meth:`remove` or :meth:`__delitem__` but no
100 exception is raised if the method was called as :meth:`discard`. The
101 behavior of :meth:`discard` may be preferred if the underlying mailbox
102 format supports concurrent modification by other processes.
Georg Brandl116aa622007-08-15 14:28:22 +0000103
104
Benjamin Petersone41251e2008-04-25 01:59:09 +0000105 .. method:: __setitem__(key, message)
Georg Brandl116aa622007-08-15 14:28:22 +0000106
Benjamin Petersone41251e2008-04-25 01:59:09 +0000107 Replace the message corresponding to *key* with *message*. Raise a
108 :exc:`KeyError` exception if no message already corresponds to *key*.
Georg Brandl116aa622007-08-15 14:28:22 +0000109
Benjamin Petersone41251e2008-04-25 01:59:09 +0000110 As with :meth:`add`, parameter *message* may be a :class:`Message`
111 instance, an :class:`email.Message.Message` instance, a string, or a
112 file-like object (which should be open in text mode). If *message* is an
113 instance of the appropriate format-specific :class:`Message` subclass
114 (e.g., if it's an :class:`mboxMessage` instance and this is an
115 :class:`mbox` instance), its format-specific information is
116 used. Otherwise, the format-specific information of the message that
117 currently corresponds to *key* is left unchanged.
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119
Benjamin Petersone41251e2008-04-25 01:59:09 +0000120 .. method:: iterkeys()
121 keys()
Georg Brandl116aa622007-08-15 14:28:22 +0000122
Benjamin Petersone41251e2008-04-25 01:59:09 +0000123 Return an iterator over all keys if called as :meth:`iterkeys` or return a
124 list of keys if called as :meth:`keys`.
Georg Brandl116aa622007-08-15 14:28:22 +0000125
126
Benjamin Petersone41251e2008-04-25 01:59:09 +0000127 .. method:: itervalues()
128 __iter__()
129 values()
Georg Brandl116aa622007-08-15 14:28:22 +0000130
Benjamin Petersone41251e2008-04-25 01:59:09 +0000131 Return an iterator over representations of all messages if called as
132 :meth:`itervalues` or :meth:`__iter__` or return a list of such
133 representations if called as :meth:`values`. The messages are represented
134 as instances of the appropriate format-specific :class:`Message` subclass
135 unless a custom message factory was specified when the :class:`Mailbox`
136 instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000137
Benjamin Petersone41251e2008-04-25 01:59:09 +0000138 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000139
Benjamin Petersone41251e2008-04-25 01:59:09 +0000140 The behavior of :meth:`__iter__` is unlike that of dictionaries, which
141 iterate over keys.
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143
Benjamin Petersone41251e2008-04-25 01:59:09 +0000144 .. method:: iteritems()
145 items()
Georg Brandl116aa622007-08-15 14:28:22 +0000146
Benjamin Petersone41251e2008-04-25 01:59:09 +0000147 Return an iterator over (*key*, *message*) pairs, where *key* is a key and
148 *message* is a message representation, if called as :meth:`iteritems` or
149 return a list of such pairs if called as :meth:`items`. The messages are
150 represented as instances of the appropriate format-specific
151 :class:`Message` subclass unless a custom message factory was specified
152 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000155 .. method:: get(key, default=None)
Benjamin Petersone41251e2008-04-25 01:59:09 +0000156 __getitem__(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000157
Benjamin Petersone41251e2008-04-25 01:59:09 +0000158 Return a representation of the message corresponding to *key*. If no such
159 message exists, *default* is returned if the method was called as
160 :meth:`get` and a :exc:`KeyError` exception is raised if the method was
161 called as :meth:`__getitem__`. The message is represented as an instance
162 of the appropriate format-specific :class:`Message` subclass unless a
163 custom message factory was specified when the :class:`Mailbox` instance
164 was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000165
166
Benjamin Petersone41251e2008-04-25 01:59:09 +0000167 .. method:: get_message(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000168
Benjamin Petersone41251e2008-04-25 01:59:09 +0000169 Return a representation of the message corresponding to *key* as an
170 instance of the appropriate format-specific :class:`Message` subclass, or
171 raise a :exc:`KeyError` exception if no such message exists.
Georg Brandl116aa622007-08-15 14:28:22 +0000172
173
Benjamin Petersone41251e2008-04-25 01:59:09 +0000174 .. method:: get_string(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000175
Benjamin Petersone41251e2008-04-25 01:59:09 +0000176 Return a string representation of the message corresponding to *key*, or
177 raise a :exc:`KeyError` exception if no such message exists.
Georg Brandl116aa622007-08-15 14:28:22 +0000178
179
Benjamin Petersone41251e2008-04-25 01:59:09 +0000180 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000181
Benjamin Petersone41251e2008-04-25 01:59:09 +0000182 Return a file-like representation of the message corresponding to *key*,
Georg Brandl6ce29fa2010-10-30 14:33:28 +0000183 or raise a :exc:`KeyError` exception if no such message exists. The
184 file-like object behaves as if open in binary mode. This file should be
Benjamin Petersone41251e2008-04-25 01:59:09 +0000185 closed once it is no longer needed.
Georg Brandl116aa622007-08-15 14:28:22 +0000186
Georg Brandl6ce29fa2010-10-30 14:33:28 +0000187 .. versionadded:: 3.2
188 The file-like object supports the context manager protocol, so that
189 you can use a :keyword:`with` statement to automatically close it.
190
Benjamin Petersone41251e2008-04-25 01:59:09 +0000191 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000192
Benjamin Petersone41251e2008-04-25 01:59:09 +0000193 Unlike other representations of messages, file-like representations are
194 not necessarily independent of the :class:`Mailbox` instance that
Georg Brandl6ce29fa2010-10-30 14:33:28 +0000195 created them or of the underlying mailbox. More specific documentation
Benjamin Petersone41251e2008-04-25 01:59:09 +0000196 is provided by each subclass.
Georg Brandl116aa622007-08-15 14:28:22 +0000197
198
Benjamin Petersone41251e2008-04-25 01:59:09 +0000199 .. method:: __contains__(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000200
Benjamin Petersone41251e2008-04-25 01:59:09 +0000201 Return ``True`` if *key* corresponds to a message, ``False`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000202
203
Benjamin Petersone41251e2008-04-25 01:59:09 +0000204 .. method:: __len__()
Georg Brandl116aa622007-08-15 14:28:22 +0000205
Benjamin Petersone41251e2008-04-25 01:59:09 +0000206 Return a count of messages in the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000207
208
Benjamin Petersone41251e2008-04-25 01:59:09 +0000209 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +0000210
Benjamin Petersone41251e2008-04-25 01:59:09 +0000211 Delete all messages from the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000212
213
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000214 .. method:: pop(key, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000215
Benjamin Petersone41251e2008-04-25 01:59:09 +0000216 Return a representation of the message corresponding to *key* and delete
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000217 the message. If no such message exists, return *default*. The message is
Benjamin Petersone41251e2008-04-25 01:59:09 +0000218 represented as an instance of the appropriate format-specific
219 :class:`Message` subclass unless a custom message factory was specified
220 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000221
222
Benjamin Petersone41251e2008-04-25 01:59:09 +0000223 .. method:: popitem()
Georg Brandl116aa622007-08-15 14:28:22 +0000224
Benjamin Petersone41251e2008-04-25 01:59:09 +0000225 Return an arbitrary (*key*, *message*) pair, where *key* is a key and
226 *message* is a message representation, and delete the corresponding
227 message. If the mailbox is empty, raise a :exc:`KeyError` exception. The
228 message is represented as an instance of the appropriate format-specific
229 :class:`Message` subclass unless a custom message factory was specified
230 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000231
232
Benjamin Petersone41251e2008-04-25 01:59:09 +0000233 .. method:: update(arg)
Georg Brandl116aa622007-08-15 14:28:22 +0000234
Benjamin Petersone41251e2008-04-25 01:59:09 +0000235 Parameter *arg* should be a *key*-to-*message* mapping or an iterable of
236 (*key*, *message*) pairs. Updates the mailbox so that, for each given
237 *key* and *message*, the message corresponding to *key* is set to
238 *message* as if by using :meth:`__setitem__`. As with :meth:`__setitem__`,
239 each *key* must already correspond to a message in the mailbox or else a
240 :exc:`KeyError` exception will be raised, so in general it is incorrect
241 for *arg* to be a :class:`Mailbox` instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000242
Benjamin Petersone41251e2008-04-25 01:59:09 +0000243 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000244
Benjamin Petersone41251e2008-04-25 01:59:09 +0000245 Unlike with dictionaries, keyword arguments are not supported.
Georg Brandl116aa622007-08-15 14:28:22 +0000246
247
Benjamin Petersone41251e2008-04-25 01:59:09 +0000248 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000249
Benjamin Petersone41251e2008-04-25 01:59:09 +0000250 Write any pending changes to the filesystem. For some :class:`Mailbox`
251 subclasses, changes are always written immediately and :meth:`flush` does
252 nothing, but you should still make a habit of calling this method.
Georg Brandl116aa622007-08-15 14:28:22 +0000253
254
Benjamin Petersone41251e2008-04-25 01:59:09 +0000255 .. method:: lock()
Georg Brandl116aa622007-08-15 14:28:22 +0000256
Benjamin Petersone41251e2008-04-25 01:59:09 +0000257 Acquire an exclusive advisory lock on the mailbox so that other processes
258 know not to modify it. An :exc:`ExternalClashError` is raised if the lock
259 is not available. The particular locking mechanisms used depend upon the
260 mailbox format. You should *always* lock the mailbox before making any
261 modifications to its contents.
Georg Brandl116aa622007-08-15 14:28:22 +0000262
263
Benjamin Petersone41251e2008-04-25 01:59:09 +0000264 .. method:: unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000265
Benjamin Petersone41251e2008-04-25 01:59:09 +0000266 Release the lock on the mailbox, if any.
Georg Brandl116aa622007-08-15 14:28:22 +0000267
268
Benjamin Petersone41251e2008-04-25 01:59:09 +0000269 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000270
Benjamin Petersone41251e2008-04-25 01:59:09 +0000271 Flush the mailbox, unlock it if necessary, and close any open files. For
272 some :class:`Mailbox` subclasses, this method does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000273
274
275.. _mailbox-maildir:
276
277:class:`Maildir`
278^^^^^^^^^^^^^^^^
279
280
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000281.. class:: Maildir(dirname, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000282
283 A subclass of :class:`Mailbox` for mailboxes in Maildir format. Parameter
284 *factory* is a callable object that accepts a file-like message representation
285 (which behaves as if opened in binary mode) and returns a custom representation.
286 If *factory* is ``None``, :class:`MaildirMessage` is used as the default message
287 representation. If *create* is ``True``, the mailbox is created if it does not
288 exist.
289
Georg Brandlaa5b4112008-05-11 20:51:18 +0000290 It is for historical reasons that *dirname* is named as such rather than *path*.
Georg Brandl116aa622007-08-15 14:28:22 +0000291
Benjamin Petersone41251e2008-04-25 01:59:09 +0000292 Maildir is a directory-based mailbox format invented for the qmail mail
293 transfer agent and now widely supported by other programs. Messages in a
294 Maildir mailbox are stored in separate files within a common directory
295 structure. This design allows Maildir mailboxes to be accessed and modified
296 by multiple unrelated programs without data corruption, so file locking is
297 unnecessary.
Georg Brandl116aa622007-08-15 14:28:22 +0000298
Benjamin Petersone41251e2008-04-25 01:59:09 +0000299 Maildir mailboxes contain three subdirectories, namely: :file:`tmp`,
300 :file:`new`, and :file:`cur`. Messages are created momentarily in the
301 :file:`tmp` subdirectory and then moved to the :file:`new` subdirectory to
302 finalize delivery. A mail user agent may subsequently move the message to the
303 :file:`cur` subdirectory and store information about the state of the message
304 in a special "info" section appended to its file name.
Georg Brandl116aa622007-08-15 14:28:22 +0000305
Benjamin Petersone41251e2008-04-25 01:59:09 +0000306 Folders of the style introduced by the Courier mail transfer agent are also
307 supported. Any subdirectory of the main mailbox is considered a folder if
308 ``'.'`` is the first character in its name. Folder names are represented by
309 :class:`Maildir` without the leading ``'.'``. Each folder is itself a Maildir
310 mailbox but should not contain other folders. Instead, a logical nesting is
311 indicated using ``'.'`` to delimit levels, e.g., "Archived.2005.07".
Georg Brandl116aa622007-08-15 14:28:22 +0000312
Benjamin Petersone41251e2008-04-25 01:59:09 +0000313 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000314
Benjamin Petersone41251e2008-04-25 01:59:09 +0000315 The Maildir specification requires the use of a colon (``':'``) in certain
316 message file names. However, some operating systems do not permit this
317 character in file names, If you wish to use a Maildir-like format on such
318 an operating system, you should specify another character to use
319 instead. The exclamation point (``'!'``) is a popular choice. For
320 example::
Georg Brandl116aa622007-08-15 14:28:22 +0000321
Benjamin Petersone41251e2008-04-25 01:59:09 +0000322 import mailbox
323 mailbox.Maildir.colon = '!'
Georg Brandl116aa622007-08-15 14:28:22 +0000324
Benjamin Petersone41251e2008-04-25 01:59:09 +0000325 The :attr:`colon` attribute may also be set on a per-instance basis.
Georg Brandl116aa622007-08-15 14:28:22 +0000326
Benjamin Petersone41251e2008-04-25 01:59:09 +0000327 :class:`Maildir` instances have all of the methods of :class:`Mailbox` in
328 addition to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000329
330
Benjamin Petersone41251e2008-04-25 01:59:09 +0000331 .. method:: list_folders()
Georg Brandl116aa622007-08-15 14:28:22 +0000332
Benjamin Petersone41251e2008-04-25 01:59:09 +0000333 Return a list of the names of all folders.
Georg Brandl116aa622007-08-15 14:28:22 +0000334
335
Ezio Melotti2d352d02009-09-15 18:51:37 +0000336 .. method:: get_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000337
Benjamin Petersone41251e2008-04-25 01:59:09 +0000338 Return a :class:`Maildir` instance representing the folder whose name is
339 *folder*. A :exc:`NoSuchMailboxError` exception is raised if the folder
340 does not exist.
Georg Brandl116aa622007-08-15 14:28:22 +0000341
342
Benjamin Petersone41251e2008-04-25 01:59:09 +0000343 .. method:: add_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000344
Benjamin Petersone41251e2008-04-25 01:59:09 +0000345 Create a folder whose name is *folder* and return a :class:`Maildir`
346 instance representing it.
Georg Brandl116aa622007-08-15 14:28:22 +0000347
348
Benjamin Petersone41251e2008-04-25 01:59:09 +0000349 .. method:: remove_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000350
Benjamin Petersone41251e2008-04-25 01:59:09 +0000351 Delete the folder whose name is *folder*. If the folder contains any
352 messages, a :exc:`NotEmptyError` exception will be raised and the folder
353 will not be deleted.
Georg Brandl116aa622007-08-15 14:28:22 +0000354
355
Benjamin Petersone41251e2008-04-25 01:59:09 +0000356 .. method:: clean()
Georg Brandl116aa622007-08-15 14:28:22 +0000357
Benjamin Petersone41251e2008-04-25 01:59:09 +0000358 Delete temporary files from the mailbox that have not been accessed in the
359 last 36 hours. The Maildir specification says that mail-reading programs
360 should do this occasionally.
Georg Brandl116aa622007-08-15 14:28:22 +0000361
Benjamin Petersone41251e2008-04-25 01:59:09 +0000362 Some :class:`Mailbox` methods implemented by :class:`Maildir` deserve special
363 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000364
365
Benjamin Petersone41251e2008-04-25 01:59:09 +0000366 .. method:: add(message)
367 __setitem__(key, message)
368 update(arg)
Georg Brandl116aa622007-08-15 14:28:22 +0000369
Benjamin Petersone41251e2008-04-25 01:59:09 +0000370 .. warning::
Georg Brandl116aa622007-08-15 14:28:22 +0000371
Benjamin Petersone41251e2008-04-25 01:59:09 +0000372 These methods generate unique file names based upon the current process
373 ID. When using multiple threads, undetected name clashes may occur and
374 cause corruption of the mailbox unless threads are coordinated to avoid
375 using these methods to manipulate the same mailbox simultaneously.
Georg Brandl116aa622007-08-15 14:28:22 +0000376
377
Benjamin Petersone41251e2008-04-25 01:59:09 +0000378 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000379
Benjamin Petersone41251e2008-04-25 01:59:09 +0000380 All changes to Maildir mailboxes are immediately applied, so this method
381 does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000382
383
Benjamin Petersone41251e2008-04-25 01:59:09 +0000384 .. method:: lock()
385 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000386
Benjamin Petersone41251e2008-04-25 01:59:09 +0000387 Maildir mailboxes do not support (or require) locking, so these methods do
388 nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000389
390
Benjamin Petersone41251e2008-04-25 01:59:09 +0000391 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000392
Benjamin Petersone41251e2008-04-25 01:59:09 +0000393 :class:`Maildir` instances do not keep any open files and the underlying
394 mailboxes do not support locking, so this method does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000395
396
Benjamin Petersone41251e2008-04-25 01:59:09 +0000397 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000398
Benjamin Petersone41251e2008-04-25 01:59:09 +0000399 Depending upon the host platform, it may not be possible to modify or
400 remove the underlying message while the returned file remains open.
Georg Brandl116aa622007-08-15 14:28:22 +0000401
402
403.. seealso::
404
405 `maildir man page from qmail <http://www.qmail.org/man/man5/maildir.html>`_
406 The original specification of the format.
407
408 `Using maildir format <http://cr.yp.to/proto/maildir.html>`_
409 Notes on Maildir by its inventor. Includes an updated name-creation scheme and
410 details on "info" semantics.
411
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000412 `maildir man page from Courier <http://www.courier-mta.org/maildir.html>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000413 Another specification of the format. Describes a common extension for supporting
414 folders.
415
416
417.. _mailbox-mbox:
418
419:class:`mbox`
420^^^^^^^^^^^^^
421
422
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000423.. class:: mbox(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000424
425 A subclass of :class:`Mailbox` for mailboxes in mbox format. Parameter *factory*
426 is a callable object that accepts a file-like message representation (which
427 behaves as if opened in binary mode) and returns a custom representation. If
428 *factory* is ``None``, :class:`mboxMessage` is used as the default message
429 representation. If *create* is ``True``, the mailbox is created if it does not
430 exist.
431
Benjamin Petersone41251e2008-04-25 01:59:09 +0000432 The mbox format is the classic format for storing mail on Unix systems. All
433 messages in an mbox mailbox are stored in a single file with the beginning of
434 each message indicated by a line whose first five characters are "From ".
Georg Brandl116aa622007-08-15 14:28:22 +0000435
Benjamin Petersone41251e2008-04-25 01:59:09 +0000436 Several variations of the mbox format exist to address perceived shortcomings in
437 the original. In the interest of compatibility, :class:`mbox` implements the
438 original format, which is sometimes referred to as :dfn:`mboxo`. This means that
439 the :mailheader:`Content-Length` header, if present, is ignored and that any
440 occurrences of "From " at the beginning of a line in a message body are
441 transformed to ">From " when storing the message, although occurrences of ">From
442 " are not transformed to "From " when reading the message.
Georg Brandl116aa622007-08-15 14:28:22 +0000443
Benjamin Petersone41251e2008-04-25 01:59:09 +0000444 Some :class:`Mailbox` methods implemented by :class:`mbox` deserve special
445 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000446
447
Benjamin Petersone41251e2008-04-25 01:59:09 +0000448 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000449
Benjamin Petersone41251e2008-04-25 01:59:09 +0000450 Using the file after calling :meth:`flush` or :meth:`close` on the
451 :class:`mbox` instance may yield unpredictable results or raise an
452 exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000453
454
Benjamin Petersone41251e2008-04-25 01:59:09 +0000455 .. method:: lock()
456 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000457
Benjamin Petersone41251e2008-04-25 01:59:09 +0000458 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000459 :c:func:`flock` and :c:func:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000460
461
462.. seealso::
463
464 `mbox man page from qmail <http://www.qmail.org/man/man5/mbox.html>`_
465 A specification of the format and its variations.
466
467 `mbox man page from tin <http://www.tin.org/bin/man.cgi?section=5&topic=mbox>`_
468 Another specification of the format, with details on locking.
469
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000470 `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 +0000471 An argument for using the original mbox format rather than a variation.
472
473 `"mbox" is a family of several mutually incompatible mailbox formats <http://homepages.tesco.net./~J.deBoynePollard/FGA/mail-mbox-formats.html>`_
474 A history of mbox variations.
475
476
477.. _mailbox-mh:
478
479:class:`MH`
480^^^^^^^^^^^
481
482
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000483.. class:: MH(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000484
485 A subclass of :class:`Mailbox` for mailboxes in MH format. Parameter *factory*
486 is a callable object that accepts a file-like message representation (which
487 behaves as if opened in binary mode) and returns a custom representation. If
488 *factory* is ``None``, :class:`MHMessage` is used as the default message
489 representation. If *create* is ``True``, the mailbox is created if it does not
490 exist.
491
Benjamin Petersone41251e2008-04-25 01:59:09 +0000492 MH is a directory-based mailbox format invented for the MH Message Handling
493 System, a mail user agent. Each message in an MH mailbox resides in its own
494 file. An MH mailbox may contain other MH mailboxes (called :dfn:`folders`) in
495 addition to messages. Folders may be nested indefinitely. MH mailboxes also
496 support :dfn:`sequences`, which are named lists used to logically group
497 messages without moving them to sub-folders. Sequences are defined in a file
498 called :file:`.mh_sequences` in each folder.
Georg Brandl116aa622007-08-15 14:28:22 +0000499
Benjamin Petersone41251e2008-04-25 01:59:09 +0000500 The :class:`MH` class manipulates MH mailboxes, but it does not attempt to
501 emulate all of :program:`mh`'s behaviors. In particular, it does not modify
502 and is not affected by the :file:`context` or :file:`.mh_profile` files that
503 are used by :program:`mh` to store its state and configuration.
Georg Brandl116aa622007-08-15 14:28:22 +0000504
Benjamin Petersone41251e2008-04-25 01:59:09 +0000505 :class:`MH` instances have all of the methods of :class:`Mailbox` in addition
506 to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000507
508
Benjamin Petersone41251e2008-04-25 01:59:09 +0000509 .. method:: list_folders()
Georg Brandl116aa622007-08-15 14:28:22 +0000510
Benjamin Petersone41251e2008-04-25 01:59:09 +0000511 Return a list of the names of all folders.
Georg Brandl116aa622007-08-15 14:28:22 +0000512
513
Benjamin Petersone41251e2008-04-25 01:59:09 +0000514 .. method:: get_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000515
Benjamin Petersone41251e2008-04-25 01:59:09 +0000516 Return an :class:`MH` instance representing the folder whose name is
517 *folder*. A :exc:`NoSuchMailboxError` exception is raised if the folder
518 does not exist.
Georg Brandl116aa622007-08-15 14:28:22 +0000519
520
Benjamin Petersone41251e2008-04-25 01:59:09 +0000521 .. method:: add_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000522
Benjamin Petersone41251e2008-04-25 01:59:09 +0000523 Create a folder whose name is *folder* and return an :class:`MH` instance
524 representing it.
Georg Brandl116aa622007-08-15 14:28:22 +0000525
526
Benjamin Petersone41251e2008-04-25 01:59:09 +0000527 .. method:: remove_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000528
Benjamin Petersone41251e2008-04-25 01:59:09 +0000529 Delete the folder whose name is *folder*. If the folder contains any
530 messages, a :exc:`NotEmptyError` exception will be raised and the folder
531 will not be deleted.
Georg Brandl116aa622007-08-15 14:28:22 +0000532
533
Benjamin Petersone41251e2008-04-25 01:59:09 +0000534 .. method:: get_sequences()
Georg Brandl116aa622007-08-15 14:28:22 +0000535
Benjamin Petersone41251e2008-04-25 01:59:09 +0000536 Return a dictionary of sequence names mapped to key lists. If there are no
537 sequences, the empty dictionary is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000538
539
Benjamin Petersone41251e2008-04-25 01:59:09 +0000540 .. method:: set_sequences(sequences)
Georg Brandl116aa622007-08-15 14:28:22 +0000541
Benjamin Petersone41251e2008-04-25 01:59:09 +0000542 Re-define the sequences that exist in the mailbox based upon *sequences*,
543 a dictionary of names mapped to key lists, like returned by
544 :meth:`get_sequences`.
Georg Brandl116aa622007-08-15 14:28:22 +0000545
546
Benjamin Petersone41251e2008-04-25 01:59:09 +0000547 .. method:: pack()
Georg Brandl116aa622007-08-15 14:28:22 +0000548
Benjamin Petersone41251e2008-04-25 01:59:09 +0000549 Rename messages in the mailbox as necessary to eliminate gaps in
550 numbering. Entries in the sequences list are updated correspondingly.
Georg Brandl116aa622007-08-15 14:28:22 +0000551
Benjamin Petersone41251e2008-04-25 01:59:09 +0000552 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000553
Benjamin Petersone41251e2008-04-25 01:59:09 +0000554 Already-issued keys are invalidated by this operation and should not be
555 subsequently used.
Georg Brandl116aa622007-08-15 14:28:22 +0000556
Benjamin Petersone41251e2008-04-25 01:59:09 +0000557 Some :class:`Mailbox` methods implemented by :class:`MH` deserve special
558 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000559
560
Benjamin Petersone41251e2008-04-25 01:59:09 +0000561 .. method:: remove(key)
562 __delitem__(key)
563 discard(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000564
Benjamin Petersone41251e2008-04-25 01:59:09 +0000565 These methods immediately delete the message. The MH convention of marking
566 a message for deletion by prepending a comma to its name is not used.
Georg Brandl116aa622007-08-15 14:28:22 +0000567
568
Benjamin Petersone41251e2008-04-25 01:59:09 +0000569 .. method:: lock()
570 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000571
Benjamin Petersone41251e2008-04-25 01:59:09 +0000572 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000573 :c:func:`flock` and :c:func:`lockf` system calls. For MH mailboxes, locking
Benjamin Petersone41251e2008-04-25 01:59:09 +0000574 the mailbox means locking the :file:`.mh_sequences` file and, only for the
575 duration of any operations that affect them, locking individual message
576 files.
Georg Brandl116aa622007-08-15 14:28:22 +0000577
578
Benjamin Petersone41251e2008-04-25 01:59:09 +0000579 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000580
Benjamin Petersone41251e2008-04-25 01:59:09 +0000581 Depending upon the host platform, it may not be possible to remove the
582 underlying message while the returned file remains open.
Georg Brandl116aa622007-08-15 14:28:22 +0000583
584
Benjamin Petersone41251e2008-04-25 01:59:09 +0000585 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000586
Benjamin Petersone41251e2008-04-25 01:59:09 +0000587 All changes to MH mailboxes are immediately applied, so this method does
588 nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000589
590
Benjamin Petersone41251e2008-04-25 01:59:09 +0000591 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000592
Benjamin Petersone41251e2008-04-25 01:59:09 +0000593 :class:`MH` instances do not keep any open files, so this method is
594 equivalent to :meth:`unlock`.
Georg Brandl116aa622007-08-15 14:28:22 +0000595
596
597.. seealso::
598
599 `nmh - Message Handling System <http://www.nongnu.org/nmh/>`_
600 Home page of :program:`nmh`, an updated version of the original :program:`mh`.
601
Georg Brandl495f7b52009-10-27 15:28:25 +0000602 `MH & nmh: Email for Users & Programmers <http://rand-mh.sourceforge.net/book/>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000603 A GPL-licensed book on :program:`mh` and :program:`nmh`, with some information
604 on the mailbox format.
605
606
607.. _mailbox-babyl:
608
609:class:`Babyl`
610^^^^^^^^^^^^^^
611
612
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000613.. class:: Babyl(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000614
615 A subclass of :class:`Mailbox` for mailboxes in Babyl format. Parameter
616 *factory* is a callable object that accepts a file-like message representation
617 (which behaves as if opened in binary mode) and returns a custom representation.
618 If *factory* is ``None``, :class:`BabylMessage` is used as the default message
619 representation. If *create* is ``True``, the mailbox is created if it does not
620 exist.
621
Benjamin Petersone41251e2008-04-25 01:59:09 +0000622 Babyl is a single-file mailbox format used by the Rmail mail user agent
623 included with Emacs. The beginning of a message is indicated by a line
624 containing the two characters Control-Underscore (``'\037'``) and Control-L
625 (``'\014'``). The end of a message is indicated by the start of the next
626 message or, in the case of the last message, a line containing a
627 Control-Underscore (``'\037'``) character.
Georg Brandl116aa622007-08-15 14:28:22 +0000628
Benjamin Petersone41251e2008-04-25 01:59:09 +0000629 Messages in a Babyl mailbox have two sets of headers, original headers and
630 so-called visible headers. Visible headers are typically a subset of the
631 original headers that have been reformatted or abridged to be more
632 attractive. Each message in a Babyl mailbox also has an accompanying list of
633 :dfn:`labels`, or short strings that record extra information about the
634 message, and a list of all user-defined labels found in the mailbox is kept
635 in the Babyl options section.
Georg Brandl116aa622007-08-15 14:28:22 +0000636
Benjamin Petersone41251e2008-04-25 01:59:09 +0000637 :class:`Babyl` instances have all of the methods of :class:`Mailbox` in
638 addition to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000639
640
Benjamin Petersone41251e2008-04-25 01:59:09 +0000641 .. method:: get_labels()
Georg Brandl116aa622007-08-15 14:28:22 +0000642
Benjamin Petersone41251e2008-04-25 01:59:09 +0000643 Return a list of the names of all user-defined labels used in the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000644
Benjamin Petersone41251e2008-04-25 01:59:09 +0000645 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000646
Benjamin Petersone41251e2008-04-25 01:59:09 +0000647 The actual messages are inspected to determine which labels exist in
648 the mailbox rather than consulting the list of labels in the Babyl
649 options section, but the Babyl section is updated whenever the mailbox
650 is modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000651
Benjamin Petersone41251e2008-04-25 01:59:09 +0000652 Some :class:`Mailbox` methods implemented by :class:`Babyl` deserve special
653 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000654
655
Benjamin Petersone41251e2008-04-25 01:59:09 +0000656 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000657
Benjamin Petersone41251e2008-04-25 01:59:09 +0000658 In Babyl mailboxes, the headers of a message are not stored contiguously
659 with the body of the message. To generate a file-like representation, the
660 headers and body are copied together into a :class:`StringIO` instance
661 (from the :mod:`StringIO` module), which has an API identical to that of a
662 file. As a result, the file-like object is truly independent of the
663 underlying mailbox but does not save memory compared to a string
664 representation.
Georg Brandl116aa622007-08-15 14:28:22 +0000665
666
Benjamin Petersone41251e2008-04-25 01:59:09 +0000667 .. method:: lock()
668 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000669
Benjamin Petersone41251e2008-04-25 01:59:09 +0000670 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000671 :c:func:`flock` and :c:func:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000672
673
674.. seealso::
675
676 `Format of Version 5 Babyl Files <http://quimby.gnus.org/notes/BABYL>`_
677 A specification of the Babyl format.
678
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000679 `Reading Mail with Rmail <http://www.gnu.org/software/emacs/manual/html_node/emacs/Rmail.html>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000680 The Rmail manual, with some information on Babyl semantics.
681
682
683.. _mailbox-mmdf:
684
685:class:`MMDF`
686^^^^^^^^^^^^^
687
688
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000689.. class:: MMDF(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000690
691 A subclass of :class:`Mailbox` for mailboxes in MMDF format. Parameter *factory*
692 is a callable object that accepts a file-like message representation (which
693 behaves as if opened in binary mode) and returns a custom representation. If
694 *factory* is ``None``, :class:`MMDFMessage` is used as the default message
695 representation. If *create* is ``True``, the mailbox is created if it does not
696 exist.
697
Benjamin Petersone41251e2008-04-25 01:59:09 +0000698 MMDF is a single-file mailbox format invented for the Multichannel Memorandum
699 Distribution Facility, a mail transfer agent. Each message is in the same
700 form as an mbox message but is bracketed before and after by lines containing
701 four Control-A (``'\001'``) characters. As with the mbox format, the
702 beginning of each message is indicated by a line whose first five characters
703 are "From ", but additional occurrences of "From " are not transformed to
704 ">From " when storing messages because the extra message separator lines
705 prevent mistaking such occurrences for the starts of subsequent messages.
Georg Brandl116aa622007-08-15 14:28:22 +0000706
Benjamin Petersone41251e2008-04-25 01:59:09 +0000707 Some :class:`Mailbox` methods implemented by :class:`MMDF` deserve special
708 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000709
710
Benjamin Petersone41251e2008-04-25 01:59:09 +0000711 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000712
Benjamin Petersone41251e2008-04-25 01:59:09 +0000713 Using the file after calling :meth:`flush` or :meth:`close` on the
714 :class:`MMDF` instance may yield unpredictable results or raise an
715 exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000716
717
Benjamin Petersone41251e2008-04-25 01:59:09 +0000718 .. method:: lock()
719 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000720
Benjamin Petersone41251e2008-04-25 01:59:09 +0000721 Three locking mechanisms are used---dot locking and, if available, the
Georg Brandl60203b42010-10-06 10:11:56 +0000722 :c:func:`flock` and :c:func:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000723
724
725.. seealso::
726
727 `mmdf man page from tin <http://www.tin.org/bin/man.cgi?section=5&topic=mmdf>`_
728 A specification of MMDF format from the documentation of tin, a newsreader.
729
730 `MMDF <http://en.wikipedia.org/wiki/MMDF>`_
731 A Wikipedia article describing the Multichannel Memorandum Distribution
732 Facility.
733
734
735.. _mailbox-message-objects:
736
737:class:`Message` objects
738------------------------
739
740
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000741.. class:: Message(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000742
743 A subclass of the :mod:`email.Message` module's :class:`Message`. Subclasses of
744 :class:`mailbox.Message` add mailbox-format-specific state and behavior.
745
746 If *message* is omitted, the new instance is created in a default, empty state.
747 If *message* is an :class:`email.Message.Message` instance, its contents are
748 copied; furthermore, any format-specific information is converted insofar as
749 possible if *message* is a :class:`Message` instance. If *message* is a string
750 or a file, it should contain an :rfc:`2822`\ -compliant message, which is read
751 and parsed.
752
Benjamin Petersone41251e2008-04-25 01:59:09 +0000753 The format-specific state and behaviors offered by subclasses vary, but in
754 general it is only the properties that are not specific to a particular
755 mailbox that are supported (although presumably the properties are specific
756 to a particular mailbox format). For example, file offsets for single-file
757 mailbox formats and file names for directory-based mailbox formats are not
758 retained, because they are only applicable to the original mailbox. But state
759 such as whether a message has been read by the user or marked as important is
760 retained, because it applies to the message itself.
Georg Brandl116aa622007-08-15 14:28:22 +0000761
Benjamin Petersone41251e2008-04-25 01:59:09 +0000762 There is no requirement that :class:`Message` instances be used to represent
763 messages retrieved using :class:`Mailbox` instances. In some situations, the
764 time and memory required to generate :class:`Message` representations might
765 not not acceptable. For such situations, :class:`Mailbox` instances also
766 offer string and file-like representations, and a custom message factory may
767 be specified when a :class:`Mailbox` instance is initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000768
769
770.. _mailbox-maildirmessage:
771
772:class:`MaildirMessage`
773^^^^^^^^^^^^^^^^^^^^^^^
774
775
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000776.. class:: MaildirMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000777
778 A message with Maildir-specific behaviors. Parameter *message* has the same
779 meaning as with the :class:`Message` constructor.
780
Benjamin Petersone41251e2008-04-25 01:59:09 +0000781 Typically, a mail user agent application moves all of the messages in the
782 :file:`new` subdirectory to the :file:`cur` subdirectory after the first time
783 the user opens and closes the mailbox, recording that the messages are old
784 whether or not they've actually been read. Each message in :file:`cur` has an
785 "info" section added to its file name to store information about its state.
786 (Some mail readers may also add an "info" section to messages in
787 :file:`new`.) The "info" section may take one of two forms: it may contain
788 "2," followed by a list of standardized flags (e.g., "2,FR") or it may
789 contain "1," followed by so-called experimental information. Standard flags
790 for Maildir messages are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000791
Benjamin Petersone41251e2008-04-25 01:59:09 +0000792 +------+---------+--------------------------------+
793 | Flag | Meaning | Explanation |
794 +======+=========+================================+
795 | D | Draft | Under composition |
796 +------+---------+--------------------------------+
797 | F | Flagged | Marked as important |
798 +------+---------+--------------------------------+
799 | P | Passed | Forwarded, resent, or bounced |
800 +------+---------+--------------------------------+
801 | R | Replied | Replied to |
802 +------+---------+--------------------------------+
803 | S | Seen | Read |
804 +------+---------+--------------------------------+
805 | T | Trashed | Marked for subsequent deletion |
806 +------+---------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000807
Benjamin Petersone41251e2008-04-25 01:59:09 +0000808 :class:`MaildirMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000809
810
Benjamin Petersone41251e2008-04-25 01:59:09 +0000811 .. method:: get_subdir()
Georg Brandl116aa622007-08-15 14:28:22 +0000812
Benjamin Petersone41251e2008-04-25 01:59:09 +0000813 Return either "new" (if the message should be stored in the :file:`new`
814 subdirectory) or "cur" (if the message should be stored in the :file:`cur`
815 subdirectory).
Georg Brandl116aa622007-08-15 14:28:22 +0000816
Benjamin Petersone41251e2008-04-25 01:59:09 +0000817 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000818
Benjamin Petersone41251e2008-04-25 01:59:09 +0000819 A message is typically moved from :file:`new` to :file:`cur` after its
820 mailbox has been accessed, whether or not the message is has been
821 read. A message ``msg`` has been read if ``"S" in msg.get_flags()`` is
822 ``True``.
Georg Brandl116aa622007-08-15 14:28:22 +0000823
824
Benjamin Petersone41251e2008-04-25 01:59:09 +0000825 .. method:: set_subdir(subdir)
Georg Brandl116aa622007-08-15 14:28:22 +0000826
Benjamin Petersone41251e2008-04-25 01:59:09 +0000827 Set the subdirectory the message should be stored in. Parameter *subdir*
828 must be either "new" or "cur".
Georg Brandl116aa622007-08-15 14:28:22 +0000829
830
Benjamin Petersone41251e2008-04-25 01:59:09 +0000831 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +0000832
Benjamin Petersone41251e2008-04-25 01:59:09 +0000833 Return a string specifying the flags that are currently set. If the
834 message complies with the standard Maildir format, the result is the
835 concatenation in alphabetical order of zero or one occurrence of each of
836 ``'D'``, ``'F'``, ``'P'``, ``'R'``, ``'S'``, and ``'T'``. The empty string
837 is returned if no flags are set or if "info" contains experimental
838 semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000839
840
Benjamin Petersone41251e2008-04-25 01:59:09 +0000841 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +0000842
Benjamin Petersone41251e2008-04-25 01:59:09 +0000843 Set the flags specified by *flags* and unset all others.
Georg Brandl116aa622007-08-15 14:28:22 +0000844
845
Benjamin Petersone41251e2008-04-25 01:59:09 +0000846 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000847
Benjamin Petersone41251e2008-04-25 01:59:09 +0000848 Set the flag(s) specified by *flag* without changing other flags. To add
849 more than one flag at a time, *flag* may be a string of more than one
850 character. The current "info" is overwritten whether or not it contains
851 experimental information rather than flags.
Georg Brandl116aa622007-08-15 14:28:22 +0000852
853
Benjamin Petersone41251e2008-04-25 01:59:09 +0000854 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000855
Benjamin Petersone41251e2008-04-25 01:59:09 +0000856 Unset the flag(s) specified by *flag* without changing other flags. To
857 remove more than one flag at a time, *flag* maybe a string of more than
858 one character. If "info" contains experimental information rather than
859 flags, the current "info" is not modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000860
861
Benjamin Petersone41251e2008-04-25 01:59:09 +0000862 .. method:: get_date()
Georg Brandl116aa622007-08-15 14:28:22 +0000863
Benjamin Petersone41251e2008-04-25 01:59:09 +0000864 Return the delivery date of the message as a floating-point number
865 representing seconds since the epoch.
Georg Brandl116aa622007-08-15 14:28:22 +0000866
867
Benjamin Petersone41251e2008-04-25 01:59:09 +0000868 .. method:: set_date(date)
Georg Brandl116aa622007-08-15 14:28:22 +0000869
Benjamin Petersone41251e2008-04-25 01:59:09 +0000870 Set the delivery date of the message to *date*, a floating-point number
871 representing seconds since the epoch.
Georg Brandl116aa622007-08-15 14:28:22 +0000872
873
Benjamin Petersone41251e2008-04-25 01:59:09 +0000874 .. method:: get_info()
Georg Brandl116aa622007-08-15 14:28:22 +0000875
Benjamin Petersone41251e2008-04-25 01:59:09 +0000876 Return a string containing the "info" for a message. This is useful for
877 accessing and modifying "info" that is experimental (i.e., not a list of
878 flags).
Georg Brandl116aa622007-08-15 14:28:22 +0000879
880
Benjamin Petersone41251e2008-04-25 01:59:09 +0000881 .. method:: set_info(info)
Georg Brandl116aa622007-08-15 14:28:22 +0000882
Benjamin Petersone41251e2008-04-25 01:59:09 +0000883 Set "info" to *info*, which should be a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000884
885When a :class:`MaildirMessage` instance is created based upon an
886:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
887and :mailheader:`X-Status` headers are omitted and the following conversions
888take place:
889
890+--------------------+----------------------------------------------+
891| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
892| | state |
893+====================+==============================================+
894| "cur" subdirectory | O flag |
895+--------------------+----------------------------------------------+
896| F flag | F flag |
897+--------------------+----------------------------------------------+
898| R flag | A flag |
899+--------------------+----------------------------------------------+
900| S flag | R flag |
901+--------------------+----------------------------------------------+
902| T flag | D flag |
903+--------------------+----------------------------------------------+
904
905When a :class:`MaildirMessage` instance is created based upon an
906:class:`MHMessage` instance, the following conversions take place:
907
908+-------------------------------+--------------------------+
909| Resulting state | :class:`MHMessage` state |
910+===============================+==========================+
911| "cur" subdirectory | "unseen" sequence |
912+-------------------------------+--------------------------+
913| "cur" subdirectory and S flag | no "unseen" sequence |
914+-------------------------------+--------------------------+
915| F flag | "flagged" sequence |
916+-------------------------------+--------------------------+
917| R flag | "replied" sequence |
918+-------------------------------+--------------------------+
919
920When a :class:`MaildirMessage` instance is created based upon a
921:class:`BabylMessage` instance, the following conversions take place:
922
923+-------------------------------+-------------------------------+
924| Resulting state | :class:`BabylMessage` state |
925+===============================+===============================+
926| "cur" subdirectory | "unseen" label |
927+-------------------------------+-------------------------------+
928| "cur" subdirectory and S flag | no "unseen" label |
929+-------------------------------+-------------------------------+
930| P flag | "forwarded" or "resent" label |
931+-------------------------------+-------------------------------+
932| R flag | "answered" label |
933+-------------------------------+-------------------------------+
934| T flag | "deleted" label |
935+-------------------------------+-------------------------------+
936
937
938.. _mailbox-mboxmessage:
939
940:class:`mboxMessage`
941^^^^^^^^^^^^^^^^^^^^
942
943
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000944.. class:: mboxMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000945
946 A message with mbox-specific behaviors. Parameter *message* has the same meaning
947 as with the :class:`Message` constructor.
948
Benjamin Petersone41251e2008-04-25 01:59:09 +0000949 Messages in an mbox mailbox are stored together in a single file. The
950 sender's envelope address and the time of delivery are typically stored in a
951 line beginning with "From " that is used to indicate the start of a message,
952 though there is considerable variation in the exact format of this data among
953 mbox implementations. Flags that indicate the state of the message, such as
954 whether it has been read or marked as important, are typically stored in
955 :mailheader:`Status` and :mailheader:`X-Status` headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000956
Benjamin Petersone41251e2008-04-25 01:59:09 +0000957 Conventional flags for mbox messages are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000958
Benjamin Petersone41251e2008-04-25 01:59:09 +0000959 +------+----------+--------------------------------+
960 | Flag | Meaning | Explanation |
961 +======+==========+================================+
962 | R | Read | Read |
963 +------+----------+--------------------------------+
964 | O | Old | Previously detected by MUA |
965 +------+----------+--------------------------------+
966 | D | Deleted | Marked for subsequent deletion |
967 +------+----------+--------------------------------+
968 | F | Flagged | Marked as important |
969 +------+----------+--------------------------------+
970 | A | Answered | Replied to |
971 +------+----------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000972
Benjamin Petersone41251e2008-04-25 01:59:09 +0000973 The "R" and "O" flags are stored in the :mailheader:`Status` header, and the
974 "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The
975 flags and headers typically appear in the order mentioned.
Georg Brandl116aa622007-08-15 14:28:22 +0000976
Benjamin Petersone41251e2008-04-25 01:59:09 +0000977 :class:`mboxMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000978
979
Benjamin Petersone41251e2008-04-25 01:59:09 +0000980 .. method:: get_from()
Georg Brandl116aa622007-08-15 14:28:22 +0000981
Benjamin Petersone41251e2008-04-25 01:59:09 +0000982 Return a string representing the "From " line that marks the start of the
983 message in an mbox mailbox. The leading "From " and the trailing newline
984 are excluded.
Georg Brandl116aa622007-08-15 14:28:22 +0000985
986
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000987 .. method:: set_from(from_, time_=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000988
Benjamin Petersone41251e2008-04-25 01:59:09 +0000989 Set the "From " line to *from_*, which should be specified without a
990 leading "From " or trailing newline. For convenience, *time_* may be
991 specified and will be formatted appropriately and appended to *from_*. If
992 *time_* is specified, it should be a :class:`struct_time` instance, a
993 tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use
994 :meth:`time.gmtime`).
Georg Brandl116aa622007-08-15 14:28:22 +0000995
996
Benjamin Petersone41251e2008-04-25 01:59:09 +0000997 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +0000998
Benjamin Petersone41251e2008-04-25 01:59:09 +0000999 Return a string specifying the flags that are currently set. If the
1000 message complies with the conventional format, the result is the
1001 concatenation in the following order of zero or one occurrence of each of
1002 ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001003
1004
Benjamin Petersone41251e2008-04-25 01:59:09 +00001005 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +00001006
Benjamin Petersone41251e2008-04-25 01:59:09 +00001007 Set the flags specified by *flags* and unset all others. Parameter *flags*
1008 should be the concatenation in any order of zero or more occurrences of
1009 each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001010
1011
Benjamin Petersone41251e2008-04-25 01:59:09 +00001012 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001013
Benjamin Petersone41251e2008-04-25 01:59:09 +00001014 Set the flag(s) specified by *flag* without changing other flags. To add
1015 more than one flag at a time, *flag* may be a string of more than one
1016 character.
Georg Brandl116aa622007-08-15 14:28:22 +00001017
1018
Benjamin Petersone41251e2008-04-25 01:59:09 +00001019 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001020
Benjamin Petersone41251e2008-04-25 01:59:09 +00001021 Unset the flag(s) specified by *flag* without changing other flags. To
1022 remove more than one flag at a time, *flag* maybe a string of more than
1023 one character.
Georg Brandl116aa622007-08-15 14:28:22 +00001024
1025When an :class:`mboxMessage` instance is created based upon a
1026:class:`MaildirMessage` instance, a "From " line is generated based upon the
1027:class:`MaildirMessage` instance's delivery date, and the following conversions
1028take place:
1029
1030+-----------------+-------------------------------+
1031| Resulting state | :class:`MaildirMessage` state |
1032+=================+===============================+
1033| R flag | S flag |
1034+-----------------+-------------------------------+
1035| O flag | "cur" subdirectory |
1036+-----------------+-------------------------------+
1037| D flag | T flag |
1038+-----------------+-------------------------------+
1039| F flag | F flag |
1040+-----------------+-------------------------------+
1041| A flag | R flag |
1042+-----------------+-------------------------------+
1043
1044When an :class:`mboxMessage` instance is created based upon an
1045:class:`MHMessage` instance, the following conversions take place:
1046
1047+-------------------+--------------------------+
1048| Resulting state | :class:`MHMessage` state |
1049+===================+==========================+
1050| R flag and O flag | no "unseen" sequence |
1051+-------------------+--------------------------+
1052| O flag | "unseen" sequence |
1053+-------------------+--------------------------+
1054| F flag | "flagged" sequence |
1055+-------------------+--------------------------+
1056| A flag | "replied" sequence |
1057+-------------------+--------------------------+
1058
1059When an :class:`mboxMessage` instance is created based upon a
1060:class:`BabylMessage` instance, the following conversions take place:
1061
1062+-------------------+-----------------------------+
1063| Resulting state | :class:`BabylMessage` state |
1064+===================+=============================+
1065| R flag and O flag | no "unseen" label |
1066+-------------------+-----------------------------+
1067| O flag | "unseen" label |
1068+-------------------+-----------------------------+
1069| D flag | "deleted" label |
1070+-------------------+-----------------------------+
1071| A flag | "answered" label |
1072+-------------------+-----------------------------+
1073
1074When a :class:`Message` instance is created based upon an :class:`MMDFMessage`
1075instance, the "From " line is copied and all flags directly correspond:
1076
1077+-----------------+----------------------------+
1078| Resulting state | :class:`MMDFMessage` state |
1079+=================+============================+
1080| R flag | R flag |
1081+-----------------+----------------------------+
1082| O flag | O flag |
1083+-----------------+----------------------------+
1084| D flag | D flag |
1085+-----------------+----------------------------+
1086| F flag | F flag |
1087+-----------------+----------------------------+
1088| A flag | A flag |
1089+-----------------+----------------------------+
1090
1091
1092.. _mailbox-mhmessage:
1093
1094:class:`MHMessage`
1095^^^^^^^^^^^^^^^^^^
1096
1097
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001098.. class:: MHMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001099
1100 A message with MH-specific behaviors. Parameter *message* has the same meaning
1101 as with the :class:`Message` constructor.
1102
Benjamin Petersone41251e2008-04-25 01:59:09 +00001103 MH messages do not support marks or flags in the traditional sense, but they
1104 do support sequences, which are logical groupings of arbitrary messages. Some
1105 mail reading programs (although not the standard :program:`mh` and
1106 :program:`nmh`) use sequences in much the same way flags are used with other
1107 formats, as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001108
Benjamin Petersone41251e2008-04-25 01:59:09 +00001109 +----------+------------------------------------------+
1110 | Sequence | Explanation |
1111 +==========+==========================================+
1112 | unseen | Not read, but previously detected by MUA |
1113 +----------+------------------------------------------+
1114 | replied | Replied to |
1115 +----------+------------------------------------------+
1116 | flagged | Marked as important |
1117 +----------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001118
Benjamin Petersone41251e2008-04-25 01:59:09 +00001119 :class:`MHMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001120
1121
Benjamin Petersone41251e2008-04-25 01:59:09 +00001122 .. method:: get_sequences()
Georg Brandl116aa622007-08-15 14:28:22 +00001123
Benjamin Petersone41251e2008-04-25 01:59:09 +00001124 Return a list of the names of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001125
1126
Benjamin Petersone41251e2008-04-25 01:59:09 +00001127 .. method:: set_sequences(sequences)
Georg Brandl116aa622007-08-15 14:28:22 +00001128
Benjamin Petersone41251e2008-04-25 01:59:09 +00001129 Set the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001130
1131
Benjamin Petersone41251e2008-04-25 01:59:09 +00001132 .. method:: add_sequence(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +00001133
Benjamin Petersone41251e2008-04-25 01:59:09 +00001134 Add *sequence* to the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001135
1136
Benjamin Petersone41251e2008-04-25 01:59:09 +00001137 .. method:: remove_sequence(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +00001138
Benjamin Petersone41251e2008-04-25 01:59:09 +00001139 Remove *sequence* from the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001140
1141When an :class:`MHMessage` instance is created based upon a
1142:class:`MaildirMessage` instance, the following conversions take place:
1143
1144+--------------------+-------------------------------+
1145| Resulting state | :class:`MaildirMessage` state |
1146+====================+===============================+
1147| "unseen" sequence | no S flag |
1148+--------------------+-------------------------------+
1149| "replied" sequence | R flag |
1150+--------------------+-------------------------------+
1151| "flagged" sequence | F flag |
1152+--------------------+-------------------------------+
1153
1154When an :class:`MHMessage` instance is created based upon an
1155:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
1156and :mailheader:`X-Status` headers are omitted and the following conversions
1157take place:
1158
1159+--------------------+----------------------------------------------+
1160| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
1161| | state |
1162+====================+==============================================+
1163| "unseen" sequence | no R flag |
1164+--------------------+----------------------------------------------+
1165| "replied" sequence | A flag |
1166+--------------------+----------------------------------------------+
1167| "flagged" sequence | F flag |
1168+--------------------+----------------------------------------------+
1169
1170When an :class:`MHMessage` instance is created based upon a
1171:class:`BabylMessage` instance, the following conversions take place:
1172
1173+--------------------+-----------------------------+
1174| Resulting state | :class:`BabylMessage` state |
1175+====================+=============================+
1176| "unseen" sequence | "unseen" label |
1177+--------------------+-----------------------------+
1178| "replied" sequence | "answered" label |
1179+--------------------+-----------------------------+
1180
1181
1182.. _mailbox-babylmessage:
1183
1184:class:`BabylMessage`
1185^^^^^^^^^^^^^^^^^^^^^
1186
1187
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001188.. class:: BabylMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001189
1190 A message with Babyl-specific behaviors. Parameter *message* has the same
1191 meaning as with the :class:`Message` constructor.
1192
Benjamin Petersone41251e2008-04-25 01:59:09 +00001193 Certain message labels, called :dfn:`attributes`, are defined by convention
1194 to have special meanings. The attributes are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001195
Benjamin Petersone41251e2008-04-25 01:59:09 +00001196 +-----------+------------------------------------------+
1197 | Label | Explanation |
1198 +===========+==========================================+
1199 | unseen | Not read, but previously detected by MUA |
1200 +-----------+------------------------------------------+
1201 | deleted | Marked for subsequent deletion |
1202 +-----------+------------------------------------------+
1203 | filed | Copied to another file or mailbox |
1204 +-----------+------------------------------------------+
1205 | answered | Replied to |
1206 +-----------+------------------------------------------+
1207 | forwarded | Forwarded |
1208 +-----------+------------------------------------------+
1209 | edited | Modified by the user |
1210 +-----------+------------------------------------------+
1211 | resent | Resent |
1212 +-----------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001213
Benjamin Petersone41251e2008-04-25 01:59:09 +00001214 By default, Rmail displays only visible headers. The :class:`BabylMessage`
1215 class, though, uses the original headers because they are more
1216 complete. Visible headers may be accessed explicitly if desired.
Georg Brandl116aa622007-08-15 14:28:22 +00001217
Benjamin Petersone41251e2008-04-25 01:59:09 +00001218 :class:`BabylMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001219
1220
Benjamin Petersone41251e2008-04-25 01:59:09 +00001221 .. method:: get_labels()
Georg Brandl116aa622007-08-15 14:28:22 +00001222
Benjamin Petersone41251e2008-04-25 01:59:09 +00001223 Return a list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001224
1225
Benjamin Petersone41251e2008-04-25 01:59:09 +00001226 .. method:: set_labels(labels)
Georg Brandl116aa622007-08-15 14:28:22 +00001227
Benjamin Petersone41251e2008-04-25 01:59:09 +00001228 Set the list of labels on the message to *labels*.
Georg Brandl116aa622007-08-15 14:28:22 +00001229
1230
Benjamin Petersone41251e2008-04-25 01:59:09 +00001231 .. method:: add_label(label)
Georg Brandl116aa622007-08-15 14:28:22 +00001232
Benjamin Petersone41251e2008-04-25 01:59:09 +00001233 Add *label* to the list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001234
1235
Benjamin Petersone41251e2008-04-25 01:59:09 +00001236 .. method:: remove_label(label)
Georg Brandl116aa622007-08-15 14:28:22 +00001237
Benjamin Petersone41251e2008-04-25 01:59:09 +00001238 Remove *label* from the list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001239
1240
Benjamin Petersone41251e2008-04-25 01:59:09 +00001241 .. method:: get_visible()
Georg Brandl116aa622007-08-15 14:28:22 +00001242
Benjamin Petersone41251e2008-04-25 01:59:09 +00001243 Return an :class:`Message` instance whose headers are the message's
1244 visible headers and whose body is empty.
Georg Brandl116aa622007-08-15 14:28:22 +00001245
1246
Benjamin Petersone41251e2008-04-25 01:59:09 +00001247 .. method:: set_visible(visible)
Georg Brandl116aa622007-08-15 14:28:22 +00001248
Benjamin Petersone41251e2008-04-25 01:59:09 +00001249 Set the message's visible headers to be the same as the headers in
1250 *message*. Parameter *visible* should be a :class:`Message` instance, an
1251 :class:`email.Message.Message` instance, a string, or a file-like object
1252 (which should be open in text mode).
Georg Brandl116aa622007-08-15 14:28:22 +00001253
1254
Benjamin Petersone41251e2008-04-25 01:59:09 +00001255 .. method:: update_visible()
Georg Brandl116aa622007-08-15 14:28:22 +00001256
Benjamin Petersone41251e2008-04-25 01:59:09 +00001257 When a :class:`BabylMessage` instance's original headers are modified, the
1258 visible headers are not automatically modified to correspond. This method
1259 updates the visible headers as follows: each visible header with a
1260 corresponding original header is set to the value of the original header,
1261 each visible header without a corresponding original header is removed,
1262 and any of :mailheader:`Date`, :mailheader:`From`, :mailheader:`Reply-To`,
1263 :mailheader:`To`, :mailheader:`CC`, and :mailheader:`Subject` that are
1264 present in the original headers but not the visible headers are added to
1265 the visible headers.
Georg Brandl116aa622007-08-15 14:28:22 +00001266
1267When a :class:`BabylMessage` instance is created based upon a
1268:class:`MaildirMessage` instance, the following conversions take place:
1269
1270+-------------------+-------------------------------+
1271| Resulting state | :class:`MaildirMessage` state |
1272+===================+===============================+
1273| "unseen" label | no S flag |
1274+-------------------+-------------------------------+
1275| "deleted" label | T flag |
1276+-------------------+-------------------------------+
1277| "answered" label | R flag |
1278+-------------------+-------------------------------+
1279| "forwarded" label | P flag |
1280+-------------------+-------------------------------+
1281
1282When a :class:`BabylMessage` instance is created based upon an
1283:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
1284and :mailheader:`X-Status` headers are omitted and the following conversions
1285take place:
1286
1287+------------------+----------------------------------------------+
1288| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
1289| | state |
1290+==================+==============================================+
1291| "unseen" label | no R flag |
1292+------------------+----------------------------------------------+
1293| "deleted" label | D flag |
1294+------------------+----------------------------------------------+
1295| "answered" label | A flag |
1296+------------------+----------------------------------------------+
1297
1298When a :class:`BabylMessage` instance is created based upon an
1299:class:`MHMessage` instance, the following conversions take place:
1300
1301+------------------+--------------------------+
1302| Resulting state | :class:`MHMessage` state |
1303+==================+==========================+
1304| "unseen" label | "unseen" sequence |
1305+------------------+--------------------------+
1306| "answered" label | "replied" sequence |
1307+------------------+--------------------------+
1308
1309
1310.. _mailbox-mmdfmessage:
1311
1312:class:`MMDFMessage`
1313^^^^^^^^^^^^^^^^^^^^
1314
1315
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001316.. class:: MMDFMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001317
1318 A message with MMDF-specific behaviors. Parameter *message* has the same meaning
1319 as with the :class:`Message` constructor.
1320
Benjamin Petersone41251e2008-04-25 01:59:09 +00001321 As with message in an mbox mailbox, MMDF messages are stored with the
1322 sender's address and the delivery date in an initial line beginning with
1323 "From ". Likewise, flags that indicate the state of the message are
1324 typically stored in :mailheader:`Status` and :mailheader:`X-Status` headers.
Georg Brandl116aa622007-08-15 14:28:22 +00001325
Benjamin Petersone41251e2008-04-25 01:59:09 +00001326 Conventional flags for MMDF messages are identical to those of mbox message
1327 and are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001328
Benjamin Petersone41251e2008-04-25 01:59:09 +00001329 +------+----------+--------------------------------+
1330 | Flag | Meaning | Explanation |
1331 +======+==========+================================+
1332 | R | Read | Read |
1333 +------+----------+--------------------------------+
1334 | O | Old | Previously detected by MUA |
1335 +------+----------+--------------------------------+
1336 | D | Deleted | Marked for subsequent deletion |
1337 +------+----------+--------------------------------+
1338 | F | Flagged | Marked as important |
1339 +------+----------+--------------------------------+
1340 | A | Answered | Replied to |
1341 +------+----------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001342
Benjamin Petersone41251e2008-04-25 01:59:09 +00001343 The "R" and "O" flags are stored in the :mailheader:`Status` header, and the
1344 "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The
1345 flags and headers typically appear in the order mentioned.
Georg Brandl116aa622007-08-15 14:28:22 +00001346
Benjamin Petersone41251e2008-04-25 01:59:09 +00001347 :class:`MMDFMessage` instances offer the following methods, which are
1348 identical to those offered by :class:`mboxMessage`:
Georg Brandl116aa622007-08-15 14:28:22 +00001349
1350
Benjamin Petersone41251e2008-04-25 01:59:09 +00001351 .. method:: get_from()
Georg Brandl116aa622007-08-15 14:28:22 +00001352
Benjamin Petersone41251e2008-04-25 01:59:09 +00001353 Return a string representing the "From " line that marks the start of the
1354 message in an mbox mailbox. The leading "From " and the trailing newline
1355 are excluded.
Georg Brandl116aa622007-08-15 14:28:22 +00001356
1357
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001358 .. method:: set_from(from_, time_=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001359
Benjamin Petersone41251e2008-04-25 01:59:09 +00001360 Set the "From " line to *from_*, which should be specified without a
1361 leading "From " or trailing newline. For convenience, *time_* may be
1362 specified and will be formatted appropriately and appended to *from_*. If
1363 *time_* is specified, it should be a :class:`struct_time` instance, a
1364 tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use
1365 :meth:`time.gmtime`).
Georg Brandl116aa622007-08-15 14:28:22 +00001366
1367
Benjamin Petersone41251e2008-04-25 01:59:09 +00001368 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +00001369
Benjamin Petersone41251e2008-04-25 01:59:09 +00001370 Return a string specifying the flags that are currently set. If the
1371 message complies with the conventional format, the result is the
1372 concatenation in the following order of zero or one occurrence of each of
1373 ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001374
1375
Benjamin Petersone41251e2008-04-25 01:59:09 +00001376 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +00001377
Benjamin Petersone41251e2008-04-25 01:59:09 +00001378 Set the flags specified by *flags* and unset all others. Parameter *flags*
1379 should be the concatenation in any order of zero or more occurrences of
1380 each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001381
1382
Benjamin Petersone41251e2008-04-25 01:59:09 +00001383 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001384
Benjamin Petersone41251e2008-04-25 01:59:09 +00001385 Set the flag(s) specified by *flag* without changing other flags. To add
1386 more than one flag at a time, *flag* may be a string of more than one
1387 character.
Georg Brandl116aa622007-08-15 14:28:22 +00001388
1389
Benjamin Petersone41251e2008-04-25 01:59:09 +00001390 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001391
Benjamin Petersone41251e2008-04-25 01:59:09 +00001392 Unset the flag(s) specified by *flag* without changing other flags. To
1393 remove more than one flag at a time, *flag* maybe a string of more than
1394 one character.
Georg Brandl116aa622007-08-15 14:28:22 +00001395
1396When an :class:`MMDFMessage` instance is created based upon a
1397:class:`MaildirMessage` instance, a "From " line is generated based upon the
1398:class:`MaildirMessage` instance's delivery date, and the following conversions
1399take place:
1400
1401+-----------------+-------------------------------+
1402| Resulting state | :class:`MaildirMessage` state |
1403+=================+===============================+
1404| R flag | S flag |
1405+-----------------+-------------------------------+
1406| O flag | "cur" subdirectory |
1407+-----------------+-------------------------------+
1408| D flag | T flag |
1409+-----------------+-------------------------------+
1410| F flag | F flag |
1411+-----------------+-------------------------------+
1412| A flag | R flag |
1413+-----------------+-------------------------------+
1414
1415When an :class:`MMDFMessage` instance is created based upon an
1416:class:`MHMessage` instance, the following conversions take place:
1417
1418+-------------------+--------------------------+
1419| Resulting state | :class:`MHMessage` state |
1420+===================+==========================+
1421| R flag and O flag | no "unseen" sequence |
1422+-------------------+--------------------------+
1423| O flag | "unseen" sequence |
1424+-------------------+--------------------------+
1425| F flag | "flagged" sequence |
1426+-------------------+--------------------------+
1427| A flag | "replied" sequence |
1428+-------------------+--------------------------+
1429
1430When an :class:`MMDFMessage` instance is created based upon a
1431:class:`BabylMessage` instance, the following conversions take place:
1432
1433+-------------------+-----------------------------+
1434| Resulting state | :class:`BabylMessage` state |
1435+===================+=============================+
1436| R flag and O flag | no "unseen" label |
1437+-------------------+-----------------------------+
1438| O flag | "unseen" label |
1439+-------------------+-----------------------------+
1440| D flag | "deleted" label |
1441+-------------------+-----------------------------+
1442| A flag | "answered" label |
1443+-------------------+-----------------------------+
1444
1445When an :class:`MMDFMessage` instance is created based upon an
1446:class:`mboxMessage` instance, the "From " line is copied and all flags directly
1447correspond:
1448
1449+-----------------+----------------------------+
1450| Resulting state | :class:`mboxMessage` state |
1451+=================+============================+
1452| R flag | R flag |
1453+-----------------+----------------------------+
1454| O flag | O flag |
1455+-----------------+----------------------------+
1456| D flag | D flag |
1457+-----------------+----------------------------+
1458| F flag | F flag |
1459+-----------------+----------------------------+
1460| A flag | A flag |
1461+-----------------+----------------------------+
1462
1463
1464Exceptions
1465----------
1466
1467The following exception classes are defined in the :mod:`mailbox` module:
1468
1469
Benjamin Petersone41251e2008-04-25 01:59:09 +00001470.. exception:: Error()
Georg Brandl116aa622007-08-15 14:28:22 +00001471
1472 The based class for all other module-specific exceptions.
1473
1474
Benjamin Petersone41251e2008-04-25 01:59:09 +00001475.. exception:: NoSuchMailboxError()
Georg Brandl116aa622007-08-15 14:28:22 +00001476
1477 Raised when a mailbox is expected but is not found, such as when instantiating a
1478 :class:`Mailbox` subclass with a path that does not exist (and with the *create*
1479 parameter set to ``False``), or when opening a folder that does not exist.
1480
1481
Georg Brandl734e2682008-08-12 08:18:18 +00001482.. exception:: NotEmptyError()
Georg Brandl116aa622007-08-15 14:28:22 +00001483
1484 Raised when a mailbox is not empty but is expected to be, such as when deleting
1485 a folder that contains messages.
1486
1487
Benjamin Petersone41251e2008-04-25 01:59:09 +00001488.. exception:: ExternalClashError()
Georg Brandl116aa622007-08-15 14:28:22 +00001489
1490 Raised when some mailbox-related condition beyond the control of the program
1491 causes it to be unable to proceed, such as when failing to acquire a lock that
1492 another program already holds a lock, or when a uniquely-generated file name
1493 already exists.
1494
1495
Benjamin Petersone41251e2008-04-25 01:59:09 +00001496.. exception:: FormatError()
Georg Brandl116aa622007-08-15 14:28:22 +00001497
1498 Raised when the data in a file cannot be parsed, such as when an :class:`MH`
1499 instance attempts to read a corrupted :file:`.mh_sequences` file.
1500
1501
Georg Brandl116aa622007-08-15 14:28:22 +00001502.. _mailbox-examples:
1503
1504Examples
1505--------
1506
1507A simple example of printing the subjects of all messages in a mailbox that seem
1508interesting::
1509
1510 import mailbox
1511 for message in mailbox.mbox('~/mbox'):
1512 subject = message['subject'] # Could possibly be None.
1513 if subject and 'python' in subject.lower():
Georg Brandl6911e3c2007-09-04 07:15:32 +00001514 print(subject)
Georg Brandl116aa622007-08-15 14:28:22 +00001515
1516To copy all mail from a Babyl mailbox to an MH mailbox, converting all of the
1517format-specific information that can be converted::
1518
1519 import mailbox
1520 destination = mailbox.MH('~/Mail')
1521 destination.lock()
1522 for message in mailbox.Babyl('~/RMAIL'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001523 destination.add(mailbox.MHMessage(message))
Georg Brandl116aa622007-08-15 14:28:22 +00001524 destination.flush()
1525 destination.unlock()
1526
1527This example sorts mail from several mailing lists into different mailboxes,
1528being careful to avoid mail corruption due to concurrent modification by other
1529programs, mail loss due to interruption of the program, or premature termination
1530due to malformed messages in the mailbox::
1531
1532 import mailbox
1533 import email.Errors
1534
1535 list_names = ('python-list', 'python-dev', 'python-bugs')
1536
Georg Brandlf6945182008-02-01 11:56:49 +00001537 boxes = {name: mailbox.mbox('~/email/%s' % name) for name in list_names}
Georg Brandl116aa622007-08-15 14:28:22 +00001538 inbox = mailbox.Maildir('~/Maildir', factory=None)
1539
1540 for key in inbox.iterkeys():
1541 try:
1542 message = inbox[key]
1543 except email.Errors.MessageParseError:
1544 continue # The message is malformed. Just leave it.
1545
1546 for name in list_names:
1547 list_id = message['list-id']
1548 if list_id and name in list_id:
1549 # Get mailbox to use
1550 box = boxes[name]
1551
1552 # Write copy to disk before removing original.
1553 # If there's a crash, you might duplicate a message, but
1554 # that's better than losing a message completely.
1555 box.lock()
1556 box.add(message)
Georg Brandl48310cd2009-01-03 21:18:54 +00001557 box.flush()
Georg Brandl116aa622007-08-15 14:28:22 +00001558 box.unlock()
1559
1560 # Remove original message
1561 inbox.lock()
1562 inbox.discard(key)
1563 inbox.flush()
1564 inbox.unlock()
1565 break # Found destination, so stop looking.
1566
1567 for box in boxes.itervalues():
1568 box.close()
1569