blob: 7409af5f15755771083c1264c826fbb9ad8d31ff [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*,
183 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
185 closed once it is no longer needed.
Georg Brandl116aa622007-08-15 14:28:22 +0000186
Benjamin Petersone41251e2008-04-25 01:59:09 +0000187 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000188
Benjamin Petersone41251e2008-04-25 01:59:09 +0000189 Unlike other representations of messages, file-like representations are
190 not necessarily independent of the :class:`Mailbox` instance that
191 created them or of the underlying mailbox. More specific documentation
192 is provided by each subclass.
Georg Brandl116aa622007-08-15 14:28:22 +0000193
194
Benjamin Petersone41251e2008-04-25 01:59:09 +0000195 .. method:: __contains__(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000196
Benjamin Petersone41251e2008-04-25 01:59:09 +0000197 Return ``True`` if *key* corresponds to a message, ``False`` otherwise.
Georg Brandl116aa622007-08-15 14:28:22 +0000198
199
Benjamin Petersone41251e2008-04-25 01:59:09 +0000200 .. method:: __len__()
Georg Brandl116aa622007-08-15 14:28:22 +0000201
Benjamin Petersone41251e2008-04-25 01:59:09 +0000202 Return a count of messages in the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000203
204
Benjamin Petersone41251e2008-04-25 01:59:09 +0000205 .. method:: clear()
Georg Brandl116aa622007-08-15 14:28:22 +0000206
Benjamin Petersone41251e2008-04-25 01:59:09 +0000207 Delete all messages from the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000208
209
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000210 .. method:: pop(key, default=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000211
Benjamin Petersone41251e2008-04-25 01:59:09 +0000212 Return a representation of the message corresponding to *key* and delete
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000213 the message. If no such message exists, return *default*. The message is
Benjamin Petersone41251e2008-04-25 01:59:09 +0000214 represented as an instance of the appropriate format-specific
215 :class:`Message` subclass unless a custom message factory was specified
216 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000217
218
Benjamin Petersone41251e2008-04-25 01:59:09 +0000219 .. method:: popitem()
Georg Brandl116aa622007-08-15 14:28:22 +0000220
Benjamin Petersone41251e2008-04-25 01:59:09 +0000221 Return an arbitrary (*key*, *message*) pair, where *key* is a key and
222 *message* is a message representation, and delete the corresponding
223 message. If the mailbox is empty, raise a :exc:`KeyError` exception. The
224 message is represented as an instance of the appropriate format-specific
225 :class:`Message` subclass unless a custom message factory was specified
226 when the :class:`Mailbox` instance was initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000227
228
Benjamin Petersone41251e2008-04-25 01:59:09 +0000229 .. method:: update(arg)
Georg Brandl116aa622007-08-15 14:28:22 +0000230
Benjamin Petersone41251e2008-04-25 01:59:09 +0000231 Parameter *arg* should be a *key*-to-*message* mapping or an iterable of
232 (*key*, *message*) pairs. Updates the mailbox so that, for each given
233 *key* and *message*, the message corresponding to *key* is set to
234 *message* as if by using :meth:`__setitem__`. As with :meth:`__setitem__`,
235 each *key* must already correspond to a message in the mailbox or else a
236 :exc:`KeyError` exception will be raised, so in general it is incorrect
237 for *arg* to be a :class:`Mailbox` instance.
Georg Brandl116aa622007-08-15 14:28:22 +0000238
Benjamin Petersone41251e2008-04-25 01:59:09 +0000239 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000240
Benjamin Petersone41251e2008-04-25 01:59:09 +0000241 Unlike with dictionaries, keyword arguments are not supported.
Georg Brandl116aa622007-08-15 14:28:22 +0000242
243
Benjamin Petersone41251e2008-04-25 01:59:09 +0000244 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000245
Benjamin Petersone41251e2008-04-25 01:59:09 +0000246 Write any pending changes to the filesystem. For some :class:`Mailbox`
247 subclasses, changes are always written immediately and :meth:`flush` does
248 nothing, but you should still make a habit of calling this method.
Georg Brandl116aa622007-08-15 14:28:22 +0000249
250
Benjamin Petersone41251e2008-04-25 01:59:09 +0000251 .. method:: lock()
Georg Brandl116aa622007-08-15 14:28:22 +0000252
Benjamin Petersone41251e2008-04-25 01:59:09 +0000253 Acquire an exclusive advisory lock on the mailbox so that other processes
254 know not to modify it. An :exc:`ExternalClashError` is raised if the lock
255 is not available. The particular locking mechanisms used depend upon the
256 mailbox format. You should *always* lock the mailbox before making any
257 modifications to its contents.
Georg Brandl116aa622007-08-15 14:28:22 +0000258
259
Benjamin Petersone41251e2008-04-25 01:59:09 +0000260 .. method:: unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000261
Benjamin Petersone41251e2008-04-25 01:59:09 +0000262 Release the lock on the mailbox, if any.
Georg Brandl116aa622007-08-15 14:28:22 +0000263
264
Benjamin Petersone41251e2008-04-25 01:59:09 +0000265 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000266
Benjamin Petersone41251e2008-04-25 01:59:09 +0000267 Flush the mailbox, unlock it if necessary, and close any open files. For
268 some :class:`Mailbox` subclasses, this method does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000269
270
271.. _mailbox-maildir:
272
273:class:`Maildir`
274^^^^^^^^^^^^^^^^
275
276
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000277.. class:: Maildir(dirname, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000278
279 A subclass of :class:`Mailbox` for mailboxes in Maildir format. Parameter
280 *factory* is a callable object that accepts a file-like message representation
281 (which behaves as if opened in binary mode) and returns a custom representation.
282 If *factory* is ``None``, :class:`MaildirMessage` is used as the default message
283 representation. If *create* is ``True``, the mailbox is created if it does not
284 exist.
285
Georg Brandlaa5b4112008-05-11 20:51:18 +0000286 It is for historical reasons that *dirname* is named as such rather than *path*.
Georg Brandl116aa622007-08-15 14:28:22 +0000287
Benjamin Petersone41251e2008-04-25 01:59:09 +0000288 Maildir is a directory-based mailbox format invented for the qmail mail
289 transfer agent and now widely supported by other programs. Messages in a
290 Maildir mailbox are stored in separate files within a common directory
291 structure. This design allows Maildir mailboxes to be accessed and modified
292 by multiple unrelated programs without data corruption, so file locking is
293 unnecessary.
Georg Brandl116aa622007-08-15 14:28:22 +0000294
Benjamin Petersone41251e2008-04-25 01:59:09 +0000295 Maildir mailboxes contain three subdirectories, namely: :file:`tmp`,
296 :file:`new`, and :file:`cur`. Messages are created momentarily in the
297 :file:`tmp` subdirectory and then moved to the :file:`new` subdirectory to
298 finalize delivery. A mail user agent may subsequently move the message to the
299 :file:`cur` subdirectory and store information about the state of the message
300 in a special "info" section appended to its file name.
Georg Brandl116aa622007-08-15 14:28:22 +0000301
Benjamin Petersone41251e2008-04-25 01:59:09 +0000302 Folders of the style introduced by the Courier mail transfer agent are also
303 supported. Any subdirectory of the main mailbox is considered a folder if
304 ``'.'`` is the first character in its name. Folder names are represented by
305 :class:`Maildir` without the leading ``'.'``. Each folder is itself a Maildir
306 mailbox but should not contain other folders. Instead, a logical nesting is
307 indicated using ``'.'`` to delimit levels, e.g., "Archived.2005.07".
Georg Brandl116aa622007-08-15 14:28:22 +0000308
Benjamin Petersone41251e2008-04-25 01:59:09 +0000309 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000310
Benjamin Petersone41251e2008-04-25 01:59:09 +0000311 The Maildir specification requires the use of a colon (``':'``) in certain
312 message file names. However, some operating systems do not permit this
313 character in file names, If you wish to use a Maildir-like format on such
314 an operating system, you should specify another character to use
315 instead. The exclamation point (``'!'``) is a popular choice. For
316 example::
Georg Brandl116aa622007-08-15 14:28:22 +0000317
Benjamin Petersone41251e2008-04-25 01:59:09 +0000318 import mailbox
319 mailbox.Maildir.colon = '!'
Georg Brandl116aa622007-08-15 14:28:22 +0000320
Benjamin Petersone41251e2008-04-25 01:59:09 +0000321 The :attr:`colon` attribute may also be set on a per-instance basis.
Georg Brandl116aa622007-08-15 14:28:22 +0000322
Benjamin Petersone41251e2008-04-25 01:59:09 +0000323 :class:`Maildir` instances have all of the methods of :class:`Mailbox` in
324 addition to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000325
326
Benjamin Petersone41251e2008-04-25 01:59:09 +0000327 .. method:: list_folders()
Georg Brandl116aa622007-08-15 14:28:22 +0000328
Benjamin Petersone41251e2008-04-25 01:59:09 +0000329 Return a list of the names of all folders.
Georg Brandl116aa622007-08-15 14:28:22 +0000330
331
Ezio Melotti61c34d82009-09-15 18:57:03 +0000332 .. method:: get_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000333
Benjamin Petersone41251e2008-04-25 01:59:09 +0000334 Return a :class:`Maildir` instance representing the folder whose name is
335 *folder*. A :exc:`NoSuchMailboxError` exception is raised if the folder
336 does not exist.
Georg Brandl116aa622007-08-15 14:28:22 +0000337
338
Benjamin Petersone41251e2008-04-25 01:59:09 +0000339 .. method:: add_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000340
Benjamin Petersone41251e2008-04-25 01:59:09 +0000341 Create a folder whose name is *folder* and return a :class:`Maildir`
342 instance representing it.
Georg Brandl116aa622007-08-15 14:28:22 +0000343
344
Benjamin Petersone41251e2008-04-25 01:59:09 +0000345 .. method:: remove_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000346
Benjamin Petersone41251e2008-04-25 01:59:09 +0000347 Delete the folder whose name is *folder*. If the folder contains any
348 messages, a :exc:`NotEmptyError` exception will be raised and the folder
349 will not be deleted.
Georg Brandl116aa622007-08-15 14:28:22 +0000350
351
Benjamin Petersone41251e2008-04-25 01:59:09 +0000352 .. method:: clean()
Georg Brandl116aa622007-08-15 14:28:22 +0000353
Benjamin Petersone41251e2008-04-25 01:59:09 +0000354 Delete temporary files from the mailbox that have not been accessed in the
355 last 36 hours. The Maildir specification says that mail-reading programs
356 should do this occasionally.
Georg Brandl116aa622007-08-15 14:28:22 +0000357
Benjamin Petersone41251e2008-04-25 01:59:09 +0000358 Some :class:`Mailbox` methods implemented by :class:`Maildir` deserve special
359 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000360
361
Benjamin Petersone41251e2008-04-25 01:59:09 +0000362 .. method:: add(message)
363 __setitem__(key, message)
364 update(arg)
Georg Brandl116aa622007-08-15 14:28:22 +0000365
Benjamin Petersone41251e2008-04-25 01:59:09 +0000366 .. warning::
Georg Brandl116aa622007-08-15 14:28:22 +0000367
Benjamin Petersone41251e2008-04-25 01:59:09 +0000368 These methods generate unique file names based upon the current process
369 ID. When using multiple threads, undetected name clashes may occur and
370 cause corruption of the mailbox unless threads are coordinated to avoid
371 using these methods to manipulate the same mailbox simultaneously.
Georg Brandl116aa622007-08-15 14:28:22 +0000372
373
Benjamin Petersone41251e2008-04-25 01:59:09 +0000374 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000375
Benjamin Petersone41251e2008-04-25 01:59:09 +0000376 All changes to Maildir mailboxes are immediately applied, so this method
377 does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000378
379
Benjamin Petersone41251e2008-04-25 01:59:09 +0000380 .. method:: lock()
381 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000382
Benjamin Petersone41251e2008-04-25 01:59:09 +0000383 Maildir mailboxes do not support (or require) locking, so these methods do
384 nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000385
386
Benjamin Petersone41251e2008-04-25 01:59:09 +0000387 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000388
Benjamin Petersone41251e2008-04-25 01:59:09 +0000389 :class:`Maildir` instances do not keep any open files and the underlying
390 mailboxes do not support locking, so this method does nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000391
392
Benjamin Petersone41251e2008-04-25 01:59:09 +0000393 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000394
Benjamin Petersone41251e2008-04-25 01:59:09 +0000395 Depending upon the host platform, it may not be possible to modify or
396 remove the underlying message while the returned file remains open.
Georg Brandl116aa622007-08-15 14:28:22 +0000397
398
399.. seealso::
400
401 `maildir man page from qmail <http://www.qmail.org/man/man5/maildir.html>`_
402 The original specification of the format.
403
404 `Using maildir format <http://cr.yp.to/proto/maildir.html>`_
405 Notes on Maildir by its inventor. Includes an updated name-creation scheme and
406 details on "info" semantics.
407
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000408 `maildir man page from Courier <http://www.courier-mta.org/maildir.html>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000409 Another specification of the format. Describes a common extension for supporting
410 folders.
411
412
413.. _mailbox-mbox:
414
415:class:`mbox`
416^^^^^^^^^^^^^
417
418
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000419.. class:: mbox(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000420
421 A subclass of :class:`Mailbox` for mailboxes in mbox format. Parameter *factory*
422 is a callable object that accepts a file-like message representation (which
423 behaves as if opened in binary mode) and returns a custom representation. If
424 *factory* is ``None``, :class:`mboxMessage` is used as the default message
425 representation. If *create* is ``True``, the mailbox is created if it does not
426 exist.
427
Benjamin Petersone41251e2008-04-25 01:59:09 +0000428 The mbox format is the classic format for storing mail on Unix systems. All
429 messages in an mbox mailbox are stored in a single file with the beginning of
430 each message indicated by a line whose first five characters are "From ".
Georg Brandl116aa622007-08-15 14:28:22 +0000431
Benjamin Petersone41251e2008-04-25 01:59:09 +0000432 Several variations of the mbox format exist to address perceived shortcomings in
433 the original. In the interest of compatibility, :class:`mbox` implements the
434 original format, which is sometimes referred to as :dfn:`mboxo`. This means that
435 the :mailheader:`Content-Length` header, if present, is ignored and that any
436 occurrences of "From " at the beginning of a line in a message body are
437 transformed to ">From " when storing the message, although occurrences of ">From
438 " are not transformed to "From " when reading the message.
Georg Brandl116aa622007-08-15 14:28:22 +0000439
Benjamin Petersone41251e2008-04-25 01:59:09 +0000440 Some :class:`Mailbox` methods implemented by :class:`mbox` deserve special
441 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000442
443
Benjamin Petersone41251e2008-04-25 01:59:09 +0000444 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000445
Benjamin Petersone41251e2008-04-25 01:59:09 +0000446 Using the file after calling :meth:`flush` or :meth:`close` on the
447 :class:`mbox` instance may yield unpredictable results or raise an
448 exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000449
450
Benjamin Petersone41251e2008-04-25 01:59:09 +0000451 .. method:: lock()
452 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000453
Benjamin Petersone41251e2008-04-25 01:59:09 +0000454 Three locking mechanisms are used---dot locking and, if available, the
455 :cfunc:`flock` and :cfunc:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000456
457
458.. seealso::
459
460 `mbox man page from qmail <http://www.qmail.org/man/man5/mbox.html>`_
461 A specification of the format and its variations.
462
463 `mbox man page from tin <http://www.tin.org/bin/man.cgi?section=5&topic=mbox>`_
464 Another specification of the format, with details on locking.
465
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000466 `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 +0000467 An argument for using the original mbox format rather than a variation.
468
469 `"mbox" is a family of several mutually incompatible mailbox formats <http://homepages.tesco.net./~J.deBoynePollard/FGA/mail-mbox-formats.html>`_
470 A history of mbox variations.
471
472
473.. _mailbox-mh:
474
475:class:`MH`
476^^^^^^^^^^^
477
478
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000479.. class:: MH(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000480
481 A subclass of :class:`Mailbox` for mailboxes in MH format. Parameter *factory*
482 is a callable object that accepts a file-like message representation (which
483 behaves as if opened in binary mode) and returns a custom representation. If
484 *factory* is ``None``, :class:`MHMessage` is used as the default message
485 representation. If *create* is ``True``, the mailbox is created if it does not
486 exist.
487
Benjamin Petersone41251e2008-04-25 01:59:09 +0000488 MH is a directory-based mailbox format invented for the MH Message Handling
489 System, a mail user agent. Each message in an MH mailbox resides in its own
490 file. An MH mailbox may contain other MH mailboxes (called :dfn:`folders`) in
491 addition to messages. Folders may be nested indefinitely. MH mailboxes also
492 support :dfn:`sequences`, which are named lists used to logically group
493 messages without moving them to sub-folders. Sequences are defined in a file
494 called :file:`.mh_sequences` in each folder.
Georg Brandl116aa622007-08-15 14:28:22 +0000495
Benjamin Petersone41251e2008-04-25 01:59:09 +0000496 The :class:`MH` class manipulates MH mailboxes, but it does not attempt to
497 emulate all of :program:`mh`'s behaviors. In particular, it does not modify
498 and is not affected by the :file:`context` or :file:`.mh_profile` files that
499 are used by :program:`mh` to store its state and configuration.
Georg Brandl116aa622007-08-15 14:28:22 +0000500
Benjamin Petersone41251e2008-04-25 01:59:09 +0000501 :class:`MH` instances have all of the methods of :class:`Mailbox` in addition
502 to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000503
504
Benjamin Petersone41251e2008-04-25 01:59:09 +0000505 .. method:: list_folders()
Georg Brandl116aa622007-08-15 14:28:22 +0000506
Benjamin Petersone41251e2008-04-25 01:59:09 +0000507 Return a list of the names of all folders.
Georg Brandl116aa622007-08-15 14:28:22 +0000508
509
Benjamin Petersone41251e2008-04-25 01:59:09 +0000510 .. method:: get_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000511
Benjamin Petersone41251e2008-04-25 01:59:09 +0000512 Return an :class:`MH` instance representing the folder whose name is
513 *folder*. A :exc:`NoSuchMailboxError` exception is raised if the folder
514 does not exist.
Georg Brandl116aa622007-08-15 14:28:22 +0000515
516
Benjamin Petersone41251e2008-04-25 01:59:09 +0000517 .. method:: add_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000518
Benjamin Petersone41251e2008-04-25 01:59:09 +0000519 Create a folder whose name is *folder* and return an :class:`MH` instance
520 representing it.
Georg Brandl116aa622007-08-15 14:28:22 +0000521
522
Benjamin Petersone41251e2008-04-25 01:59:09 +0000523 .. method:: remove_folder(folder)
Georg Brandl116aa622007-08-15 14:28:22 +0000524
Benjamin Petersone41251e2008-04-25 01:59:09 +0000525 Delete the folder whose name is *folder*. If the folder contains any
526 messages, a :exc:`NotEmptyError` exception will be raised and the folder
527 will not be deleted.
Georg Brandl116aa622007-08-15 14:28:22 +0000528
529
Benjamin Petersone41251e2008-04-25 01:59:09 +0000530 .. method:: get_sequences()
Georg Brandl116aa622007-08-15 14:28:22 +0000531
Benjamin Petersone41251e2008-04-25 01:59:09 +0000532 Return a dictionary of sequence names mapped to key lists. If there are no
533 sequences, the empty dictionary is returned.
Georg Brandl116aa622007-08-15 14:28:22 +0000534
535
Benjamin Petersone41251e2008-04-25 01:59:09 +0000536 .. method:: set_sequences(sequences)
Georg Brandl116aa622007-08-15 14:28:22 +0000537
Benjamin Petersone41251e2008-04-25 01:59:09 +0000538 Re-define the sequences that exist in the mailbox based upon *sequences*,
539 a dictionary of names mapped to key lists, like returned by
540 :meth:`get_sequences`.
Georg Brandl116aa622007-08-15 14:28:22 +0000541
542
Benjamin Petersone41251e2008-04-25 01:59:09 +0000543 .. method:: pack()
Georg Brandl116aa622007-08-15 14:28:22 +0000544
Benjamin Petersone41251e2008-04-25 01:59:09 +0000545 Rename messages in the mailbox as necessary to eliminate gaps in
546 numbering. Entries in the sequences list are updated correspondingly.
Georg Brandl116aa622007-08-15 14:28:22 +0000547
Benjamin Petersone41251e2008-04-25 01:59:09 +0000548 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000549
Benjamin Petersone41251e2008-04-25 01:59:09 +0000550 Already-issued keys are invalidated by this operation and should not be
551 subsequently used.
Georg Brandl116aa622007-08-15 14:28:22 +0000552
Benjamin Petersone41251e2008-04-25 01:59:09 +0000553 Some :class:`Mailbox` methods implemented by :class:`MH` deserve special
554 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000555
556
Benjamin Petersone41251e2008-04-25 01:59:09 +0000557 .. method:: remove(key)
558 __delitem__(key)
559 discard(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000560
Benjamin Petersone41251e2008-04-25 01:59:09 +0000561 These methods immediately delete the message. The MH convention of marking
562 a message for deletion by prepending a comma to its name is not used.
Georg Brandl116aa622007-08-15 14:28:22 +0000563
564
Benjamin Petersone41251e2008-04-25 01:59:09 +0000565 .. method:: lock()
566 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000567
Benjamin Petersone41251e2008-04-25 01:59:09 +0000568 Three locking mechanisms are used---dot locking and, if available, the
569 :cfunc:`flock` and :cfunc:`lockf` system calls. For MH mailboxes, locking
570 the mailbox means locking the :file:`.mh_sequences` file and, only for the
571 duration of any operations that affect them, locking individual message
572 files.
Georg Brandl116aa622007-08-15 14:28:22 +0000573
574
Benjamin Petersone41251e2008-04-25 01:59:09 +0000575 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000576
Benjamin Petersone41251e2008-04-25 01:59:09 +0000577 Depending upon the host platform, it may not be possible to remove the
578 underlying message while the returned file remains open.
Georg Brandl116aa622007-08-15 14:28:22 +0000579
580
Benjamin Petersone41251e2008-04-25 01:59:09 +0000581 .. method:: flush()
Georg Brandl116aa622007-08-15 14:28:22 +0000582
Benjamin Petersone41251e2008-04-25 01:59:09 +0000583 All changes to MH mailboxes are immediately applied, so this method does
584 nothing.
Georg Brandl116aa622007-08-15 14:28:22 +0000585
586
Benjamin Petersone41251e2008-04-25 01:59:09 +0000587 .. method:: close()
Georg Brandl116aa622007-08-15 14:28:22 +0000588
Benjamin Petersone41251e2008-04-25 01:59:09 +0000589 :class:`MH` instances do not keep any open files, so this method is
590 equivalent to :meth:`unlock`.
Georg Brandl116aa622007-08-15 14:28:22 +0000591
592
593.. seealso::
594
595 `nmh - Message Handling System <http://www.nongnu.org/nmh/>`_
596 Home page of :program:`nmh`, an updated version of the original :program:`mh`.
597
Georg Brandl628e6f92009-10-27 20:24:45 +0000598 `MH & nmh: Email for Users & Programmers <http://rand-mh.sourceforge.net/book/>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000599 A GPL-licensed book on :program:`mh` and :program:`nmh`, with some information
600 on the mailbox format.
601
602
603.. _mailbox-babyl:
604
605:class:`Babyl`
606^^^^^^^^^^^^^^
607
608
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000609.. class:: Babyl(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000610
611 A subclass of :class:`Mailbox` for mailboxes in Babyl format. Parameter
612 *factory* is a callable object that accepts a file-like message representation
613 (which behaves as if opened in binary mode) and returns a custom representation.
614 If *factory* is ``None``, :class:`BabylMessage` is used as the default message
615 representation. If *create* is ``True``, the mailbox is created if it does not
616 exist.
617
Benjamin Petersone41251e2008-04-25 01:59:09 +0000618 Babyl is a single-file mailbox format used by the Rmail mail user agent
619 included with Emacs. The beginning of a message is indicated by a line
620 containing the two characters Control-Underscore (``'\037'``) and Control-L
621 (``'\014'``). The end of a message is indicated by the start of the next
622 message or, in the case of the last message, a line containing a
623 Control-Underscore (``'\037'``) character.
Georg Brandl116aa622007-08-15 14:28:22 +0000624
Benjamin Petersone41251e2008-04-25 01:59:09 +0000625 Messages in a Babyl mailbox have two sets of headers, original headers and
626 so-called visible headers. Visible headers are typically a subset of the
627 original headers that have been reformatted or abridged to be more
628 attractive. Each message in a Babyl mailbox also has an accompanying list of
629 :dfn:`labels`, or short strings that record extra information about the
630 message, and a list of all user-defined labels found in the mailbox is kept
631 in the Babyl options section.
Georg Brandl116aa622007-08-15 14:28:22 +0000632
Benjamin Petersone41251e2008-04-25 01:59:09 +0000633 :class:`Babyl` instances have all of the methods of :class:`Mailbox` in
634 addition to the following:
Georg Brandl116aa622007-08-15 14:28:22 +0000635
636
Benjamin Petersone41251e2008-04-25 01:59:09 +0000637 .. method:: get_labels()
Georg Brandl116aa622007-08-15 14:28:22 +0000638
Benjamin Petersone41251e2008-04-25 01:59:09 +0000639 Return a list of the names of all user-defined labels used in the mailbox.
Georg Brandl116aa622007-08-15 14:28:22 +0000640
Benjamin Petersone41251e2008-04-25 01:59:09 +0000641 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000642
Benjamin Petersone41251e2008-04-25 01:59:09 +0000643 The actual messages are inspected to determine which labels exist in
644 the mailbox rather than consulting the list of labels in the Babyl
645 options section, but the Babyl section is updated whenever the mailbox
646 is modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000647
Benjamin Petersone41251e2008-04-25 01:59:09 +0000648 Some :class:`Mailbox` methods implemented by :class:`Babyl` deserve special
649 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000650
651
Benjamin Petersone41251e2008-04-25 01:59:09 +0000652 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000653
Benjamin Petersone41251e2008-04-25 01:59:09 +0000654 In Babyl mailboxes, the headers of a message are not stored contiguously
655 with the body of the message. To generate a file-like representation, the
656 headers and body are copied together into a :class:`StringIO` instance
657 (from the :mod:`StringIO` module), which has an API identical to that of a
658 file. As a result, the file-like object is truly independent of the
659 underlying mailbox but does not save memory compared to a string
660 representation.
Georg Brandl116aa622007-08-15 14:28:22 +0000661
662
Benjamin Petersone41251e2008-04-25 01:59:09 +0000663 .. method:: lock()
664 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000665
Benjamin Petersone41251e2008-04-25 01:59:09 +0000666 Three locking mechanisms are used---dot locking and, if available, the
667 :cfunc:`flock` and :cfunc:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000668
669
670.. seealso::
671
672 `Format of Version 5 Babyl Files <http://quimby.gnus.org/notes/BABYL>`_
673 A specification of the Babyl format.
674
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000675 `Reading Mail with Rmail <http://www.gnu.org/software/emacs/manual/html_node/emacs/Rmail.html>`_
Georg Brandl116aa622007-08-15 14:28:22 +0000676 The Rmail manual, with some information on Babyl semantics.
677
678
679.. _mailbox-mmdf:
680
681:class:`MMDF`
682^^^^^^^^^^^^^
683
684
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000685.. class:: MMDF(path, factory=None, create=True)
Georg Brandl116aa622007-08-15 14:28:22 +0000686
687 A subclass of :class:`Mailbox` for mailboxes in MMDF format. Parameter *factory*
688 is a callable object that accepts a file-like message representation (which
689 behaves as if opened in binary mode) and returns a custom representation. If
690 *factory* is ``None``, :class:`MMDFMessage` is used as the default message
691 representation. If *create* is ``True``, the mailbox is created if it does not
692 exist.
693
Benjamin Petersone41251e2008-04-25 01:59:09 +0000694 MMDF is a single-file mailbox format invented for the Multichannel Memorandum
695 Distribution Facility, a mail transfer agent. Each message is in the same
696 form as an mbox message but is bracketed before and after by lines containing
697 four Control-A (``'\001'``) characters. As with the mbox format, the
698 beginning of each message is indicated by a line whose first five characters
699 are "From ", but additional occurrences of "From " are not transformed to
700 ">From " when storing messages because the extra message separator lines
701 prevent mistaking such occurrences for the starts of subsequent messages.
Georg Brandl116aa622007-08-15 14:28:22 +0000702
Benjamin Petersone41251e2008-04-25 01:59:09 +0000703 Some :class:`Mailbox` methods implemented by :class:`MMDF` deserve special
704 remarks:
Georg Brandl116aa622007-08-15 14:28:22 +0000705
706
Benjamin Petersone41251e2008-04-25 01:59:09 +0000707 .. method:: get_file(key)
Georg Brandl116aa622007-08-15 14:28:22 +0000708
Benjamin Petersone41251e2008-04-25 01:59:09 +0000709 Using the file after calling :meth:`flush` or :meth:`close` on the
710 :class:`MMDF` instance may yield unpredictable results or raise an
711 exception.
Georg Brandl116aa622007-08-15 14:28:22 +0000712
713
Benjamin Petersone41251e2008-04-25 01:59:09 +0000714 .. method:: lock()
715 unlock()
Georg Brandl116aa622007-08-15 14:28:22 +0000716
Benjamin Petersone41251e2008-04-25 01:59:09 +0000717 Three locking mechanisms are used---dot locking and, if available, the
718 :cfunc:`flock` and :cfunc:`lockf` system calls.
Georg Brandl116aa622007-08-15 14:28:22 +0000719
720
721.. seealso::
722
723 `mmdf man page from tin <http://www.tin.org/bin/man.cgi?section=5&topic=mmdf>`_
724 A specification of MMDF format from the documentation of tin, a newsreader.
725
726 `MMDF <http://en.wikipedia.org/wiki/MMDF>`_
727 A Wikipedia article describing the Multichannel Memorandum Distribution
728 Facility.
729
730
731.. _mailbox-message-objects:
732
733:class:`Message` objects
734------------------------
735
736
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000737.. class:: Message(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000738
739 A subclass of the :mod:`email.Message` module's :class:`Message`. Subclasses of
740 :class:`mailbox.Message` add mailbox-format-specific state and behavior.
741
742 If *message* is omitted, the new instance is created in a default, empty state.
743 If *message* is an :class:`email.Message.Message` instance, its contents are
744 copied; furthermore, any format-specific information is converted insofar as
745 possible if *message* is a :class:`Message` instance. If *message* is a string
746 or a file, it should contain an :rfc:`2822`\ -compliant message, which is read
747 and parsed.
748
Benjamin Petersone41251e2008-04-25 01:59:09 +0000749 The format-specific state and behaviors offered by subclasses vary, but in
750 general it is only the properties that are not specific to a particular
751 mailbox that are supported (although presumably the properties are specific
752 to a particular mailbox format). For example, file offsets for single-file
753 mailbox formats and file names for directory-based mailbox formats are not
754 retained, because they are only applicable to the original mailbox. But state
755 such as whether a message has been read by the user or marked as important is
756 retained, because it applies to the message itself.
Georg Brandl116aa622007-08-15 14:28:22 +0000757
Benjamin Petersone41251e2008-04-25 01:59:09 +0000758 There is no requirement that :class:`Message` instances be used to represent
759 messages retrieved using :class:`Mailbox` instances. In some situations, the
760 time and memory required to generate :class:`Message` representations might
761 not not acceptable. For such situations, :class:`Mailbox` instances also
762 offer string and file-like representations, and a custom message factory may
763 be specified when a :class:`Mailbox` instance is initialized.
Georg Brandl116aa622007-08-15 14:28:22 +0000764
765
766.. _mailbox-maildirmessage:
767
768:class:`MaildirMessage`
769^^^^^^^^^^^^^^^^^^^^^^^
770
771
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000772.. class:: MaildirMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000773
774 A message with Maildir-specific behaviors. Parameter *message* has the same
775 meaning as with the :class:`Message` constructor.
776
Benjamin Petersone41251e2008-04-25 01:59:09 +0000777 Typically, a mail user agent application moves all of the messages in the
778 :file:`new` subdirectory to the :file:`cur` subdirectory after the first time
779 the user opens and closes the mailbox, recording that the messages are old
780 whether or not they've actually been read. Each message in :file:`cur` has an
781 "info" section added to its file name to store information about its state.
782 (Some mail readers may also add an "info" section to messages in
783 :file:`new`.) The "info" section may take one of two forms: it may contain
784 "2," followed by a list of standardized flags (e.g., "2,FR") or it may
785 contain "1," followed by so-called experimental information. Standard flags
786 for Maildir messages are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000787
Benjamin Petersone41251e2008-04-25 01:59:09 +0000788 +------+---------+--------------------------------+
789 | Flag | Meaning | Explanation |
790 +======+=========+================================+
791 | D | Draft | Under composition |
792 +------+---------+--------------------------------+
793 | F | Flagged | Marked as important |
794 +------+---------+--------------------------------+
795 | P | Passed | Forwarded, resent, or bounced |
796 +------+---------+--------------------------------+
797 | R | Replied | Replied to |
798 +------+---------+--------------------------------+
799 | S | Seen | Read |
800 +------+---------+--------------------------------+
801 | T | Trashed | Marked for subsequent deletion |
802 +------+---------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000803
Benjamin Petersone41251e2008-04-25 01:59:09 +0000804 :class:`MaildirMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000805
806
Benjamin Petersone41251e2008-04-25 01:59:09 +0000807 .. method:: get_subdir()
Georg Brandl116aa622007-08-15 14:28:22 +0000808
Benjamin Petersone41251e2008-04-25 01:59:09 +0000809 Return either "new" (if the message should be stored in the :file:`new`
810 subdirectory) or "cur" (if the message should be stored in the :file:`cur`
811 subdirectory).
Georg Brandl116aa622007-08-15 14:28:22 +0000812
Benjamin Petersone41251e2008-04-25 01:59:09 +0000813 .. note::
Georg Brandl116aa622007-08-15 14:28:22 +0000814
Benjamin Petersone41251e2008-04-25 01:59:09 +0000815 A message is typically moved from :file:`new` to :file:`cur` after its
816 mailbox has been accessed, whether or not the message is has been
817 read. A message ``msg`` has been read if ``"S" in msg.get_flags()`` is
818 ``True``.
Georg Brandl116aa622007-08-15 14:28:22 +0000819
820
Benjamin Petersone41251e2008-04-25 01:59:09 +0000821 .. method:: set_subdir(subdir)
Georg Brandl116aa622007-08-15 14:28:22 +0000822
Benjamin Petersone41251e2008-04-25 01:59:09 +0000823 Set the subdirectory the message should be stored in. Parameter *subdir*
824 must be either "new" or "cur".
Georg Brandl116aa622007-08-15 14:28:22 +0000825
826
Benjamin Petersone41251e2008-04-25 01:59:09 +0000827 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +0000828
Benjamin Petersone41251e2008-04-25 01:59:09 +0000829 Return a string specifying the flags that are currently set. If the
830 message complies with the standard Maildir format, the result is the
831 concatenation in alphabetical order of zero or one occurrence of each of
832 ``'D'``, ``'F'``, ``'P'``, ``'R'``, ``'S'``, and ``'T'``. The empty string
833 is returned if no flags are set or if "info" contains experimental
834 semantics.
Georg Brandl116aa622007-08-15 14:28:22 +0000835
836
Benjamin Petersone41251e2008-04-25 01:59:09 +0000837 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +0000838
Benjamin Petersone41251e2008-04-25 01:59:09 +0000839 Set the flags specified by *flags* and unset all others.
Georg Brandl116aa622007-08-15 14:28:22 +0000840
841
Benjamin Petersone41251e2008-04-25 01:59:09 +0000842 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000843
Benjamin Petersone41251e2008-04-25 01:59:09 +0000844 Set the flag(s) specified by *flag* without changing other flags. To add
845 more than one flag at a time, *flag* may be a string of more than one
846 character. The current "info" is overwritten whether or not it contains
847 experimental information rather than flags.
Georg Brandl116aa622007-08-15 14:28:22 +0000848
849
Benjamin Petersone41251e2008-04-25 01:59:09 +0000850 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +0000851
Benjamin Petersone41251e2008-04-25 01:59:09 +0000852 Unset the flag(s) specified by *flag* without changing other flags. To
853 remove more than one flag at a time, *flag* maybe a string of more than
854 one character. If "info" contains experimental information rather than
855 flags, the current "info" is not modified.
Georg Brandl116aa622007-08-15 14:28:22 +0000856
857
Benjamin Petersone41251e2008-04-25 01:59:09 +0000858 .. method:: get_date()
Georg Brandl116aa622007-08-15 14:28:22 +0000859
Benjamin Petersone41251e2008-04-25 01:59:09 +0000860 Return the delivery date of the message as a floating-point number
861 representing seconds since the epoch.
Georg Brandl116aa622007-08-15 14:28:22 +0000862
863
Benjamin Petersone41251e2008-04-25 01:59:09 +0000864 .. method:: set_date(date)
Georg Brandl116aa622007-08-15 14:28:22 +0000865
Benjamin Petersone41251e2008-04-25 01:59:09 +0000866 Set the delivery date of the message to *date*, 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:: get_info()
Georg Brandl116aa622007-08-15 14:28:22 +0000871
Benjamin Petersone41251e2008-04-25 01:59:09 +0000872 Return a string containing the "info" for a message. This is useful for
873 accessing and modifying "info" that is experimental (i.e., not a list of
874 flags).
Georg Brandl116aa622007-08-15 14:28:22 +0000875
876
Benjamin Petersone41251e2008-04-25 01:59:09 +0000877 .. method:: set_info(info)
Georg Brandl116aa622007-08-15 14:28:22 +0000878
Benjamin Petersone41251e2008-04-25 01:59:09 +0000879 Set "info" to *info*, which should be a string.
Georg Brandl116aa622007-08-15 14:28:22 +0000880
881When a :class:`MaildirMessage` instance is created based upon an
882:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
883and :mailheader:`X-Status` headers are omitted and the following conversions
884take place:
885
886+--------------------+----------------------------------------------+
887| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
888| | state |
889+====================+==============================================+
890| "cur" subdirectory | O flag |
891+--------------------+----------------------------------------------+
892| F flag | F flag |
893+--------------------+----------------------------------------------+
894| R flag | A flag |
895+--------------------+----------------------------------------------+
896| S flag | R flag |
897+--------------------+----------------------------------------------+
898| T flag | D flag |
899+--------------------+----------------------------------------------+
900
901When a :class:`MaildirMessage` instance is created based upon an
902:class:`MHMessage` instance, the following conversions take place:
903
904+-------------------------------+--------------------------+
905| Resulting state | :class:`MHMessage` state |
906+===============================+==========================+
907| "cur" subdirectory | "unseen" sequence |
908+-------------------------------+--------------------------+
909| "cur" subdirectory and S flag | no "unseen" sequence |
910+-------------------------------+--------------------------+
911| F flag | "flagged" sequence |
912+-------------------------------+--------------------------+
913| R flag | "replied" sequence |
914+-------------------------------+--------------------------+
915
916When a :class:`MaildirMessage` instance is created based upon a
917:class:`BabylMessage` instance, the following conversions take place:
918
919+-------------------------------+-------------------------------+
920| Resulting state | :class:`BabylMessage` state |
921+===============================+===============================+
922| "cur" subdirectory | "unseen" label |
923+-------------------------------+-------------------------------+
924| "cur" subdirectory and S flag | no "unseen" label |
925+-------------------------------+-------------------------------+
926| P flag | "forwarded" or "resent" label |
927+-------------------------------+-------------------------------+
928| R flag | "answered" label |
929+-------------------------------+-------------------------------+
930| T flag | "deleted" label |
931+-------------------------------+-------------------------------+
932
933
934.. _mailbox-mboxmessage:
935
936:class:`mboxMessage`
937^^^^^^^^^^^^^^^^^^^^
938
939
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000940.. class:: mboxMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000941
942 A message with mbox-specific behaviors. Parameter *message* has the same meaning
943 as with the :class:`Message` constructor.
944
Benjamin Petersone41251e2008-04-25 01:59:09 +0000945 Messages in an mbox mailbox are stored together in a single file. The
946 sender's envelope address and the time of delivery are typically stored in a
947 line beginning with "From " that is used to indicate the start of a message,
948 though there is considerable variation in the exact format of this data among
949 mbox implementations. Flags that indicate the state of the message, such as
950 whether it has been read or marked as important, are typically stored in
951 :mailheader:`Status` and :mailheader:`X-Status` headers.
Georg Brandl116aa622007-08-15 14:28:22 +0000952
Benjamin Petersone41251e2008-04-25 01:59:09 +0000953 Conventional flags for mbox messages are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +0000954
Benjamin Petersone41251e2008-04-25 01:59:09 +0000955 +------+----------+--------------------------------+
956 | Flag | Meaning | Explanation |
957 +======+==========+================================+
958 | R | Read | Read |
959 +------+----------+--------------------------------+
960 | O | Old | Previously detected by MUA |
961 +------+----------+--------------------------------+
962 | D | Deleted | Marked for subsequent deletion |
963 +------+----------+--------------------------------+
964 | F | Flagged | Marked as important |
965 +------+----------+--------------------------------+
966 | A | Answered | Replied to |
967 +------+----------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000968
Benjamin Petersone41251e2008-04-25 01:59:09 +0000969 The "R" and "O" flags are stored in the :mailheader:`Status` header, and the
970 "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The
971 flags and headers typically appear in the order mentioned.
Georg Brandl116aa622007-08-15 14:28:22 +0000972
Benjamin Petersone41251e2008-04-25 01:59:09 +0000973 :class:`mboxMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000974
975
Benjamin Petersone41251e2008-04-25 01:59:09 +0000976 .. method:: get_from()
Georg Brandl116aa622007-08-15 14:28:22 +0000977
Benjamin Petersone41251e2008-04-25 01:59:09 +0000978 Return a string representing the "From " line that marks the start of the
979 message in an mbox mailbox. The leading "From " and the trailing newline
980 are excluded.
Georg Brandl116aa622007-08-15 14:28:22 +0000981
982
Georg Brandlcd7f32b2009-06-08 09:13:45 +0000983 .. method:: set_from(from_, time_=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000984
Benjamin Petersone41251e2008-04-25 01:59:09 +0000985 Set the "From " line to *from_*, which should be specified without a
986 leading "From " or trailing newline. For convenience, *time_* may be
987 specified and will be formatted appropriately and appended to *from_*. If
988 *time_* is specified, it should be a :class:`struct_time` instance, a
989 tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use
990 :meth:`time.gmtime`).
Georg Brandl116aa622007-08-15 14:28:22 +0000991
992
Benjamin Petersone41251e2008-04-25 01:59:09 +0000993 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +0000994
Benjamin Petersone41251e2008-04-25 01:59:09 +0000995 Return a string specifying the flags that are currently set. If the
996 message complies with the conventional format, the result is the
997 concatenation in the following order of zero or one occurrence of each of
998 ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +0000999
1000
Benjamin Petersone41251e2008-04-25 01:59:09 +00001001 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +00001002
Benjamin Petersone41251e2008-04-25 01:59:09 +00001003 Set the flags specified by *flags* and unset all others. Parameter *flags*
1004 should be the concatenation in any order of zero or more occurrences of
1005 each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001006
1007
Benjamin Petersone41251e2008-04-25 01:59:09 +00001008 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001009
Benjamin Petersone41251e2008-04-25 01:59:09 +00001010 Set the flag(s) specified by *flag* without changing other flags. To add
1011 more than one flag at a time, *flag* may be a string of more than one
1012 character.
Georg Brandl116aa622007-08-15 14:28:22 +00001013
1014
Benjamin Petersone41251e2008-04-25 01:59:09 +00001015 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001016
Benjamin Petersone41251e2008-04-25 01:59:09 +00001017 Unset the flag(s) specified by *flag* without changing other flags. To
1018 remove more than one flag at a time, *flag* maybe a string of more than
1019 one character.
Georg Brandl116aa622007-08-15 14:28:22 +00001020
1021When an :class:`mboxMessage` instance is created based upon a
1022:class:`MaildirMessage` instance, a "From " line is generated based upon the
1023:class:`MaildirMessage` instance's delivery date, and the following conversions
1024take place:
1025
1026+-----------------+-------------------------------+
1027| Resulting state | :class:`MaildirMessage` state |
1028+=================+===============================+
1029| R flag | S flag |
1030+-----------------+-------------------------------+
1031| O flag | "cur" subdirectory |
1032+-----------------+-------------------------------+
1033| D flag | T flag |
1034+-----------------+-------------------------------+
1035| F flag | F flag |
1036+-----------------+-------------------------------+
1037| A flag | R flag |
1038+-----------------+-------------------------------+
1039
1040When an :class:`mboxMessage` instance is created based upon an
1041:class:`MHMessage` instance, the following conversions take place:
1042
1043+-------------------+--------------------------+
1044| Resulting state | :class:`MHMessage` state |
1045+===================+==========================+
1046| R flag and O flag | no "unseen" sequence |
1047+-------------------+--------------------------+
1048| O flag | "unseen" sequence |
1049+-------------------+--------------------------+
1050| F flag | "flagged" sequence |
1051+-------------------+--------------------------+
1052| A flag | "replied" sequence |
1053+-------------------+--------------------------+
1054
1055When an :class:`mboxMessage` instance is created based upon a
1056:class:`BabylMessage` instance, the following conversions take place:
1057
1058+-------------------+-----------------------------+
1059| Resulting state | :class:`BabylMessage` state |
1060+===================+=============================+
1061| R flag and O flag | no "unseen" label |
1062+-------------------+-----------------------------+
1063| O flag | "unseen" label |
1064+-------------------+-----------------------------+
1065| D flag | "deleted" label |
1066+-------------------+-----------------------------+
1067| A flag | "answered" label |
1068+-------------------+-----------------------------+
1069
1070When a :class:`Message` instance is created based upon an :class:`MMDFMessage`
1071instance, the "From " line is copied and all flags directly correspond:
1072
1073+-----------------+----------------------------+
1074| Resulting state | :class:`MMDFMessage` state |
1075+=================+============================+
1076| R flag | R flag |
1077+-----------------+----------------------------+
1078| O flag | O flag |
1079+-----------------+----------------------------+
1080| D flag | D flag |
1081+-----------------+----------------------------+
1082| F flag | F flag |
1083+-----------------+----------------------------+
1084| A flag | A flag |
1085+-----------------+----------------------------+
1086
1087
1088.. _mailbox-mhmessage:
1089
1090:class:`MHMessage`
1091^^^^^^^^^^^^^^^^^^
1092
1093
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001094.. class:: MHMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001095
1096 A message with MH-specific behaviors. Parameter *message* has the same meaning
1097 as with the :class:`Message` constructor.
1098
Benjamin Petersone41251e2008-04-25 01:59:09 +00001099 MH messages do not support marks or flags in the traditional sense, but they
1100 do support sequences, which are logical groupings of arbitrary messages. Some
1101 mail reading programs (although not the standard :program:`mh` and
1102 :program:`nmh`) use sequences in much the same way flags are used with other
1103 formats, as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001104
Benjamin Petersone41251e2008-04-25 01:59:09 +00001105 +----------+------------------------------------------+
1106 | Sequence | Explanation |
1107 +==========+==========================================+
1108 | unseen | Not read, but previously detected by MUA |
1109 +----------+------------------------------------------+
1110 | replied | Replied to |
1111 +----------+------------------------------------------+
1112 | flagged | Marked as important |
1113 +----------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001114
Benjamin Petersone41251e2008-04-25 01:59:09 +00001115 :class:`MHMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001116
1117
Benjamin Petersone41251e2008-04-25 01:59:09 +00001118 .. method:: get_sequences()
Georg Brandl116aa622007-08-15 14:28:22 +00001119
Benjamin Petersone41251e2008-04-25 01:59:09 +00001120 Return a list of the names of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001121
1122
Benjamin Petersone41251e2008-04-25 01:59:09 +00001123 .. method:: set_sequences(sequences)
Georg Brandl116aa622007-08-15 14:28:22 +00001124
Benjamin Petersone41251e2008-04-25 01:59:09 +00001125 Set the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001126
1127
Benjamin Petersone41251e2008-04-25 01:59:09 +00001128 .. method:: add_sequence(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +00001129
Benjamin Petersone41251e2008-04-25 01:59:09 +00001130 Add *sequence* to the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001131
1132
Benjamin Petersone41251e2008-04-25 01:59:09 +00001133 .. method:: remove_sequence(sequence)
Georg Brandl116aa622007-08-15 14:28:22 +00001134
Benjamin Petersone41251e2008-04-25 01:59:09 +00001135 Remove *sequence* from the list of sequences that include this message.
Georg Brandl116aa622007-08-15 14:28:22 +00001136
1137When an :class:`MHMessage` instance is created based upon a
1138:class:`MaildirMessage` instance, the following conversions take place:
1139
1140+--------------------+-------------------------------+
1141| Resulting state | :class:`MaildirMessage` state |
1142+====================+===============================+
1143| "unseen" sequence | no S flag |
1144+--------------------+-------------------------------+
1145| "replied" sequence | R flag |
1146+--------------------+-------------------------------+
1147| "flagged" sequence | F flag |
1148+--------------------+-------------------------------+
1149
1150When an :class:`MHMessage` instance is created based upon an
1151:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
1152and :mailheader:`X-Status` headers are omitted and the following conversions
1153take place:
1154
1155+--------------------+----------------------------------------------+
1156| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
1157| | state |
1158+====================+==============================================+
1159| "unseen" sequence | no R flag |
1160+--------------------+----------------------------------------------+
1161| "replied" sequence | A flag |
1162+--------------------+----------------------------------------------+
1163| "flagged" sequence | F flag |
1164+--------------------+----------------------------------------------+
1165
1166When an :class:`MHMessage` instance is created based upon a
1167:class:`BabylMessage` instance, the following conversions take place:
1168
1169+--------------------+-----------------------------+
1170| Resulting state | :class:`BabylMessage` state |
1171+====================+=============================+
1172| "unseen" sequence | "unseen" label |
1173+--------------------+-----------------------------+
1174| "replied" sequence | "answered" label |
1175+--------------------+-----------------------------+
1176
1177
1178.. _mailbox-babylmessage:
1179
1180:class:`BabylMessage`
1181^^^^^^^^^^^^^^^^^^^^^
1182
1183
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001184.. class:: BabylMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001185
1186 A message with Babyl-specific behaviors. Parameter *message* has the same
1187 meaning as with the :class:`Message` constructor.
1188
Benjamin Petersone41251e2008-04-25 01:59:09 +00001189 Certain message labels, called :dfn:`attributes`, are defined by convention
1190 to have special meanings. The attributes are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001191
Benjamin Petersone41251e2008-04-25 01:59:09 +00001192 +-----------+------------------------------------------+
1193 | Label | Explanation |
1194 +===========+==========================================+
1195 | unseen | Not read, but previously detected by MUA |
1196 +-----------+------------------------------------------+
1197 | deleted | Marked for subsequent deletion |
1198 +-----------+------------------------------------------+
1199 | filed | Copied to another file or mailbox |
1200 +-----------+------------------------------------------+
1201 | answered | Replied to |
1202 +-----------+------------------------------------------+
1203 | forwarded | Forwarded |
1204 +-----------+------------------------------------------+
1205 | edited | Modified by the user |
1206 +-----------+------------------------------------------+
1207 | resent | Resent |
1208 +-----------+------------------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001209
Benjamin Petersone41251e2008-04-25 01:59:09 +00001210 By default, Rmail displays only visible headers. The :class:`BabylMessage`
1211 class, though, uses the original headers because they are more
1212 complete. Visible headers may be accessed explicitly if desired.
Georg Brandl116aa622007-08-15 14:28:22 +00001213
Benjamin Petersone41251e2008-04-25 01:59:09 +00001214 :class:`BabylMessage` instances offer the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +00001215
1216
Benjamin Petersone41251e2008-04-25 01:59:09 +00001217 .. method:: get_labels()
Georg Brandl116aa622007-08-15 14:28:22 +00001218
Benjamin Petersone41251e2008-04-25 01:59:09 +00001219 Return a list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001220
1221
Benjamin Petersone41251e2008-04-25 01:59:09 +00001222 .. method:: set_labels(labels)
Georg Brandl116aa622007-08-15 14:28:22 +00001223
Benjamin Petersone41251e2008-04-25 01:59:09 +00001224 Set the list of labels on the message to *labels*.
Georg Brandl116aa622007-08-15 14:28:22 +00001225
1226
Benjamin Petersone41251e2008-04-25 01:59:09 +00001227 .. method:: add_label(label)
Georg Brandl116aa622007-08-15 14:28:22 +00001228
Benjamin Petersone41251e2008-04-25 01:59:09 +00001229 Add *label* to the list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001230
1231
Benjamin Petersone41251e2008-04-25 01:59:09 +00001232 .. method:: remove_label(label)
Georg Brandl116aa622007-08-15 14:28:22 +00001233
Benjamin Petersone41251e2008-04-25 01:59:09 +00001234 Remove *label* from the list of labels on the message.
Georg Brandl116aa622007-08-15 14:28:22 +00001235
1236
Benjamin Petersone41251e2008-04-25 01:59:09 +00001237 .. method:: get_visible()
Georg Brandl116aa622007-08-15 14:28:22 +00001238
Benjamin Petersone41251e2008-04-25 01:59:09 +00001239 Return an :class:`Message` instance whose headers are the message's
1240 visible headers and whose body is empty.
Georg Brandl116aa622007-08-15 14:28:22 +00001241
1242
Benjamin Petersone41251e2008-04-25 01:59:09 +00001243 .. method:: set_visible(visible)
Georg Brandl116aa622007-08-15 14:28:22 +00001244
Benjamin Petersone41251e2008-04-25 01:59:09 +00001245 Set the message's visible headers to be the same as the headers in
1246 *message*. Parameter *visible* should be a :class:`Message` instance, an
1247 :class:`email.Message.Message` instance, a string, or a file-like object
1248 (which should be open in text mode).
Georg Brandl116aa622007-08-15 14:28:22 +00001249
1250
Benjamin Petersone41251e2008-04-25 01:59:09 +00001251 .. method:: update_visible()
Georg Brandl116aa622007-08-15 14:28:22 +00001252
Benjamin Petersone41251e2008-04-25 01:59:09 +00001253 When a :class:`BabylMessage` instance's original headers are modified, the
1254 visible headers are not automatically modified to correspond. This method
1255 updates the visible headers as follows: each visible header with a
1256 corresponding original header is set to the value of the original header,
1257 each visible header without a corresponding original header is removed,
1258 and any of :mailheader:`Date`, :mailheader:`From`, :mailheader:`Reply-To`,
1259 :mailheader:`To`, :mailheader:`CC`, and :mailheader:`Subject` that are
1260 present in the original headers but not the visible headers are added to
1261 the visible headers.
Georg Brandl116aa622007-08-15 14:28:22 +00001262
1263When a :class:`BabylMessage` instance is created based upon a
1264:class:`MaildirMessage` instance, the following conversions take place:
1265
1266+-------------------+-------------------------------+
1267| Resulting state | :class:`MaildirMessage` state |
1268+===================+===============================+
1269| "unseen" label | no S flag |
1270+-------------------+-------------------------------+
1271| "deleted" label | T flag |
1272+-------------------+-------------------------------+
1273| "answered" label | R flag |
1274+-------------------+-------------------------------+
1275| "forwarded" label | P flag |
1276+-------------------+-------------------------------+
1277
1278When a :class:`BabylMessage` instance is created based upon an
1279:class:`mboxMessage` or :class:`MMDFMessage` instance, the :mailheader:`Status`
1280and :mailheader:`X-Status` headers are omitted and the following conversions
1281take place:
1282
1283+------------------+----------------------------------------------+
1284| Resulting state | :class:`mboxMessage` or :class:`MMDFMessage` |
1285| | state |
1286+==================+==============================================+
1287| "unseen" label | no R flag |
1288+------------------+----------------------------------------------+
1289| "deleted" label | D flag |
1290+------------------+----------------------------------------------+
1291| "answered" label | A flag |
1292+------------------+----------------------------------------------+
1293
1294When a :class:`BabylMessage` instance is created based upon an
1295:class:`MHMessage` instance, the following conversions take place:
1296
1297+------------------+--------------------------+
1298| Resulting state | :class:`MHMessage` state |
1299+==================+==========================+
1300| "unseen" label | "unseen" sequence |
1301+------------------+--------------------------+
1302| "answered" label | "replied" sequence |
1303+------------------+--------------------------+
1304
1305
1306.. _mailbox-mmdfmessage:
1307
1308:class:`MMDFMessage`
1309^^^^^^^^^^^^^^^^^^^^
1310
1311
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001312.. class:: MMDFMessage(message=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001313
1314 A message with MMDF-specific behaviors. Parameter *message* has the same meaning
1315 as with the :class:`Message` constructor.
1316
Benjamin Petersone41251e2008-04-25 01:59:09 +00001317 As with message in an mbox mailbox, MMDF messages are stored with the
1318 sender's address and the delivery date in an initial line beginning with
1319 "From ". Likewise, flags that indicate the state of the message are
1320 typically stored in :mailheader:`Status` and :mailheader:`X-Status` headers.
Georg Brandl116aa622007-08-15 14:28:22 +00001321
Benjamin Petersone41251e2008-04-25 01:59:09 +00001322 Conventional flags for MMDF messages are identical to those of mbox message
1323 and are as follows:
Georg Brandl116aa622007-08-15 14:28:22 +00001324
Benjamin Petersone41251e2008-04-25 01:59:09 +00001325 +------+----------+--------------------------------+
1326 | Flag | Meaning | Explanation |
1327 +======+==========+================================+
1328 | R | Read | Read |
1329 +------+----------+--------------------------------+
1330 | O | Old | Previously detected by MUA |
1331 +------+----------+--------------------------------+
1332 | D | Deleted | Marked for subsequent deletion |
1333 +------+----------+--------------------------------+
1334 | F | Flagged | Marked as important |
1335 +------+----------+--------------------------------+
1336 | A | Answered | Replied to |
1337 +------+----------+--------------------------------+
Georg Brandl116aa622007-08-15 14:28:22 +00001338
Benjamin Petersone41251e2008-04-25 01:59:09 +00001339 The "R" and "O" flags are stored in the :mailheader:`Status` header, and the
1340 "D", "F", and "A" flags are stored in the :mailheader:`X-Status` header. The
1341 flags and headers typically appear in the order mentioned.
Georg Brandl116aa622007-08-15 14:28:22 +00001342
Benjamin Petersone41251e2008-04-25 01:59:09 +00001343 :class:`MMDFMessage` instances offer the following methods, which are
1344 identical to those offered by :class:`mboxMessage`:
Georg Brandl116aa622007-08-15 14:28:22 +00001345
1346
Benjamin Petersone41251e2008-04-25 01:59:09 +00001347 .. method:: get_from()
Georg Brandl116aa622007-08-15 14:28:22 +00001348
Benjamin Petersone41251e2008-04-25 01:59:09 +00001349 Return a string representing the "From " line that marks the start of the
1350 message in an mbox mailbox. The leading "From " and the trailing newline
1351 are excluded.
Georg Brandl116aa622007-08-15 14:28:22 +00001352
1353
Georg Brandlcd7f32b2009-06-08 09:13:45 +00001354 .. method:: set_from(from_, time_=None)
Georg Brandl116aa622007-08-15 14:28:22 +00001355
Benjamin Petersone41251e2008-04-25 01:59:09 +00001356 Set the "From " line to *from_*, which should be specified without a
1357 leading "From " or trailing newline. For convenience, *time_* may be
1358 specified and will be formatted appropriately and appended to *from_*. If
1359 *time_* is specified, it should be a :class:`struct_time` instance, a
1360 tuple suitable for passing to :meth:`time.strftime`, or ``True`` (to use
1361 :meth:`time.gmtime`).
Georg Brandl116aa622007-08-15 14:28:22 +00001362
1363
Benjamin Petersone41251e2008-04-25 01:59:09 +00001364 .. method:: get_flags()
Georg Brandl116aa622007-08-15 14:28:22 +00001365
Benjamin Petersone41251e2008-04-25 01:59:09 +00001366 Return a string specifying the flags that are currently set. If the
1367 message complies with the conventional format, the result is the
1368 concatenation in the following order of zero or one occurrence of each of
1369 ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001370
1371
Benjamin Petersone41251e2008-04-25 01:59:09 +00001372 .. method:: set_flags(flags)
Georg Brandl116aa622007-08-15 14:28:22 +00001373
Benjamin Petersone41251e2008-04-25 01:59:09 +00001374 Set the flags specified by *flags* and unset all others. Parameter *flags*
1375 should be the concatenation in any order of zero or more occurrences of
1376 each of ``'R'``, ``'O'``, ``'D'``, ``'F'``, and ``'A'``.
Georg Brandl116aa622007-08-15 14:28:22 +00001377
1378
Benjamin Petersone41251e2008-04-25 01:59:09 +00001379 .. method:: add_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001380
Benjamin Petersone41251e2008-04-25 01:59:09 +00001381 Set the flag(s) specified by *flag* without changing other flags. To add
1382 more than one flag at a time, *flag* may be a string of more than one
1383 character.
Georg Brandl116aa622007-08-15 14:28:22 +00001384
1385
Benjamin Petersone41251e2008-04-25 01:59:09 +00001386 .. method:: remove_flag(flag)
Georg Brandl116aa622007-08-15 14:28:22 +00001387
Benjamin Petersone41251e2008-04-25 01:59:09 +00001388 Unset the flag(s) specified by *flag* without changing other flags. To
1389 remove more than one flag at a time, *flag* maybe a string of more than
1390 one character.
Georg Brandl116aa622007-08-15 14:28:22 +00001391
1392When an :class:`MMDFMessage` instance is created based upon a
1393:class:`MaildirMessage` instance, a "From " line is generated based upon the
1394:class:`MaildirMessage` instance's delivery date, and the following conversions
1395take place:
1396
1397+-----------------+-------------------------------+
1398| Resulting state | :class:`MaildirMessage` state |
1399+=================+===============================+
1400| R flag | S flag |
1401+-----------------+-------------------------------+
1402| O flag | "cur" subdirectory |
1403+-----------------+-------------------------------+
1404| D flag | T flag |
1405+-----------------+-------------------------------+
1406| F flag | F flag |
1407+-----------------+-------------------------------+
1408| A flag | R flag |
1409+-----------------+-------------------------------+
1410
1411When an :class:`MMDFMessage` instance is created based upon an
1412:class:`MHMessage` instance, the following conversions take place:
1413
1414+-------------------+--------------------------+
1415| Resulting state | :class:`MHMessage` state |
1416+===================+==========================+
1417| R flag and O flag | no "unseen" sequence |
1418+-------------------+--------------------------+
1419| O flag | "unseen" sequence |
1420+-------------------+--------------------------+
1421| F flag | "flagged" sequence |
1422+-------------------+--------------------------+
1423| A flag | "replied" sequence |
1424+-------------------+--------------------------+
1425
1426When an :class:`MMDFMessage` instance is created based upon a
1427:class:`BabylMessage` instance, the following conversions take place:
1428
1429+-------------------+-----------------------------+
1430| Resulting state | :class:`BabylMessage` state |
1431+===================+=============================+
1432| R flag and O flag | no "unseen" label |
1433+-------------------+-----------------------------+
1434| O flag | "unseen" label |
1435+-------------------+-----------------------------+
1436| D flag | "deleted" label |
1437+-------------------+-----------------------------+
1438| A flag | "answered" label |
1439+-------------------+-----------------------------+
1440
1441When an :class:`MMDFMessage` instance is created based upon an
1442:class:`mboxMessage` instance, the "From " line is copied and all flags directly
1443correspond:
1444
1445+-----------------+----------------------------+
1446| Resulting state | :class:`mboxMessage` state |
1447+=================+============================+
1448| R flag | R flag |
1449+-----------------+----------------------------+
1450| O flag | O flag |
1451+-----------------+----------------------------+
1452| D flag | D flag |
1453+-----------------+----------------------------+
1454| F flag | F flag |
1455+-----------------+----------------------------+
1456| A flag | A flag |
1457+-----------------+----------------------------+
1458
1459
1460Exceptions
1461----------
1462
1463The following exception classes are defined in the :mod:`mailbox` module:
1464
1465
Benjamin Petersone41251e2008-04-25 01:59:09 +00001466.. exception:: Error()
Georg Brandl116aa622007-08-15 14:28:22 +00001467
1468 The based class for all other module-specific exceptions.
1469
1470
Benjamin Petersone41251e2008-04-25 01:59:09 +00001471.. exception:: NoSuchMailboxError()
Georg Brandl116aa622007-08-15 14:28:22 +00001472
1473 Raised when a mailbox is expected but is not found, such as when instantiating a
1474 :class:`Mailbox` subclass with a path that does not exist (and with the *create*
1475 parameter set to ``False``), or when opening a folder that does not exist.
1476
1477
Georg Brandl734e2682008-08-12 08:18:18 +00001478.. exception:: NotEmptyError()
Georg Brandl116aa622007-08-15 14:28:22 +00001479
1480 Raised when a mailbox is not empty but is expected to be, such as when deleting
1481 a folder that contains messages.
1482
1483
Benjamin Petersone41251e2008-04-25 01:59:09 +00001484.. exception:: ExternalClashError()
Georg Brandl116aa622007-08-15 14:28:22 +00001485
1486 Raised when some mailbox-related condition beyond the control of the program
1487 causes it to be unable to proceed, such as when failing to acquire a lock that
1488 another program already holds a lock, or when a uniquely-generated file name
1489 already exists.
1490
1491
Benjamin Petersone41251e2008-04-25 01:59:09 +00001492.. exception:: FormatError()
Georg Brandl116aa622007-08-15 14:28:22 +00001493
1494 Raised when the data in a file cannot be parsed, such as when an :class:`MH`
1495 instance attempts to read a corrupted :file:`.mh_sequences` file.
1496
1497
Georg Brandl116aa622007-08-15 14:28:22 +00001498.. _mailbox-examples:
1499
1500Examples
1501--------
1502
1503A simple example of printing the subjects of all messages in a mailbox that seem
1504interesting::
1505
1506 import mailbox
1507 for message in mailbox.mbox('~/mbox'):
1508 subject = message['subject'] # Could possibly be None.
1509 if subject and 'python' in subject.lower():
Georg Brandl6911e3c2007-09-04 07:15:32 +00001510 print(subject)
Georg Brandl116aa622007-08-15 14:28:22 +00001511
1512To copy all mail from a Babyl mailbox to an MH mailbox, converting all of the
1513format-specific information that can be converted::
1514
1515 import mailbox
1516 destination = mailbox.MH('~/Mail')
1517 destination.lock()
1518 for message in mailbox.Babyl('~/RMAIL'):
Christian Heimesdd15f6c2008-03-16 00:07:10 +00001519 destination.add(mailbox.MHMessage(message))
Georg Brandl116aa622007-08-15 14:28:22 +00001520 destination.flush()
1521 destination.unlock()
1522
1523This example sorts mail from several mailing lists into different mailboxes,
1524being careful to avoid mail corruption due to concurrent modification by other
1525programs, mail loss due to interruption of the program, or premature termination
1526due to malformed messages in the mailbox::
1527
1528 import mailbox
1529 import email.Errors
1530
1531 list_names = ('python-list', 'python-dev', 'python-bugs')
1532
Georg Brandlf6945182008-02-01 11:56:49 +00001533 boxes = {name: mailbox.mbox('~/email/%s' % name) for name in list_names}
Georg Brandl116aa622007-08-15 14:28:22 +00001534 inbox = mailbox.Maildir('~/Maildir', factory=None)
1535
1536 for key in inbox.iterkeys():
1537 try:
1538 message = inbox[key]
1539 except email.Errors.MessageParseError:
1540 continue # The message is malformed. Just leave it.
1541
1542 for name in list_names:
1543 list_id = message['list-id']
1544 if list_id and name in list_id:
1545 # Get mailbox to use
1546 box = boxes[name]
1547
1548 # Write copy to disk before removing original.
1549 # If there's a crash, you might duplicate a message, but
1550 # that's better than losing a message completely.
1551 box.lock()
1552 box.add(message)
Georg Brandl48310cd2009-01-03 21:18:54 +00001553 box.flush()
Georg Brandl116aa622007-08-15 14:28:22 +00001554 box.unlock()
1555
1556 # Remove original message
1557 inbox.lock()
1558 inbox.discard(key)
1559 inbox.flush()
1560 inbox.unlock()
1561 break # Found destination, so stop looking.
1562
1563 for box in boxes.itervalues():
1564 box.close()
1565