blob: 24765c8d3ea1c20eab34b6b3d90c609841b5aa32 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{mailbox} ---
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00002 Manipulate mailboxes in various formats}
Fred Drake199b79c1999-02-20 05:04:59 +00003
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00004\declaremodule{}{mailbox}
5\moduleauthor{Gregory K.~Johnson}{gkj@gregorykjohnson.com}
6\sectionauthor{Gregory K.~Johnson}{gkj@gregorykjohnson.com}
7\modulesynopsis{Manipulate mailboxes in various formats}
Fred Drakeb91e9341998-07-23 17:59:49 +00008
Guido van Rossum39a23cc1997-06-02 21:04:41 +00009
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +000010This 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 \module{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\begin{seealso}
18 \seemodule{email}{Represent and manipulate messages.}
19\end{seealso}
20
21\subsection{\class{Mailbox} objects}
22\label{mailbox-objects}
23
24\begin{classdesc*}{Mailbox}
25A mailbox, which may be inspected and modified.
26\end{classdesc*}
27
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +000028The \class{Mailbox} class defines an interface and
29is not intended to be instantiated. Instead, format-specific
30subclasses should inherit from \class{Mailbox} and your code
31should instantiate a particular subclass.
32
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +000033The \class{Mailbox} interface is dictionary-like, with small keys
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +000034corresponding to messages. Keys are issued by the \class{Mailbox}
35instance with which they will be used and are only meaningful to that
36\class{Mailbox} instance. A key continues to identify a message even
37if the corresponding message is modified, such as by replacing it with
38another message.
39
40Messages may be added to a \class{Mailbox} instance using the set-like
41method \method{add()} and removed using a \code{del} statement or the
42set-like methods \method{remove()} and \method{discard()}.
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +000043
44\class{Mailbox} interface semantics differ from dictionary semantics in some
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +000045noteworthy ways. Each time a message is requested, a new
46representation (typically a \class{Message} instance) is generated
47based upon the current state of the mailbox. Similarly, when a message
48is added to a \class{Mailbox} instance, the provided message
49representation's contents are copied. In neither case is a reference
50to the message representation kept by the \class{Mailbox} instance.
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +000051
52The default \class{Mailbox} iterator iterates over message representations, not
53keys as the default dictionary iterator does. Moreover, modification of a
54mailbox during iteration is safe and well-defined. Messages added to the
55mailbox after an iterator is created will not be seen by the iterator. Messages
56removed from the mailbox before the iterator yields them will be silently
57skipped, though using a key from an iterator may result in a
58\exception{KeyError} exception if the corresponding message is subsequently
59removed.
60
Andrew M. Kuchling564c9f52007-01-24 20:08:13 +000061\begin{notice}[warning]
62Be very cautious when modifying mailboxes that might be
63simultaneously changed by some other process. The safest mailbox
64format to use for such tasks is Maildir; try to avoid using
65single-file formats such as mbox for concurrent writing. If you're
66modifying a mailbox, you
67\emph{must} lock it by calling the \method{lock()} and
68\method{unlock()} methods \emph{before} reading any messages in the file
69or making any changes by adding or deleting a message. Failing to
70lock the mailbox runs the risk of losing messages or corrupting the entire
71mailbox.
72\end{notice}
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +000073
74\class{Mailbox} instances have the following methods:
75
76\begin{methoddesc}{add}{message}
77Add \var{message} to the mailbox and return the key that has been assigned to
78it.
79
80Parameter \var{message} may be a \class{Message} instance, an
81\class{email.Message.Message} instance, a string, or a file-like object (which
82should be open in text mode). If \var{message} is an instance of the
83appropriate format-specific \class{Message} subclass (e.g., if it's an
84\class{mboxMessage} instance and this is an \class{mbox} instance), its
85format-specific information is used. Otherwise, reasonable defaults for
86format-specific information are used.
87\end{methoddesc}
88
89\begin{methoddesc}{remove}{key}
90\methodline{__delitem__}{key}
91\methodline{discard}{key}
92Delete the message corresponding to \var{key} from the mailbox.
93
94If no such message exists, a \exception{KeyError} exception is raised if the
95method was called as \method{remove()} or \method{__delitem__()} but no
96exception is raised if the method was called as \method{discard()}. The
97behavior of \method{discard()} may be preferred if the underlying mailbox
98format supports concurrent modification by other processes.
99\end{methoddesc}
100
101\begin{methoddesc}{__setitem__}{key, message}
102Replace the message corresponding to \var{key} with \var{message}. Raise a
103\exception{KeyError} exception if no message already corresponds to \var{key}.
104
105As with \method{add()}, parameter \var{message} may be a \class{Message}
106instance, an \class{email.Message.Message} instance, a string, or a file-like
107object (which should be open in text mode). If \var{message} is an instance of
108the appropriate format-specific \class{Message} subclass (e.g., if it's an
109\class{mboxMessage} instance and this is an \class{mbox} instance), its
110format-specific information is used. Otherwise, the format-specific information
111of the message that currently corresponds to \var{key} is left unchanged.
112\end{methoddesc}
113
114\begin{methoddesc}{iterkeys}{}
115\methodline{keys}{}
116Return an iterator over all keys if called as \method{iterkeys()} or return a
117list of keys if called as \method{keys()}.
118\end{methoddesc}
119
120\begin{methoddesc}{itervalues}{}
121\methodline{__iter__}{}
122\methodline{values}{}
123Return an iterator over representations of all messages if called as
124\method{itervalues()} or \method{__iter__()} or return a list of such
125representations if called as \method{values()}. The messages are represented as
126instances of the appropriate format-specific \class{Message} subclass unless a
127custom message factory was specified when the \class{Mailbox} instance was
128initialized. \note{The behavior of \method{__iter__()} is unlike that of
129dictionaries, which iterate over keys.}
130\end{methoddesc}
131
132\begin{methoddesc}{iteritems}{}
133\methodline{items}{}
134Return an iterator over (\var{key}, \var{message}) pairs, where \var{key} is a
135key and \var{message} is a message representation, if called as
136\method{iteritems()} or return a list of such pairs if called as
137\method{items()}. The messages are represented as instances of the appropriate
138format-specific \class{Message} subclass unless a custom message factory was
139specified when the \class{Mailbox} instance was initialized.
140\end{methoddesc}
141
142\begin{methoddesc}{get}{key\optional{, default=None}}
143\methodline{__getitem__}{key}
144Return a representation of the message corresponding to \var{key}. If no such
145message exists, \var{default} is returned if the method was called as
146\method{get()} and a \exception{KeyError} exception is raised if the method was
147called as \method{__getitem__()}. The message is represented as an instance of
148the appropriate format-specific \class{Message} subclass unless a custom
149message factory was specified when the \class{Mailbox} instance was
150initialized.
151\end{methoddesc}
152
153\begin{methoddesc}{get_message}{key}
154Return a representation of the message corresponding to \var{key} as an
155instance of the appropriate format-specific \class{Message} subclass, or raise
156a \exception{KeyError} exception if no such message exists.
157\end{methoddesc}
158
159\begin{methoddesc}{get_string}{key}
160Return a string representation of the message corresponding to \var{key}, or
161raise a \exception{KeyError} exception if no such message exists.
162\end{methoddesc}
163
164\begin{methoddesc}{get_file}{key}
165Return a file-like representation of the message corresponding to \var{key},
166or raise a \exception{KeyError} exception if no such message exists. The
167file-like object behaves as if open in binary mode. This file should be closed
168once it is no longer needed.
169
170\note{Unlike other representations of messages, file-like representations are
171not necessarily independent of the \class{Mailbox} instance that created them
172or of the underlying mailbox. More specific documentation is provided by each
173subclass.}
174\end{methoddesc}
175
176\begin{methoddesc}{has_key}{key}
177\methodline{__contains__}{key}
178Return \code{True} if \var{key} corresponds to a message, \code{False}
179otherwise.
180\end{methoddesc}
181
182\begin{methoddesc}{__len__}{}
183Return a count of messages in the mailbox.
184\end{methoddesc}
185
186\begin{methoddesc}{clear}{}
187Delete all messages from the mailbox.
188\end{methoddesc}
189
190\begin{methoddesc}{pop}{key\optional{, default}}
191Return a representation of the message corresponding to \var{key} and delete
192the message. If no such message exists, return \var{default} if it was supplied
193or else raise a \exception{KeyError} exception. The message is represented as
194an instance of the appropriate format-specific \class{Message} subclass unless
195a custom message factory was specified when the \class{Mailbox} instance was
196initialized.
197\end{methoddesc}
198
199\begin{methoddesc}{popitem}{}
200Return an arbitrary (\var{key}, \var{message}) pair, where \var{key} is a key
201and \var{message} is a message representation, and delete the corresponding
202message. If the mailbox is empty, raise a \exception{KeyError} exception. The
203message is represented as an instance of the appropriate format-specific
204\class{Message} subclass unless a custom message factory was specified when the
205\class{Mailbox} instance was initialized.
206\end{methoddesc}
207
208\begin{methoddesc}{update}{arg}
209Parameter \var{arg} should be a \var{key}-to-\var{message} mapping or an
210iterable of (\var{key}, \var{message}) pairs. Updates the mailbox so that, for
211each given \var{key} and \var{message}, the message corresponding to \var{key}
212is set to \var{message} as if by using \method{__setitem__()}. As with
213\method{__setitem__()}, each \var{key} must already correspond to a message in
214the mailbox or else a \exception{KeyError} exception will be raised, so in
215general it is incorrect for \var{arg} to be a \class{Mailbox} instance.
216\note{Unlike with dictionaries, keyword arguments are not supported.}
217\end{methoddesc}
218
219\begin{methoddesc}{flush}{}
220Write any pending changes to the filesystem. For some \class{Mailbox}
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +0000221subclasses, changes are always written immediately and \method{flush()} does
222nothing, but you should still make a habit of calling this method.
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000223\end{methoddesc}
224
225\begin{methoddesc}{lock}{}
226Acquire an exclusive advisory lock on the mailbox so that other processes know
227not to modify it. An \exception{ExternalClashError} is raised if the lock is
228not available. The particular locking mechanisms used depend upon the mailbox
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +0000229format. You should \emph{always} lock the mailbox before making any
230modifications to its contents.
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000231\end{methoddesc}
232
233\begin{methoddesc}{unlock}{}
234Release the lock on the mailbox, if any.
235\end{methoddesc}
236
237\begin{methoddesc}{close}{}
238Flush the mailbox, unlock it if necessary, and close any open files. For some
239\class{Mailbox} subclasses, this method does nothing.
240\end{methoddesc}
241
242
243\subsubsection{\class{Maildir}}
244\label{mailbox-maildir}
245
246\begin{classdesc}{Maildir}{dirname\optional{, factory=rfc822.Message\optional{,
247create=True}}}
248A subclass of \class{Mailbox} for mailboxes in Maildir format. Parameter
249\var{factory} is a callable object that accepts a file-like message
Andrew M. Kuchling09019652006-05-07 17:14:53 +0000250representation (which behaves as if opened in binary mode) and returns a custom
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000251representation. If \var{factory} is \code{None}, \class{MaildirMessage} is used
252as the default message representation. If \var{create} is \code{True}, the
253mailbox is created if it does not exist.
254
255It is for historical reasons that \var{factory} defaults to
256\class{rfc822.Message} and that \var{dirname} is named as such rather than
257\var{path}. For a \class{Maildir} instance that behaves like instances of other
258\class{Mailbox} subclasses, set \var{factory} to \code{None}.
259\end{classdesc}
260
261Maildir is a directory-based mailbox format invented for the qmail mail
262transfer agent and now widely supported by other programs. Messages in a
263Maildir mailbox are stored in separate files within a common directory
264structure. This design allows Maildir mailboxes to be accessed and modified by
265multiple unrelated programs without data corruption, so file locking is
266unnecessary.
267
268Maildir mailboxes contain three subdirectories, namely: \file{tmp}, \file{new},
269and \file{cur}. Messages are created momentarily in the \file{tmp} subdirectory
270and then moved to the \file{new} subdirectory to finalize delivery. A mail user
271agent may subsequently move the message to the \file{cur} subdirectory and
272store information about the state of the message in a special "info" section
273appended to its file name.
274
275Folders of the style introduced by the Courier mail transfer agent are also
276supported. Any subdirectory of the main mailbox is considered a folder if
277\character{.} is the first character in its name. Folder names are represented
278by \class{Maildir} without the leading \character{.}. Each folder is itself a
279Maildir mailbox but should not contain other folders. Instead, a logical
280nesting is indicated using \character{.} to delimit levels, e.g.,
281"Archived.2005.07".
282
283\begin{notice}
284The Maildir specification requires the use of a colon (\character{:}) in
285certain message file names. However, some operating systems do not permit this
286character in file names, If you wish to use a Maildir-like format on such an
287operating system, you should specify another character to use instead. The
288exclamation point (\character{!}) is a popular choice. For example:
289\begin{verbatim}
290import mailbox
291mailbox.Maildir.colon = '!'
292\end{verbatim}
293The \member{colon} attribute may also be set on a per-instance basis.
294\end{notice}
295
296\class{Maildir} instances have all of the methods of \class{Mailbox} in
297addition to the following:
298
299\begin{methoddesc}{list_folders}{}
300Return a list of the names of all folders.
301\end{methoddesc}
302
303\begin{methoddesc}{get_folder}{folder}
304Return a \class{Maildir} instance representing the folder whose name is
305\var{folder}. A \exception{NoSuchMailboxError} exception is raised if the
306folder does not exist.
307\end{methoddesc}
308
309\begin{methoddesc}{add_folder}{folder}
310Create a folder whose name is \var{folder} and return a \class{Maildir}
311instance representing it.
312\end{methoddesc}
313
314\begin{methoddesc}{remove_folder}{folder}
315Delete the folder whose name is \var{folder}. If the folder contains any
316messages, a \exception{NotEmptyError} exception will be raised and the folder
317will not be deleted.
318\end{methoddesc}
319
320\begin{methoddesc}{clean}{}
321Delete temporary files from the mailbox that have not been accessed in the
322last 36 hours. The Maildir specification says that mail-reading programs
323should do this occasionally.
324\end{methoddesc}
325
326Some \class{Mailbox} methods implemented by \class{Maildir} deserve special
327remarks:
328
329\begin{methoddesc}{add}{message}
330\methodline[Maildir]{__setitem__}{key, message}
331\methodline[Maildir]{update}{arg}
332\warning{These methods generate unique file names based upon the current
333process ID. When using multiple threads, undetected name clashes may occur and
334cause corruption of the mailbox unless threads are coordinated to avoid using
335these methods to manipulate the same mailbox simultaneously.}
336\end{methoddesc}
337
338\begin{methoddesc}{flush}{}
339All changes to Maildir mailboxes are immediately applied, so this method does
340nothing.
341\end{methoddesc}
342
343\begin{methoddesc}{lock}{}
344\methodline{unlock}{}
345Maildir mailboxes do not support (or require) locking, so these methods do
346nothing.
347\end{methoddesc}
348
349\begin{methoddesc}{close}{}
350\class{Maildir} instances do not keep any open files and the underlying
351mailboxes do not support locking, so this method does nothing.
352\end{methoddesc}
353
354\begin{methoddesc}{get_file}{key}
355Depending upon the host platform, it may not be possible to modify or remove
356the underlying message while the returned file remains open.
357\end{methoddesc}
358
359\begin{seealso}
360 \seelink{http://www.qmail.org/man/man5/maildir.html}{maildir man page from
361 qmail}{The original specification of the format.}
362 \seelink{http://cr.yp.to/proto/maildir.html}{Using maildir format}{Notes
363 on Maildir by its inventor. Includes an updated name-creation scheme and
364 details on "info" semantics.}
365 \seelink{http://www.courier-mta.org/?maildir.html}{maildir man page from
366 Courier}{Another specification of the format. Describes a common extension
367 for supporting folders.}
368\end{seealso}
369
370\subsubsection{\class{mbox}}
371\label{mailbox-mbox}
372
373\begin{classdesc}{mbox}{path\optional{, factory=None\optional{, create=True}}}
374A subclass of \class{Mailbox} for mailboxes in mbox format. Parameter
375\var{factory} is a callable object that accepts a file-like message
Andrew M. Kuchling09019652006-05-07 17:14:53 +0000376representation (which behaves as if opened in binary mode) and returns a custom
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000377representation. If \var{factory} is \code{None}, \class{mboxMessage} is used as
378the default message representation. If \var{create} is \code{True}, the mailbox
379is created if it does not exist.
380\end{classdesc}
381
382The mbox format is the classic format for storing mail on \UNIX{} systems. All
383messages in an mbox mailbox are stored in a single file with the beginning of
384each message indicated by a line whose first five characters are "From~".
385
386Several variations of the mbox format exist to address perceived shortcomings
387in the original. In the interest of compatibility, \class{mbox} implements the
388original format, which is sometimes referred to as \dfn{mboxo}. This means that
389the \mailheader{Content-Length} header, if present, is ignored and that any
390occurrences of "From~" at the beginning of a line in a message body are
391transformed to ">From~" when storing the message, although occurences of
392">From~" are not transformed to "From~" when reading the message.
393
394Some \class{Mailbox} methods implemented by \class{mbox} deserve special
395remarks:
396
397\begin{methoddesc}{get_file}{key}
398Using the file after calling \method{flush()} or \method{close()} on the
399\class{mbox} instance may yield unpredictable results or raise an exception.
400\end{methoddesc}
401
402\begin{methoddesc}{lock}{}
403\methodline{unlock}{}
404Three locking mechanisms are used---dot locking and, if available, the
405\cfunction{flock()} and \cfunction{lockf()} system calls.
406\end{methoddesc}
407
408\begin{seealso}
409 \seelink{http://www.qmail.org/man/man5/mbox.html}{mbox man page from
410 qmail}{A specification of the format and its variations.}
411 \seelink{http://www.tin.org/bin/man.cgi?section=5\&topic=mbox}{mbox man
412 page from tin}{Another specification of the format, with details on
413 locking.}
414 \seelink{http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html}
415 {Configuring Netscape Mail on \UNIX{}: Why The Content-Length Format is
416 Bad}{An argument for using the original mbox format rather than a
417 variation.}
418 \seelink{http://homepages.tesco.net./\tilde{}J.deBoynePollard/FGA/mail-mbox-formats.html}
419 {"mbox" is a family of several mutually incompatible mailbox formats}{A
420 history of mbox variations.}
421\end{seealso}
422
423\subsubsection{\class{MH}}
424\label{mailbox-mh}
425
426\begin{classdesc}{MH}{path\optional{, factory=None\optional{, create=True}}}
427A subclass of \class{Mailbox} for mailboxes in MH format. Parameter
428\var{factory} is a callable object that accepts a file-like message
Andrew M. Kuchling09019652006-05-07 17:14:53 +0000429representation (which behaves as if opened in binary mode) and returns a custom
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000430representation. If \var{factory} is \code{None}, \class{MHMessage} is used as
431the default message representation. If \var{create} is \code{True}, the mailbox
432is created if it does not exist.
433\end{classdesc}
434
435MH is a directory-based mailbox format invented for the MH Message Handling
436System, a mail user agent. Each message in an MH mailbox resides in its own
437file. An MH mailbox may contain other MH mailboxes (called \dfn{folders}) in
438addition to messages. Folders may be nested indefinitely. MH mailboxes also
439support \dfn{sequences}, which are named lists used to logically group messages
440without moving them to sub-folders. Sequences are defined in a file called
441\file{.mh_sequences} in each folder.
442
443The \class{MH} class manipulates MH mailboxes, but it does not attempt to
444emulate all of \program{mh}'s behaviors. In particular, it does not modify and
445is not affected by the \file{context} or \file{.mh_profile} files that are used
446by \program{mh} to store its state and configuration.
447
448\class{MH} instances have all of the methods of \class{Mailbox} in addition to
449the following:
450
451\begin{methoddesc}{list_folders}{}
452Return a list of the names of all folders.
453\end{methoddesc}
454
455\begin{methoddesc}{get_folder}{folder}
456Return an \class{MH} instance representing the folder whose name is
457\var{folder}. A \exception{NoSuchMailboxError} exception is raised if the
458folder does not exist.
459\end{methoddesc}
460
461\begin{methoddesc}{add_folder}{folder}
462Create a folder whose name is \var{folder} and return an \class{MH} instance
463representing it.
464\end{methoddesc}
465
466\begin{methoddesc}{remove_folder}{folder}
467Delete the folder whose name is \var{folder}. If the folder contains any
468messages, a \exception{NotEmptyError} exception will be raised and the folder
469will not be deleted.
470\end{methoddesc}
471
472\begin{methoddesc}{get_sequences}{}
473Return a dictionary of sequence names mapped to key lists. If there are no
474sequences, the empty dictionary is returned.
475\end{methoddesc}
476
477\begin{methoddesc}{set_sequences}{sequences}
478Re-define the sequences that exist in the mailbox based upon \var{sequences}, a
479dictionary of names mapped to key lists, like returned by
480\method{get_sequences()}.
481\end{methoddesc}
482
483\begin{methoddesc}{pack}{}
484Rename messages in the mailbox as necessary to eliminate gaps in numbering.
485Entries in the sequences list are updated correspondingly. \note{Already-issued
486keys are invalidated by this operation and should not be subsequently used.}
487\end{methoddesc}
488
489Some \class{Mailbox} methods implemented by \class{MH} deserve special remarks:
490
491\begin{methoddesc}{remove}{key}
492\methodline{__delitem__}{key}
493\methodline{discard}{key}
494These methods immediately delete the message. The MH convention of marking a
495message for deletion by prepending a comma to its name is not used.
496\end{methoddesc}
497
498\begin{methoddesc}{lock}{}
499\methodline{unlock}{}
500Three locking mechanisms are used---dot locking and, if available, the
501\cfunction{flock()} and \cfunction{lockf()} system calls. For MH mailboxes,
502locking the mailbox means locking the \file{.mh_sequences} file and, only for
503the duration of any operations that affect them, locking individual message
504files.
505\end{methoddesc}
506
507\begin{methoddesc}{get_file}{key}
508Depending upon the host platform, it may not be possible to remove the
509underlying message while the returned file remains open.
510\end{methoddesc}
511
512\begin{methoddesc}{flush}{}
513All changes to MH mailboxes are immediately applied, so this method does
514nothing.
515\end{methoddesc}
516
517\begin{methoddesc}{close}{}
518\class{MH} instances do not keep any open files, so this method is equivelant
519to \method{unlock()}.
520\end{methoddesc}
521
522\begin{seealso}
523\seelink{http://www.nongnu.org/nmh/}{nmh - Message Handling System}{Home page
524of \program{nmh}, an updated version of the original \program{mh}.}
525\seelink{http://www.ics.uci.edu/\tilde{}mh/book/}{MH \& nmh: Email for Users \&
526Programmers}{A GPL-licensed book on \program{mh} and \program{nmh}, with some
527information on the mailbox format.}
528\end{seealso}
529
530\subsubsection{\class{Babyl}}
531\label{mailbox-babyl}
532
533\begin{classdesc}{Babyl}{path\optional{, factory=None\optional{, create=True}}}
534A subclass of \class{Mailbox} for mailboxes in Babyl format. Parameter
535\var{factory} is a callable object that accepts a file-like message
Andrew M. Kuchling09019652006-05-07 17:14:53 +0000536representation (which behaves as if opened in binary mode) and returns a custom
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000537representation. If \var{factory} is \code{None}, \class{BabylMessage} is used
538as the default message representation. If \var{create} is \code{True}, the
539mailbox is created if it does not exist.
540\end{classdesc}
541
542Babyl is a single-file mailbox format used by the Rmail mail user agent
543included with Emacs. The beginning of a message is indicated by a line
544containing the two characters Control-Underscore
545(\character{\textbackslash037}) and Control-L (\character{\textbackslash014}).
546The end of a message is indicated by the start of the next message or, in the
547case of the last message, a line containing a Control-Underscore
548(\character{\textbackslash037}) character.
549
550Messages in a Babyl mailbox have two sets of headers, original headers and
551so-called visible headers. Visible headers are typically a subset of the
552original headers that have been reformatted or abridged to be more attractive.
553Each message in a Babyl mailbox also has an accompanying list of \dfn{labels},
554or short strings that record extra information about the message, and a list of
555all user-defined labels found in the mailbox is kept in the Babyl options
556section.
557
558\class{Babyl} instances have all of the methods of \class{Mailbox} in addition
559to the following:
560
561\begin{methoddesc}{get_labels}{}
562Return a list of the names of all user-defined labels used in the mailbox.
563\note{The actual messages are inspected to determine which labels exist in the
564mailbox rather than consulting the list of labels in the Babyl options section,
565but the Babyl section is updated whenever the mailbox is modified.}
566\end{methoddesc}
567
568Some \class{Mailbox} methods implemented by \class{Babyl} deserve special
569remarks:
570
571\begin{methoddesc}{get_file}{key}
572In Babyl mailboxes, the headers of a message are not stored contiguously with
573the body of the message. To generate a file-like representation, the headers
574and body are copied together into a \class{StringIO} instance (from the
575\module{StringIO} module), which has an API identical to that of a file. As a
576result, the file-like object is truly independent of the underlying mailbox but
577does not save memory compared to a string representation.
578\end{methoddesc}
579
580\begin{methoddesc}{lock}{}
581\methodline{unlock}{}
582Three locking mechanisms are used---dot locking and, if available, the
583\cfunction{flock()} and \cfunction{lockf()} system calls.
584\end{methoddesc}
585
586\begin{seealso}
587\seelink{http://quimby.gnus.org/notes/BABYL}{Format of Version 5 Babyl Files}{A
588specification of the Babyl format.}
589\seelink{http://www.gnu.org/software/emacs/manual/html_node/Rmail.html}{Reading
590Mail with Rmail}{The Rmail manual, with some information on Babyl semantics.}
591\end{seealso}
592
593\subsubsection{\class{MMDF}}
594\label{mailbox-mmdf}
595
596\begin{classdesc}{MMDF}{path\optional{, factory=None\optional{, create=True}}}
597A subclass of \class{Mailbox} for mailboxes in MMDF format. Parameter
598\var{factory} is a callable object that accepts a file-like message
Andrew M. Kuchling09019652006-05-07 17:14:53 +0000599representation (which behaves as if opened in binary mode) and returns a custom
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +0000600representation. If \var{factory} is \code{None}, \class{MMDFMessage} is used as
601the default message representation. If \var{create} is \code{True}, the mailbox
602is created if it does not exist.
603\end{classdesc}
604
605MMDF is a single-file mailbox format invented for the Multichannel Memorandum
606Distribution Facility, a mail transfer agent. Each message is in the same form
607as an mbox message but is bracketed before and after by lines containing four
608Control-A (\character{\textbackslash001}) characters. As with the mbox format,
609the beginning of each message is indicated by a line whose first five
610characters are "From~", but additional occurrences of "From~" are not
611transformed to ">From~" when storing messages because the extra message
612separator lines prevent mistaking such occurrences for the starts of subsequent
613messages.
614
615Some \class{Mailbox} methods implemented by \class{MMDF} deserve special
616remarks:
617
618\begin{methoddesc}{get_file}{key}
619Using the file after calling \method{flush()} or \method{close()} on the
620\class{MMDF} instance may yield unpredictable results or raise an exception.
621\end{methoddesc}
622
623\begin{methoddesc}{lock}{}
624\methodline{unlock}{}
625Three locking mechanisms are used---dot locking and, if available, the
626\cfunction{flock()} and \cfunction{lockf()} system calls.
627\end{methoddesc}
628
629\begin{seealso}
630\seelink{http://www.tin.org/bin/man.cgi?section=5\&topic=mmdf}{mmdf man page
631from tin}{A specification of MMDF format from the documentation of tin, a
632newsreader.}
633\seelink{http://en.wikipedia.org/wiki/MMDF}{MMDF}{A Wikipedia article
634describing the Multichannel Memorandum Distribution Facility.}
635\end{seealso}
636
637\subsection{\class{Message} objects}
638\label{mailbox-message-objects}
639
640\begin{classdesc}{Message}{\optional{message}}
641A subclass of the \module{email.Message} module's \class{Message}. Subclasses
642of \class{mailbox.Message} add mailbox-format-specific state and behavior.
643
644If \var{message} is omitted, the new instance is created in a default, empty
645state. If \var{message} is an \class{email.Message.Message} instance, its
646contents are copied; furthermore, any format-specific information is converted
647insofar as possible if \var{message} is a \class{Message} instance. If
648\var{message} is a string or a file, it should contain an \rfc{2822}-compliant
649message, which is read and parsed.
650\end{classdesc}
651
652The format-specific state and behaviors offered by subclasses vary, but in
653general it is only the properties that are not specific to a particular mailbox
654that are supported (although presumably the properties are specific to a
655particular mailbox format). For example, file offsets for single-file mailbox
656formats and file names for directory-based mailbox formats are not retained,
657because they are only applicable to the original mailbox. But state such as
658whether a message has been read by the user or marked as important is retained,
659because it applies to the message itself.
660
661There is no requirement that \class{Message} instances be used to represent
662messages retrieved using \class{Mailbox} instances. In some situations, the
663time and memory required to generate \class{Message} representations might not
664not acceptable. For such situations, \class{Mailbox} instances also offer
665string and file-like representations, and a custom message factory may be
666specified when a \class{Mailbox} instance is initialized.
667
668\subsubsection{\class{MaildirMessage}}
669\label{mailbox-maildirmessage}
670
671\begin{classdesc}{MaildirMessage}{\optional{message}}
672A message with Maildir-specific behaviors. Parameter \var{message}
673has the same meaning as with the \class{Message} constructor.
674\end{classdesc}
675
676Typically, a mail user agent application moves all of the messages in the
677\file{new} subdirectory to the \file{cur} subdirectory after the first time the
678user opens and closes the mailbox, recording that the messages are old whether
679or not they've actually been read. Each message in \file{cur} has an "info"
680section added to its file name to store information about its state. (Some mail
681readers may also add an "info" section to messages in \file{new}.) The "info"
682section may take one of two forms: it may contain "2," followed by a list of
683standardized flags (e.g., "2,FR") or it may contain "1," followed by so-called
684experimental information. Standard flags for Maildir messages are as follows:
685
686\begin{tableiii}{l|l|l}{textrm}{Flag}{Meaning}{Explanation}
687\lineiii{D}{Draft}{Under composition}
688\lineiii{F}{Flagged}{Marked as important}
689\lineiii{P}{Passed}{Forwarded, resent, or bounced}
690\lineiii{R}{Replied}{Replied to}
691\lineiii{S}{Seen}{Read}
692\lineiii{T}{Trashed}{Marked for subsequent deletion}
693\end{tableiii}
694
695\class{MaildirMessage} instances offer the following methods:
696
697\begin{methoddesc}{get_subdir}{}
698Return either "new" (if the message should be stored in the \file{new}
699subdirectory) or "cur" (if the message should be stored in the \file{cur}
700subdirectory). \note{A message is typically moved from \file{new} to \file{cur}
701after its mailbox has been accessed, whether or not the message is has been
702read. A message \code{msg} has been read if \code{"S" not in msg.get_flags()}
703is \code{True}.}
704\end{methoddesc}
705
706\begin{methoddesc}{set_subdir}{subdir}
707Set the subdirectory the message should be stored in. Parameter \var{subdir}
708must be either "new" or "cur".
709\end{methoddesc}
710
711\begin{methoddesc}{get_flags}{}
712Return a string specifying the flags that are currently set. If the message
713complies with the standard Maildir format, the result is the concatenation in
714alphabetical order of zero or one occurrence of each of \character{D},
715\character{F}, \character{P}, \character{R}, \character{S}, and \character{T}.
716The empty string is returned if no flags are set or if "info" contains
717experimental semantics.
718\end{methoddesc}
719
720\begin{methoddesc}{set_flags}{flags}
721Set the flags specified by \var{flags} and unset all others.
722\end{methoddesc}
723
724\begin{methoddesc}{add_flag}{flag}
725Set the flag(s) specified by \var{flag} without changing other flags. To add
726more than one flag at a time, \var{flag} may be a string of more than one
727character. The current "info" is overwritten whether or not it contains
728experimental information rather than
729flags.
730\end{methoddesc}
731
732\begin{methoddesc}{remove_flag}{flag}
733Unset the flag(s) specified by \var{flag} without changing other flags. To
734remove more than one flag at a time, \var{flag} maybe a string of more than one
735character. If "info" contains experimental information rather than flags, the
736current "info" is not modified.
737\end{methoddesc}
738
739\begin{methoddesc}{get_date}{}
740Return the delivery date of the message as a floating-point number representing
741seconds since the epoch.
742\end{methoddesc}
743
744\begin{methoddesc}{set_date}{date}
745Set the delivery date of the message to \var{date}, a floating-point number
746representing seconds since the epoch.
747\end{methoddesc}
748
749\begin{methoddesc}{get_info}{}
750Return a string containing the "info" for a message. This is useful for
751accessing and modifying "info" that is experimental (i.e., not a list of
752flags).
753\end{methoddesc}
754
755\begin{methoddesc}{set_info}{info}
756Set "info" to \var{info}, which should be a string.
757\end{methoddesc}
758
759When a \class{MaildirMessage} instance is created based upon an
760\class{mboxMessage} or \class{MMDFMessage} instance, the \mailheader{Status}
761and \mailheader{X-Status} headers are omitted and the following conversions
762take place:
763
764\begin{tableii}{l|l}{textrm}
765 {Resulting state}{\class{mboxMessage} or \class{MMDFMessage} state}
766\lineii{"cur" subdirectory}{O flag}
767\lineii{F flag}{F flag}
768\lineii{R flag}{A flag}
769\lineii{S flag}{R flag}
770\lineii{T flag}{D flag}
771\end{tableii}
772
773When a \class{MaildirMessage} instance is created based upon an
774\class{MHMessage} instance, the following conversions take place:
775
776\begin{tableii}{l|l}{textrm}
777 {Resulting state}{\class{MHMessage} state}
778\lineii{"cur" subdirectory}{"unseen" sequence}
779\lineii{"cur" subdirectory and S flag}{no "unseen" sequence}
780\lineii{F flag}{"flagged" sequence}
781\lineii{R flag}{"replied" sequence}
782\end{tableii}
783
784When a \class{MaildirMessage} instance is created based upon a
785\class{BabylMessage} instance, the following conversions take place:
786
787\begin{tableii}{l|l}{textrm}
788 {Resulting state}{\class{BabylMessage} state}
789\lineii{"cur" subdirectory}{"unseen" label}
790\lineii{"cur" subdirectory and S flag}{no "unseen" label}
791\lineii{P flag}{"forwarded" or "resent" label}
792\lineii{R flag}{"answered" label}
793\lineii{T flag}{"deleted" label}
794\end{tableii}
795
796\subsubsection{\class{mboxMessage}}
797\label{mailbox-mboxmessage}
798
799\begin{classdesc}{mboxMessage}{\optional{message}}
800A message with mbox-specific behaviors. Parameter \var{message} has the same
801meaning as with the \class{Message} constructor.
802\end{classdesc}
803
804Messages in an mbox mailbox are stored together in a single file. The sender's
805envelope address and the time of delivery are typically stored in a line
806beginning with "From~" that is used to indicate the start of a message, though
807there is considerable variation in the exact format of this data among mbox
808implementations. Flags that indicate the state of the message, such as whether
809it has been read or marked as important, are typically stored in
810\mailheader{Status} and \mailheader{X-Status} headers.
811
812Conventional flags for mbox messages are as follows:
813
814\begin{tableiii}{l|l|l}{textrm}{Flag}{Meaning}{Explanation}
815\lineiii{R}{Read}{Read}
816\lineiii{O}{Old}{Previously detected by MUA}
817\lineiii{D}{Deleted}{Marked for subsequent deletion}
818\lineiii{F}{Flagged}{Marked as important}
819\lineiii{A}{Answered}{Replied to}
820\end{tableiii}
821
822The "R" and "O" flags are stored in the \mailheader{Status} header, and the
823"D", "F", and "A" flags are stored in the \mailheader{X-Status} header. The
824flags and headers typically appear in the order mentioned.
825
826\class{mboxMessage} instances offer the following methods:
827
828\begin{methoddesc}{get_from}{}
829Return a string representing the "From~" line that marks the start of the
830message in an mbox mailbox. The leading "From~" and the trailing newline are
831excluded.
832\end{methoddesc}
833
834\begin{methoddesc}{set_from}{from_\optional{, time_=None}}
835Set the "From~" line to \var{from_}, which should be specified without a
836leading "From~" or trailing newline. For convenience, \var{time_} may be
837specified and will be formatted appropriately and appended to \var{from_}. If
838\var{time_} is specified, it should be a \class{struct_time} instance, a tuple
839suitable for passing to \method{time.strftime()}, or \code{True} (to use
840\method{time.gmtime()}).
841\end{methoddesc}
842
843\begin{methoddesc}{get_flags}{}
844Return a string specifying the flags that are currently set. If the message
845complies with the conventional format, the result is the concatenation in the
846following order of zero or one occurrence of each of \character{R},
847\character{O}, \character{D}, \character{F}, and \character{A}.
848\end{methoddesc}
849
850\begin{methoddesc}{set_flags}{flags}
851Set the flags specified by \var{flags} and unset all others. Parameter
852\var{flags} should be the concatenation in any order of zero or more
853occurrences of each of \character{R}, \character{O}, \character{D},
854\character{F}, and \character{A}.
855\end{methoddesc}
856
857\begin{methoddesc}{add_flag}{flag}
858Set the flag(s) specified by \var{flag} without changing other flags. To add
859more than one flag at a time, \var{flag} may be a string of more than one
860character.
861\end{methoddesc}
862
863\begin{methoddesc}{remove_flag}{flag}
864Unset the flag(s) specified by \var{flag} without changing other flags. To
865remove more than one flag at a time, \var{flag} maybe a string of more than one
866character.
867\end{methoddesc}
868
869When an \class{mboxMessage} instance is created based upon a
870\class{MaildirMessage} instance, a "From~" line is generated based upon the
871\class{MaildirMessage} instance's delivery date, and the following conversions
872take place:
873
874\begin{tableii}{l|l}{textrm}
875 {Resulting state}{\class{MaildirMessage} state}
876\lineii{R flag}{S flag}
877\lineii{O flag}{"cur" subdirectory}
878\lineii{D flag}{T flag}
879\lineii{F flag}{F flag}
880\lineii{A flag}{R flag}
881\end{tableii}
882
883When an \class{mboxMessage} instance is created based upon an \class{MHMessage}
884instance, the following conversions take place:
885
886\begin{tableii}{l|l}{textrm}
887 {Resulting state}{\class{MHMessage} state}
888\lineii{R flag and O flag}{no "unseen" sequence}
889\lineii{O flag}{"unseen" sequence}
890\lineii{F flag}{"flagged" sequence}
891\lineii{A flag}{"replied" sequence}
892\end{tableii}
893
894When an \class{mboxMessage} instance is created based upon a
895\class{BabylMessage} instance, the following conversions take place:
896
897\begin{tableii}{l|l}{textrm}
898 {Resulting state}{\class{BabylMessage} state}
899\lineii{R flag and O flag}{no "unseen" label}
900\lineii{O flag}{"unseen" label}
901\lineii{D flag}{"deleted" label}
902\lineii{A flag}{"answered" label}
903\end{tableii}
904
905When a \class{Message} instance is created based upon an \class{MMDFMessage}
906instance, the "From~" line is copied and all flags directly correspond:
907
908\begin{tableii}{l|l}{textrm}
909 {Resulting state}{\class{MMDFMessage} state}
910\lineii{R flag}{R flag}
911\lineii{O flag}{O flag}
912\lineii{D flag}{D flag}
913\lineii{F flag}{F flag}
914\lineii{A flag}{A flag}
915\end{tableii}
916
917\subsubsection{\class{MHMessage}}
918\label{mailbox-mhmessage}
919
920\begin{classdesc}{MHMessage}{\optional{message}}
921A message with MH-specific behaviors. Parameter \var{message} has the same
922meaning as with the \class{Message} constructor.
923\end{classdesc}
924
925MH messages do not support marks or flags in the traditional sense, but they do
926support sequences, which are logical groupings of arbitrary messages. Some mail
927reading programs (although not the standard \program{mh} and \program{nmh}) use
928sequences in much the same way flags are used with other formats, as follows:
929
930\begin{tableii}{l|l}{textrm}{Sequence}{Explanation}
931\lineii{unseen}{Not read, but previously detected by MUA}
932\lineii{replied}{Replied to}
933\lineii{flagged}{Marked as important}
934\end{tableii}
935
936\class{MHMessage} instances offer the following methods:
937
938\begin{methoddesc}{get_sequences}{}
939Return a list of the names of sequences that include this message.
940\end{methoddesc}
941
942\begin{methoddesc}{set_sequences}{sequences}
943Set the list of sequences that include this message.
944\end{methoddesc}
945
946\begin{methoddesc}{add_sequence}{sequence}
947Add \var{sequence} to the list of sequences that include this message.
948\end{methoddesc}
949
950\begin{methoddesc}{remove_sequence}{sequence}
951Remove \var{sequence} from the list of sequences that include this message.
952\end{methoddesc}
953
954When an \class{MHMessage} instance is created based upon a
955\class{MaildirMessage} instance, the following conversions take place:
956
957\begin{tableii}{l|l}{textrm}
958 {Resulting state}{\class{MaildirMessage} state}
959\lineii{"unseen" sequence}{no S flag}
960\lineii{"replied" sequence}{R flag}
961\lineii{"flagged" sequence}{F flag}
962\end{tableii}
963
964When an \class{MHMessage} instance is created based upon an \class{mboxMessage}
965or \class{MMDFMessage} instance, the \mailheader{Status} and
966\mailheader{X-Status} headers are omitted and the following conversions take
967place:
968
969\begin{tableii}{l|l}{textrm}
970 {Resulting state}{\class{mboxMessage} or \class{MMDFMessage} state}
971\lineii{"unseen" sequence}{no R flag}
972\lineii{"replied" sequence}{A flag}
973\lineii{"flagged" sequence}{F flag}
974\end{tableii}
975
976When an \class{MHMessage} instance is created based upon a \class{BabylMessage}
977instance, the following conversions take place:
978
979\begin{tableii}{l|l}{textrm}
980 {Resulting state}{\class{BabylMessage} state}
981\lineii{"unseen" sequence}{"unseen" label}
982\lineii{"replied" sequence}{"answered" label}
983\end{tableii}
984
985\subsubsection{\class{BabylMessage}}
986\label{mailbox-babylmessage}
987
988\begin{classdesc}{BabylMessage}{\optional{message}}
989A message with Babyl-specific behaviors. Parameter \var{message} has the same
990meaning as with the \class{Message} constructor.
991\end{classdesc}
992
993Certain message labels, called \dfn{attributes}, are defined by convention to
994have special meanings. The attributes are as follows:
995
996\begin{tableii}{l|l}{textrm}{Label}{Explanation}
997\lineii{unseen}{Not read, but previously detected by MUA}
998\lineii{deleted}{Marked for subsequent deletion}
999\lineii{filed}{Copied to another file or mailbox}
1000\lineii{answered}{Replied to}
1001\lineii{forwarded}{Forwarded}
1002\lineii{edited}{Modified by the user}
1003\lineii{resent}{Resent}
1004\end{tableii}
1005
1006By default, Rmail displays only
1007visible headers. The \class{BabylMessage} class, though, uses the original
1008headers because they are more complete. Visible headers may be accessed
1009explicitly if desired.
1010
1011\class{BabylMessage} instances offer the following methods:
1012
1013\begin{methoddesc}{get_labels}{}
1014Return a list of labels on the message.
1015\end{methoddesc}
1016
1017\begin{methoddesc}{set_labels}{labels}
1018Set the list of labels on the message to \var{labels}.
1019\end{methoddesc}
1020
1021\begin{methoddesc}{add_label}{label}
1022Add \var{label} to the list of labels on the message.
1023\end{methoddesc}
1024
1025\begin{methoddesc}{remove_label}{label}
1026Remove \var{label} from the list of labels on the message.
1027\end{methoddesc}
1028
1029\begin{methoddesc}{get_visible}{}
1030Return an \class{Message} instance whose headers are the message's visible
1031headers and whose body is empty.
1032\end{methoddesc}
1033
1034\begin{methoddesc}{set_visible}{visible}
1035Set the message's visible headers to be the same as the headers in
1036\var{message}. Parameter \var{visible} should be a \class{Message} instance, an
1037\class{email.Message.Message} instance, a string, or a file-like object (which
1038should be open in text mode).
1039\end{methoddesc}
1040
1041\begin{methoddesc}{update_visible}{}
1042When a \class{BabylMessage} instance's original headers are modified, the
1043visible headers are not automatically modified to correspond. This method
1044updates the visible headers as follows: each visible header with a
1045corresponding original header is set to the value of the original header, each
1046visible header without a corresponding original header is removed, and any of
1047\mailheader{Date}, \mailheader{From}, \mailheader{Reply-To}, \mailheader{To},
1048\mailheader{CC}, and \mailheader{Subject} that are present in the original
1049headers but not the visible headers are added to the visible headers.
1050\end{methoddesc}
1051
1052When a \class{BabylMessage} instance is created based upon a
1053\class{MaildirMessage} instance, the following conversions take place:
1054
1055\begin{tableii}{l|l}{textrm}
1056 {Resulting state}{\class{MaildirMessage} state}
1057\lineii{"unseen" label}{no S flag}
1058\lineii{"deleted" label}{T flag}
1059\lineii{"answered" label}{R flag}
1060\lineii{"forwarded" label}{P flag}
1061\end{tableii}
1062
1063When a \class{BabylMessage} instance is created based upon an
1064\class{mboxMessage} or \class{MMDFMessage} instance, the \mailheader{Status}
1065and \mailheader{X-Status} headers are omitted and the following conversions
1066take place:
1067
1068\begin{tableii}{l|l}{textrm}
1069 {Resulting state}{\class{mboxMessage} or \class{MMDFMessage} state}
1070\lineii{"unseen" label}{no R flag}
1071\lineii{"deleted" label}{D flag}
1072\lineii{"answered" label}{A flag}
1073\end{tableii}
1074
1075When a \class{BabylMessage} instance is created based upon an \class{MHMessage}
1076instance, the following conversions take place:
1077
1078\begin{tableii}{l|l}{textrm}
1079 {Resulting state}{\class{MHMessage} state}
1080\lineii{"unseen" label}{"unseen" sequence}
1081\lineii{"answered" label}{"replied" sequence}
1082\end{tableii}
1083
1084\subsubsection{\class{MMDFMessage}}
1085\label{mailbox-mmdfmessage}
1086
1087\begin{classdesc}{MMDFMessage}{\optional{message}}
1088A message with MMDF-specific behaviors. Parameter \var{message} has the same
1089meaning as with the \class{Message} constructor.
1090\end{classdesc}
1091
1092As with message in an mbox mailbox, MMDF messages are stored with the sender's
1093address and the delivery date in an initial line beginning with "From ".
1094Likewise, flags that indicate the state of the message are typically stored in
1095\mailheader{Status} and \mailheader{X-Status} headers.
1096
1097Conventional flags for MMDF messages are identical to those of mbox message and
1098are as follows:
1099
1100\begin{tableiii}{l|l|l}{textrm}{Flag}{Meaning}{Explanation}
1101\lineiii{R}{Read}{Read}
1102\lineiii{O}{Old}{Previously detected by MUA}
1103\lineiii{D}{Deleted}{Marked for subsequent deletion}
1104\lineiii{F}{Flagged}{Marked as important}
1105\lineiii{A}{Answered}{Replied to}
1106\end{tableiii}
1107
1108The "R" and "O" flags are stored in the \mailheader{Status} header, and the
1109"D", "F", and "A" flags are stored in the \mailheader{X-Status} header. The
1110flags and headers typically appear in the order mentioned.
1111
1112\class{MMDFMessage} instances offer the following methods, which are identical
1113to those offered by \class{mboxMessage}:
1114
1115\begin{methoddesc}{get_from}{}
1116Return a string representing the "From~" line that marks the start of the
1117message in an mbox mailbox. The leading "From~" and the trailing newline are
1118excluded.
1119\end{methoddesc}
1120
1121\begin{methoddesc}{set_from}{from_\optional{, time_=None}}
1122Set the "From~" line to \var{from_}, which should be specified without a
1123leading "From~" or trailing newline. For convenience, \var{time_} may be
1124specified and will be formatted appropriately and appended to \var{from_}. If
1125\var{time_} is specified, it should be a \class{struct_time} instance, a tuple
1126suitable for passing to \method{time.strftime()}, or \code{True} (to use
1127\method{time.gmtime()}).
1128\end{methoddesc}
1129
1130\begin{methoddesc}{get_flags}{}
1131Return a string specifying the flags that are currently set. If the message
1132complies with the conventional format, the result is the concatenation in the
1133following order of zero or one occurrence of each of \character{R},
1134\character{O}, \character{D}, \character{F}, and \character{A}.
1135\end{methoddesc}
1136
1137\begin{methoddesc}{set_flags}{flags}
1138Set the flags specified by \var{flags} and unset all others. Parameter
1139\var{flags} should be the concatenation in any order of zero or more
1140occurrences of each of \character{R}, \character{O}, \character{D},
1141\character{F}, and \character{A}.
1142\end{methoddesc}
1143
1144\begin{methoddesc}{add_flag}{flag}
1145Set the flag(s) specified by \var{flag} without changing other flags. To add
1146more than one flag at a time, \var{flag} may be a string of more than one
1147character.
1148\end{methoddesc}
1149
1150\begin{methoddesc}{remove_flag}{flag}
1151Unset the flag(s) specified by \var{flag} without changing other flags. To
1152remove more than one flag at a time, \var{flag} maybe a string of more than one
1153character.
1154\end{methoddesc}
1155
1156When an \class{MMDFMessage} instance is created based upon a
1157\class{MaildirMessage} instance, a "From~" line is generated based upon the
1158\class{MaildirMessage} instance's delivery date, and the following conversions
1159take place:
1160
1161\begin{tableii}{l|l}{textrm}
1162 {Resulting state}{\class{MaildirMessage} state}
1163\lineii{R flag}{S flag}
1164\lineii{O flag}{"cur" subdirectory}
1165\lineii{D flag}{T flag}
1166\lineii{F flag}{F flag}
1167\lineii{A flag}{R flag}
1168\end{tableii}
1169
1170When an \class{MMDFMessage} instance is created based upon an \class{MHMessage}
1171instance, the following conversions take place:
1172
1173\begin{tableii}{l|l}{textrm}
1174 {Resulting state}{\class{MHMessage} state}
1175\lineii{R flag and O flag}{no "unseen" sequence}
1176\lineii{O flag}{"unseen" sequence}
1177\lineii{F flag}{"flagged" sequence}
1178\lineii{A flag}{"replied" sequence}
1179\end{tableii}
1180
1181When an \class{MMDFMessage} instance is created based upon a
1182\class{BabylMessage} instance, the following conversions take place:
1183
1184\begin{tableii}{l|l}{textrm}
1185 {Resulting state}{\class{BabylMessage} state}
1186\lineii{R flag and O flag}{no "unseen" label}
1187\lineii{O flag}{"unseen" label}
1188\lineii{D flag}{"deleted" label}
1189\lineii{A flag}{"answered" label}
1190\end{tableii}
1191
1192When an \class{MMDFMessage} instance is created based upon an
1193\class{mboxMessage} instance, the "From~" line is copied and all flags directly
1194correspond:
1195
1196\begin{tableii}{l|l}{textrm}
1197 {Resulting state}{\class{mboxMessage} state}
1198\lineii{R flag}{R flag}
1199\lineii{O flag}{O flag}
1200\lineii{D flag}{D flag}
1201\lineii{F flag}{F flag}
1202\lineii{A flag}{A flag}
1203\end{tableii}
1204
1205\subsection{Exceptions}
1206\label{mailbox-deprecated}
1207
1208The following exception classes are defined in the \module{mailbox} module:
1209
1210\begin{classdesc}{Error}{}
1211The based class for all other module-specific exceptions.
1212\end{classdesc}
1213
1214\begin{classdesc}{NoSuchMailboxError}{}
1215Raised when a mailbox is expected but is not found, such as when instantiating
1216a \class{Mailbox} subclass with a path that does not exist (and with the
1217\var{create} parameter set to \code{False}), or when opening a folder that does
1218not exist.
1219\end{classdesc}
1220
1221\begin{classdesc}{NotEmptyErrorError}{}
1222Raised when a mailbox is not empty but is expected to be, such as when deleting
1223a folder that contains messages.
1224\end{classdesc}
1225
1226\begin{classdesc}{ExternalClashError}{}
1227Raised when some mailbox-related condition beyond the control of the program
1228causes it to be unable to proceed, such as when failing to acquire a lock that
1229another program already holds a lock, or when a uniquely-generated file name
1230already exists.
1231\end{classdesc}
1232
1233\begin{classdesc}{FormatError}{}
1234Raised when the data in a file cannot be parsed, such as when an \class{MH}
1235instance attempts to read a corrupted \file{.mh_sequences} file.
1236\end{classdesc}
1237
1238\subsection{Deprecated classes and methods}
1239\label{mailbox-deprecated}
1240
1241Older versions of the \module{mailbox} module do not support modification of
1242mailboxes, such as adding or removing message, and do not provide classes to
1243represent format-specific message properties. For backward compatibility, the
1244older mailbox classes are still available, but the newer classes should be used
1245in preference to them.
1246
1247Older mailbox objects support only iteration and provide a single public
1248method:
1249
1250\begin{methoddesc}{next}{}
1251Return the next message in the mailbox, created with the optional \var{factory}
1252argument passed into the mailbox object's constructor. By default this is an
1253\class{rfc822.Message} object (see the \refmodule{rfc822} module). Depending
1254on the mailbox implementation the \var{fp} attribute of this object may be a
1255true file object or a class instance simulating a file object, taking care of
1256things like message boundaries if multiple mail messages are contained in a
1257single file, etc. If no more messages are available, this method returns
1258\code{None}.
1259\end{methoddesc}
1260
1261Most of the older mailbox classes have names that differ from the current
1262mailbox class names, except for \class{Maildir}. For this reason, the new
1263\class{Maildir} class defines a \method{next()} method and its constructor
1264differs slightly from those of the other new mailbox classes.
1265
1266The older mailbox classes whose names are not the same as their newer
1267counterparts are as follows:
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001268
Barry Warsaw30dbd142001-01-31 22:14:01 +00001269\begin{classdesc}{UnixMailbox}{fp\optional{, factory}}
Fred Drake62700312001-02-02 03:51:05 +00001270Access to a classic \UNIX-style mailbox, where all messages are
1271contained in a single file and separated by \samp{From }
1272(a.k.a.\ \samp{From_}) lines. The file object \var{fp} points to the
1273mailbox file. The optional \var{factory} parameter is a callable that
1274should create new message objects. \var{factory} is called with one
1275argument, \var{fp} by the \method{next()} method of the mailbox
1276object. The default is the \class{rfc822.Message} class (see the
Barry Warsaw47db2522003-06-20 22:04:03 +00001277\refmodule{rfc822} module -- and the note below).
Barry Warsaw30dbd142001-01-31 22:14:01 +00001278
Fred Drake0d736212004-05-11 05:29:34 +00001279\begin{notice}
1280 For reasons of this module's internal implementation, you will
1281 probably want to open the \var{fp} object in binary mode. This is
1282 especially important on Windows.
1283\end{notice}
Barry Warsawdd69b0a2004-05-10 23:12:52 +00001284
Fred Drake62700312001-02-02 03:51:05 +00001285For maximum portability, messages in a \UNIX-style mailbox are
1286separated by any line that begins exactly with the string \code{'From
1287'} (note the trailing space) if preceded by exactly two newlines.
1288Because of the wide-range of variations in practice, nothing else on
1289the From_ line should be considered. However, the current
1290implementation doesn't check for the leading two newlines. This is
1291usually fine for most applications.
Barry Warsaw30dbd142001-01-31 22:14:01 +00001292
1293The \class{UnixMailbox} class implements a more strict version of
1294From_ line checking, using a regular expression that usually correctly
1295matched From_ delimiters. It considers delimiter line to be separated
Fred Drake62700312001-02-02 03:51:05 +00001296by \samp{From \var{name} \var{time}} lines. For maximum portability,
1297use the \class{PortableUnixMailbox} class instead. This class is
1298identical to \class{UnixMailbox} except that individual messages are
1299separated by only \samp{From } lines.
Barry Warsaw30dbd142001-01-31 22:14:01 +00001300
Fred Drake62700312001-02-02 03:51:05 +00001301For more information, see
1302\citetitle[http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html]{Configuring
1303Netscape Mail on \UNIX: Why the Content-Length Format is Bad}.
1304\end{classdesc}
1305
1306\begin{classdesc}{PortableUnixMailbox}{fp\optional{, factory}}
1307A less-strict version of \class{UnixMailbox}, which considers only the
1308\samp{From } at the beginning of the line separating messages. The
1309``\var{name} \var{time}'' portion of the From line is ignored, to
1310protect against some variations that are observed in practice. This
1311works since lines in the message which begin with \code{'From '} are
Greg Ward02669a32002-09-23 19:32:42 +00001312quoted by mail handling software at delivery-time.
Fred Drake2e495c91998-03-14 06:48:33 +00001313\end{classdesc}
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001314
Barry Warsaw30dbd142001-01-31 22:14:01 +00001315\begin{classdesc}{MmdfMailbox}{fp\optional{, factory}}
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001316Access an MMDF-style mailbox, where all messages are contained
1317in a single file and separated by lines consisting of 4 control-A
Fred Drake6e99adb1998-02-13 22:17:21 +00001318characters. The file object \var{fp} points to the mailbox file.
Barry Warsaw30dbd142001-01-31 22:14:01 +00001319Optional \var{factory} is as with the \class{UnixMailbox} class.
Fred Drake2e495c91998-03-14 06:48:33 +00001320\end{classdesc}
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001321
Barry Warsaw30dbd142001-01-31 22:14:01 +00001322\begin{classdesc}{MHMailbox}{dirname\optional{, factory}}
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001323Access an MH mailbox, a directory with each message in a separate
Fred Drake6e99adb1998-02-13 22:17:21 +00001324file with a numeric name.
1325The name of the mailbox directory is passed in \var{dirname}.
Barry Warsaw30dbd142001-01-31 22:14:01 +00001326\var{factory} is as with the \class{UnixMailbox} class.
Fred Drake2e495c91998-03-14 06:48:33 +00001327\end{classdesc}
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001328
Barry Warsaw30dbd142001-01-31 22:14:01 +00001329\begin{classdesc}{BabylMailbox}{fp\optional{, factory}}
Barry Warsawc3cbbaf2001-04-11 20:12:33 +00001330Access a Babyl mailbox, which is similar to an MMDF mailbox. In
1331Babyl format, each message has two sets of headers, the
1332\emph{original} headers and the \emph{visible} headers. The original
Raymond Hettinger999b57c2003-08-25 04:28:05 +00001333headers appear before a line containing only \code{'*** EOOH ***'}
Barry Warsawc3cbbaf2001-04-11 20:12:33 +00001334(End-Of-Original-Headers) and the visible headers appear after the
1335\code{EOOH} line. Babyl-compliant mail readers will show you only the
1336visible headers, and \class{BabylMailbox} objects will return messages
1337containing only the visible headers. You'll have to do your own
1338parsing of the mailbox file to get at the original headers. Mail
1339messages start with the EOOH line and end with a line containing only
1340\code{'\e{}037\e{}014'}. \var{factory} is as with the
1341\class{UnixMailbox} class.
Fred Drake199b79c1999-02-20 05:04:59 +00001342\end{classdesc}
1343
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001344If you wish to use the older mailbox classes with the \module{email} module
1345rather than the deprecated \module{rfc822} module, you can do so as follows:
Barry Warsaw47db2522003-06-20 22:04:03 +00001346
1347\begin{verbatim}
1348import email
1349import email.Errors
1350import mailbox
1351
1352def msgfactory(fp):
1353 try:
1354 return email.message_from_file(fp)
1355 except email.Errors.MessageParseError:
1356 # Don't return None since that will
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001357 # stop the mailbox iterator
1358 return ''
Barry Warsaw47db2522003-06-20 22:04:03 +00001359
1360mbox = mailbox.UnixMailbox(fp, msgfactory)
1361\end{verbatim}
1362
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001363Alternatively, if you know your mailbox contains only well-formed MIME
1364messages, you can simplify this to:
Barry Warsaw47db2522003-06-20 22:04:03 +00001365
1366\begin{verbatim}
1367import email
1368import mailbox
1369
1370mbox = mailbox.UnixMailbox(fp, email.message_from_file)
1371\end{verbatim}
Fred Drake199b79c1999-02-20 05:04:59 +00001372
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001373\subsection{Examples}
1374\label{mailbox-examples}
Fred Drake1400baa2001-05-21 21:23:01 +00001375
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001376A simple example of printing the subjects of all messages in a mailbox that
1377seem interesting:
Fred Drake1400baa2001-05-21 21:23:01 +00001378
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001379\begin{verbatim}
1380import mailbox
1381for message in mailbox.mbox('~/mbox'):
1382 subject = message['subject'] # Could possibly be None.
1383 if subject and 'python' in subject.lower():
1384 print subject
1385\end{verbatim}
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001386
Andrew M. Kuchling4b6d5372006-07-27 22:40:05 +00001387To copy all mail from a Babyl mailbox to an MH mailbox, converting all
1388of the format-specific information that can be converted:
Guido van Rossum39a23cc1997-06-02 21:04:41 +00001389
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001390\begin{verbatim}
1391import mailbox
1392destination = mailbox.MH('~/Mail')
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +00001393destination.lock()
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001394for message in mailbox.Babyl('~/RMAIL'):
1395 destination.add(MHMessage(message))
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +00001396destination.flush()
1397destination.unlock()
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001398\end{verbatim}
1399
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +00001400This example sorts mail from several mailing lists into different
1401mailboxes, being careful to avoid mail corruption due to concurrent
1402modification by other programs, mail loss due to interruption of the
1403program, or premature termination due to malformed messages in the
1404mailbox:
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001405
1406\begin{verbatim}
1407import mailbox
1408import email.Errors
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +00001409
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001410list_names = ('python-list', 'python-dev', 'python-bugs')
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +00001411
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001412boxes = dict((name, mailbox.mbox('~/email/%s' % name)) for name in list_names)
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +00001413inbox = mailbox.Maildir('~/Maildir', factory=None)
1414
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001415for key in inbox.iterkeys():
1416 try:
1417 message = inbox[key]
1418 except email.Errors.MessageParseError:
1419 continue # The message is malformed. Just leave it.
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +00001420
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001421 for name in list_names:
1422 list_id = message['list-id']
1423 if list_id and name in list_id:
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +00001424 # Get mailbox to use
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001425 box = boxes[name]
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +00001426
1427 # Write copy to disk before removing original.
1428 # If there's a crash, you might duplicate a message, but
1429 # that's better than losing a message completely.
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001430 box.lock()
1431 box.add(message)
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +00001432 box.flush()
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001433 box.unlock()
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +00001434
1435 # Remove original message
1436 inbox.lock()
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001437 inbox.discard(key)
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +00001438 inbox.flush()
1439 inbox.unlock()
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001440 break # Found destination, so stop looking.
Andrew M. Kuchlingdfad6142007-01-17 19:56:06 +00001441
Andrew M. Kuchling1da4a942006-04-22 02:32:43 +00001442for box in boxes.itervalues():
1443 box.close()
1444\end{verbatim}