Minor fix for currentframe (SF #1652788).
[python.git] / Doc / lib / libmailbox.tex
blob24765c8d3ea1c20eab34b6b3d90c609841b5aa32
1 \section{\module{mailbox} ---
2 Manipulate mailboxes in various formats}
4 \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}
10 This module defines two classes, \class{Mailbox} and \class{Message}, for
11 accessing 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}
14 class with format-specific state and behavior. Supported mailbox formats are
15 Maildir, mbox, MH, Babyl, and MMDF.
17 \begin{seealso}
18 \seemodule{email}{Represent and manipulate messages.}
19 \end{seealso}
21 \subsection{\class{Mailbox} objects}
22 \label{mailbox-objects}
24 \begin{classdesc*}{Mailbox}
25 A mailbox, which may be inspected and modified.
26 \end{classdesc*}
28 The \class{Mailbox} class defines an interface and
29 is not intended to be instantiated. Instead, format-specific
30 subclasses should inherit from \class{Mailbox} and your code
31 should instantiate a particular subclass.
33 The \class{Mailbox} interface is dictionary-like, with small keys
34 corresponding to messages. Keys are issued by the \class{Mailbox}
35 instance with which they will be used and are only meaningful to that
36 \class{Mailbox} instance. A key continues to identify a message even
37 if the corresponding message is modified, such as by replacing it with
38 another message.
40 Messages may be added to a \class{Mailbox} instance using the set-like
41 method \method{add()} and removed using a \code{del} statement or the
42 set-like methods \method{remove()} and \method{discard()}.
44 \class{Mailbox} interface semantics differ from dictionary semantics in some
45 noteworthy ways. Each time a message is requested, a new
46 representation (typically a \class{Message} instance) is generated
47 based upon the current state of the mailbox. Similarly, when a message
48 is added to a \class{Mailbox} instance, the provided message
49 representation's contents are copied. In neither case is a reference
50 to the message representation kept by the \class{Mailbox} instance.
52 The default \class{Mailbox} iterator iterates over message representations, not
53 keys as the default dictionary iterator does. Moreover, modification of a
54 mailbox during iteration is safe and well-defined. Messages added to the
55 mailbox after an iterator is created will not be seen by the iterator. Messages
56 removed from the mailbox before the iterator yields them will be silently
57 skipped, though using a key from an iterator may result in a
58 \exception{KeyError} exception if the corresponding message is subsequently
59 removed.
61 \begin{notice}[warning]
62 Be very cautious when modifying mailboxes that might be
63 simultaneously changed by some other process. The safest mailbox
64 format to use for such tasks is Maildir; try to avoid using
65 single-file formats such as mbox for concurrent writing. If you're
66 modifying 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
69 or making any changes by adding or deleting a message. Failing to
70 lock the mailbox runs the risk of losing messages or corrupting the entire
71 mailbox.
72 \end{notice}
74 \class{Mailbox} instances have the following methods:
76 \begin{methoddesc}{add}{message}
77 Add \var{message} to the mailbox and return the key that has been assigned to
78 it.
80 Parameter \var{message} may be a \class{Message} instance, an
81 \class{email.Message.Message} instance, a string, or a file-like object (which
82 should be open in text mode). If \var{message} is an instance of the
83 appropriate format-specific \class{Message} subclass (e.g., if it's an
84 \class{mboxMessage} instance and this is an \class{mbox} instance), its
85 format-specific information is used. Otherwise, reasonable defaults for
86 format-specific information are used.
87 \end{methoddesc}
89 \begin{methoddesc}{remove}{key}
90 \methodline{__delitem__}{key}
91 \methodline{discard}{key}
92 Delete the message corresponding to \var{key} from the mailbox.
94 If no such message exists, a \exception{KeyError} exception is raised if the
95 method was called as \method{remove()} or \method{__delitem__()} but no
96 exception is raised if the method was called as \method{discard()}. The
97 behavior of \method{discard()} may be preferred if the underlying mailbox
98 format supports concurrent modification by other processes.
99 \end{methoddesc}
101 \begin{methoddesc}{__setitem__}{key, message}
102 Replace the message corresponding to \var{key} with \var{message}. Raise a
103 \exception{KeyError} exception if no message already corresponds to \var{key}.
105 As with \method{add()}, parameter \var{message} may be a \class{Message}
106 instance, an \class{email.Message.Message} instance, a string, or a file-like
107 object (which should be open in text mode). If \var{message} is an instance of
108 the appropriate format-specific \class{Message} subclass (e.g., if it's an
109 \class{mboxMessage} instance and this is an \class{mbox} instance), its
110 format-specific information is used. Otherwise, the format-specific information
111 of the message that currently corresponds to \var{key} is left unchanged.
112 \end{methoddesc}
114 \begin{methoddesc}{iterkeys}{}
115 \methodline{keys}{}
116 Return an iterator over all keys if called as \method{iterkeys()} or return a
117 list of keys if called as \method{keys()}.
118 \end{methoddesc}
120 \begin{methoddesc}{itervalues}{}
121 \methodline{__iter__}{}
122 \methodline{values}{}
123 Return an iterator over representations of all messages if called as
124 \method{itervalues()} or \method{__iter__()} or return a list of such
125 representations if called as \method{values()}. The messages are represented as
126 instances of the appropriate format-specific \class{Message} subclass unless a
127 custom message factory was specified when the \class{Mailbox} instance was
128 initialized. \note{The behavior of \method{__iter__()} is unlike that of
129 dictionaries, which iterate over keys.}
130 \end{methoddesc}
132 \begin{methoddesc}{iteritems}{}
133 \methodline{items}{}
134 Return an iterator over (\var{key}, \var{message}) pairs, where \var{key} is a
135 key 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
138 format-specific \class{Message} subclass unless a custom message factory was
139 specified when the \class{Mailbox} instance was initialized.
140 \end{methoddesc}
142 \begin{methoddesc}{get}{key\optional{, default=None}}
143 \methodline{__getitem__}{key}
144 Return a representation of the message corresponding to \var{key}. If no such
145 message 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
147 called as \method{__getitem__()}. The message is represented as an instance of
148 the appropriate format-specific \class{Message} subclass unless a custom
149 message factory was specified when the \class{Mailbox} instance was
150 initialized.
151 \end{methoddesc}
153 \begin{methoddesc}{get_message}{key}
154 Return a representation of the message corresponding to \var{key} as an
155 instance of the appropriate format-specific \class{Message} subclass, or raise
156 a \exception{KeyError} exception if no such message exists.
157 \end{methoddesc}
159 \begin{methoddesc}{get_string}{key}
160 Return a string representation of the message corresponding to \var{key}, or
161 raise a \exception{KeyError} exception if no such message exists.
162 \end{methoddesc}
164 \begin{methoddesc}{get_file}{key}
165 Return a file-like representation of the message corresponding to \var{key},
166 or raise a \exception{KeyError} exception if no such message exists. The
167 file-like object behaves as if open in binary mode. This file should be closed
168 once it is no longer needed.
170 \note{Unlike other representations of messages, file-like representations are
171 not necessarily independent of the \class{Mailbox} instance that created them
172 or of the underlying mailbox. More specific documentation is provided by each
173 subclass.}
174 \end{methoddesc}
176 \begin{methoddesc}{has_key}{key}
177 \methodline{__contains__}{key}
178 Return \code{True} if \var{key} corresponds to a message, \code{False}
179 otherwise.
180 \end{methoddesc}
182 \begin{methoddesc}{__len__}{}
183 Return a count of messages in the mailbox.
184 \end{methoddesc}
186 \begin{methoddesc}{clear}{}
187 Delete all messages from the mailbox.
188 \end{methoddesc}
190 \begin{methoddesc}{pop}{key\optional{, default}}
191 Return a representation of the message corresponding to \var{key} and delete
192 the message. If no such message exists, return \var{default} if it was supplied
193 or else raise a \exception{KeyError} exception. The message is represented as
194 an instance of the appropriate format-specific \class{Message} subclass unless
195 a custom message factory was specified when the \class{Mailbox} instance was
196 initialized.
197 \end{methoddesc}
199 \begin{methoddesc}{popitem}{}
200 Return an arbitrary (\var{key}, \var{message}) pair, where \var{key} is a key
201 and \var{message} is a message representation, and delete the corresponding
202 message. If the mailbox is empty, raise a \exception{KeyError} exception. The
203 message 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}
208 \begin{methoddesc}{update}{arg}
209 Parameter \var{arg} should be a \var{key}-to-\var{message} mapping or an
210 iterable of (\var{key}, \var{message}) pairs. Updates the mailbox so that, for
211 each given \var{key} and \var{message}, the message corresponding to \var{key}
212 is 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
214 the mailbox or else a \exception{KeyError} exception will be raised, so in
215 general 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}
219 \begin{methoddesc}{flush}{}
220 Write any pending changes to the filesystem. For some \class{Mailbox}
221 subclasses, changes are always written immediately and \method{flush()} does
222 nothing, but you should still make a habit of calling this method.
223 \end{methoddesc}
225 \begin{methoddesc}{lock}{}
226 Acquire an exclusive advisory lock on the mailbox so that other processes know
227 not to modify it. An \exception{ExternalClashError} is raised if the lock is
228 not available. The particular locking mechanisms used depend upon the mailbox
229 format. You should \emph{always} lock the mailbox before making any
230 modifications to its contents.
231 \end{methoddesc}
233 \begin{methoddesc}{unlock}{}
234 Release the lock on the mailbox, if any.
235 \end{methoddesc}
237 \begin{methoddesc}{close}{}
238 Flush the mailbox, unlock it if necessary, and close any open files. For some
239 \class{Mailbox} subclasses, this method does nothing.
240 \end{methoddesc}
243 \subsubsection{\class{Maildir}}
244 \label{mailbox-maildir}
246 \begin{classdesc}{Maildir}{dirname\optional{, factory=rfc822.Message\optional{,
247 create=True}}}
248 A subclass of \class{Mailbox} for mailboxes in Maildir format. Parameter
249 \var{factory} is a callable object that accepts a file-like message
250 representation (which behaves as if opened in binary mode) and returns a custom
251 representation. If \var{factory} is \code{None}, \class{MaildirMessage} is used
252 as the default message representation. If \var{create} is \code{True}, the
253 mailbox is created if it does not exist.
255 It 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}
261 Maildir is a directory-based mailbox format invented for the qmail mail
262 transfer agent and now widely supported by other programs. Messages in a
263 Maildir mailbox are stored in separate files within a common directory
264 structure. This design allows Maildir mailboxes to be accessed and modified by
265 multiple unrelated programs without data corruption, so file locking is
266 unnecessary.
268 Maildir mailboxes contain three subdirectories, namely: \file{tmp}, \file{new},
269 and \file{cur}. Messages are created momentarily in the \file{tmp} subdirectory
270 and then moved to the \file{new} subdirectory to finalize delivery. A mail user
271 agent may subsequently move the message to the \file{cur} subdirectory and
272 store information about the state of the message in a special "info" section
273 appended to its file name.
275 Folders of the style introduced by the Courier mail transfer agent are also
276 supported. Any subdirectory of the main mailbox is considered a folder if
277 \character{.} is the first character in its name. Folder names are represented
278 by \class{Maildir} without the leading \character{.}. Each folder is itself a
279 Maildir mailbox but should not contain other folders. Instead, a logical
280 nesting is indicated using \character{.} to delimit levels, e.g.,
281 "Archived.2005.07".
283 \begin{notice}
284 The Maildir specification requires the use of a colon (\character{:}) in
285 certain message file names. However, some operating systems do not permit this
286 character in file names, If you wish to use a Maildir-like format on such an
287 operating system, you should specify another character to use instead. The
288 exclamation point (\character{!}) is a popular choice. For example:
289 \begin{verbatim}
290 import mailbox
291 mailbox.Maildir.colon = '!'
292 \end{verbatim}
293 The \member{colon} attribute may also be set on a per-instance basis.
294 \end{notice}
296 \class{Maildir} instances have all of the methods of \class{Mailbox} in
297 addition to the following:
299 \begin{methoddesc}{list_folders}{}
300 Return a list of the names of all folders.
301 \end{methoddesc}
303 \begin{methoddesc}{get_folder}{folder}
304 Return a \class{Maildir} instance representing the folder whose name is
305 \var{folder}. A \exception{NoSuchMailboxError} exception is raised if the
306 folder does not exist.
307 \end{methoddesc}
309 \begin{methoddesc}{add_folder}{folder}
310 Create a folder whose name is \var{folder} and return a \class{Maildir}
311 instance representing it.
312 \end{methoddesc}
314 \begin{methoddesc}{remove_folder}{folder}
315 Delete the folder whose name is \var{folder}. If the folder contains any
316 messages, a \exception{NotEmptyError} exception will be raised and the folder
317 will not be deleted.
318 \end{methoddesc}
320 \begin{methoddesc}{clean}{}
321 Delete temporary files from the mailbox that have not been accessed in the
322 last 36 hours. The Maildir specification says that mail-reading programs
323 should do this occasionally.
324 \end{methoddesc}
326 Some \class{Mailbox} methods implemented by \class{Maildir} deserve special
327 remarks:
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
333 process ID. When using multiple threads, undetected name clashes may occur and
334 cause corruption of the mailbox unless threads are coordinated to avoid using
335 these methods to manipulate the same mailbox simultaneously.}
336 \end{methoddesc}
338 \begin{methoddesc}{flush}{}
339 All changes to Maildir mailboxes are immediately applied, so this method does
340 nothing.
341 \end{methoddesc}
343 \begin{methoddesc}{lock}{}
344 \methodline{unlock}{}
345 Maildir mailboxes do not support (or require) locking, so these methods do
346 nothing.
347 \end{methoddesc}
349 \begin{methoddesc}{close}{}
350 \class{Maildir} instances do not keep any open files and the underlying
351 mailboxes do not support locking, so this method does nothing.
352 \end{methoddesc}
354 \begin{methoddesc}{get_file}{key}
355 Depending upon the host platform, it may not be possible to modify or remove
356 the underlying message while the returned file remains open.
357 \end{methoddesc}
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}
370 \subsubsection{\class{mbox}}
371 \label{mailbox-mbox}
373 \begin{classdesc}{mbox}{path\optional{, factory=None\optional{, create=True}}}
374 A subclass of \class{Mailbox} for mailboxes in mbox format. Parameter
375 \var{factory} is a callable object that accepts a file-like message
376 representation (which behaves as if opened in binary mode) and returns a custom
377 representation. If \var{factory} is \code{None}, \class{mboxMessage} is used as
378 the default message representation. If \var{create} is \code{True}, the mailbox
379 is created if it does not exist.
380 \end{classdesc}
382 The mbox format is the classic format for storing mail on \UNIX{} systems. All
383 messages in an mbox mailbox are stored in a single file with the beginning of
384 each message indicated by a line whose first five characters are "From~".
386 Several variations of the mbox format exist to address perceived shortcomings
387 in the original. In the interest of compatibility, \class{mbox} implements the
388 original format, which is sometimes referred to as \dfn{mboxo}. This means that
389 the \mailheader{Content-Length} header, if present, is ignored and that any
390 occurrences of "From~" at the beginning of a line in a message body are
391 transformed to ">From~" when storing the message, although occurences of
392 ">From~" are not transformed to "From~" when reading the message.
394 Some \class{Mailbox} methods implemented by \class{mbox} deserve special
395 remarks:
397 \begin{methoddesc}{get_file}{key}
398 Using 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}
402 \begin{methoddesc}{lock}{}
403 \methodline{unlock}{}
404 Three locking mechanisms are used---dot locking and, if available, the
405 \cfunction{flock()} and \cfunction{lockf()} system calls.
406 \end{methoddesc}
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}
423 \subsubsection{\class{MH}}
424 \label{mailbox-mh}
426 \begin{classdesc}{MH}{path\optional{, factory=None\optional{, create=True}}}
427 A subclass of \class{Mailbox} for mailboxes in MH format. Parameter
428 \var{factory} is a callable object that accepts a file-like message
429 representation (which behaves as if opened in binary mode) and returns a custom
430 representation. If \var{factory} is \code{None}, \class{MHMessage} is used as
431 the default message representation. If \var{create} is \code{True}, the mailbox
432 is created if it does not exist.
433 \end{classdesc}
435 MH is a directory-based mailbox format invented for the MH Message Handling
436 System, a mail user agent. Each message in an MH mailbox resides in its own
437 file. An MH mailbox may contain other MH mailboxes (called \dfn{folders}) in
438 addition to messages. Folders may be nested indefinitely. MH mailboxes also
439 support \dfn{sequences}, which are named lists used to logically group messages
440 without moving them to sub-folders. Sequences are defined in a file called
441 \file{.mh_sequences} in each folder.
443 The \class{MH} class manipulates MH mailboxes, but it does not attempt to
444 emulate all of \program{mh}'s behaviors. In particular, it does not modify and
445 is not affected by the \file{context} or \file{.mh_profile} files that are used
446 by \program{mh} to store its state and configuration.
448 \class{MH} instances have all of the methods of \class{Mailbox} in addition to
449 the following:
451 \begin{methoddesc}{list_folders}{}
452 Return a list of the names of all folders.
453 \end{methoddesc}
455 \begin{methoddesc}{get_folder}{folder}
456 Return an \class{MH} instance representing the folder whose name is
457 \var{folder}. A \exception{NoSuchMailboxError} exception is raised if the
458 folder does not exist.
459 \end{methoddesc}
461 \begin{methoddesc}{add_folder}{folder}
462 Create a folder whose name is \var{folder} and return an \class{MH} instance
463 representing it.
464 \end{methoddesc}
466 \begin{methoddesc}{remove_folder}{folder}
467 Delete the folder whose name is \var{folder}. If the folder contains any
468 messages, a \exception{NotEmptyError} exception will be raised and the folder
469 will not be deleted.
470 \end{methoddesc}
472 \begin{methoddesc}{get_sequences}{}
473 Return a dictionary of sequence names mapped to key lists. If there are no
474 sequences, the empty dictionary is returned.
475 \end{methoddesc}
477 \begin{methoddesc}{set_sequences}{sequences}
478 Re-define the sequences that exist in the mailbox based upon \var{sequences}, a
479 dictionary of names mapped to key lists, like returned by
480 \method{get_sequences()}.
481 \end{methoddesc}
483 \begin{methoddesc}{pack}{}
484 Rename messages in the mailbox as necessary to eliminate gaps in numbering.
485 Entries in the sequences list are updated correspondingly. \note{Already-issued
486 keys are invalidated by this operation and should not be subsequently used.}
487 \end{methoddesc}
489 Some \class{Mailbox} methods implemented by \class{MH} deserve special remarks:
491 \begin{methoddesc}{remove}{key}
492 \methodline{__delitem__}{key}
493 \methodline{discard}{key}
494 These methods immediately delete the message. The MH convention of marking a
495 message for deletion by prepending a comma to its name is not used.
496 \end{methoddesc}
498 \begin{methoddesc}{lock}{}
499 \methodline{unlock}{}
500 Three locking mechanisms are used---dot locking and, if available, the
501 \cfunction{flock()} and \cfunction{lockf()} system calls. For MH mailboxes,
502 locking the mailbox means locking the \file{.mh_sequences} file and, only for
503 the duration of any operations that affect them, locking individual message
504 files.
505 \end{methoddesc}
507 \begin{methoddesc}{get_file}{key}
508 Depending upon the host platform, it may not be possible to remove the
509 underlying message while the returned file remains open.
510 \end{methoddesc}
512 \begin{methoddesc}{flush}{}
513 All changes to MH mailboxes are immediately applied, so this method does
514 nothing.
515 \end{methoddesc}
517 \begin{methoddesc}{close}{}
518 \class{MH} instances do not keep any open files, so this method is equivelant
519 to \method{unlock()}.
520 \end{methoddesc}
522 \begin{seealso}
523 \seelink{http://www.nongnu.org/nmh/}{nmh - Message Handling System}{Home page
524 of \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 \&
526 Programmers}{A GPL-licensed book on \program{mh} and \program{nmh}, with some
527 information on the mailbox format.}
528 \end{seealso}
530 \subsubsection{\class{Babyl}}
531 \label{mailbox-babyl}
533 \begin{classdesc}{Babyl}{path\optional{, factory=None\optional{, create=True}}}
534 A subclass of \class{Mailbox} for mailboxes in Babyl format. Parameter
535 \var{factory} is a callable object that accepts a file-like message
536 representation (which behaves as if opened in binary mode) and returns a custom
537 representation. If \var{factory} is \code{None}, \class{BabylMessage} is used
538 as the default message representation. If \var{create} is \code{True}, the
539 mailbox is created if it does not exist.
540 \end{classdesc}
542 Babyl is a single-file mailbox format used by the Rmail mail user agent
543 included with Emacs. The beginning of a message is indicated by a line
544 containing the two characters Control-Underscore
545 (\character{\textbackslash037}) and Control-L (\character{\textbackslash014}).
546 The end of a message is indicated by the start of the next message or, in the
547 case of the last message, a line containing a Control-Underscore
548 (\character{\textbackslash037}) character.
550 Messages in a Babyl mailbox have two sets of headers, original headers and
551 so-called visible headers. Visible headers are typically a subset of the
552 original headers that have been reformatted or abridged to be more attractive.
553 Each message in a Babyl mailbox also has an accompanying list of \dfn{labels},
554 or short strings that record extra information about the message, and a list of
555 all user-defined labels found in the mailbox is kept in the Babyl options
556 section.
558 \class{Babyl} instances have all of the methods of \class{Mailbox} in addition
559 to the following:
561 \begin{methoddesc}{get_labels}{}
562 Return 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
564 mailbox rather than consulting the list of labels in the Babyl options section,
565 but the Babyl section is updated whenever the mailbox is modified.}
566 \end{methoddesc}
568 Some \class{Mailbox} methods implemented by \class{Babyl} deserve special
569 remarks:
571 \begin{methoddesc}{get_file}{key}
572 In Babyl mailboxes, the headers of a message are not stored contiguously with
573 the body of the message. To generate a file-like representation, the headers
574 and 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
576 result, the file-like object is truly independent of the underlying mailbox but
577 does not save memory compared to a string representation.
578 \end{methoddesc}
580 \begin{methoddesc}{lock}{}
581 \methodline{unlock}{}
582 Three locking mechanisms are used---dot locking and, if available, the
583 \cfunction{flock()} and \cfunction{lockf()} system calls.
584 \end{methoddesc}
586 \begin{seealso}
587 \seelink{http://quimby.gnus.org/notes/BABYL}{Format of Version 5 Babyl Files}{A
588 specification of the Babyl format.}
589 \seelink{http://www.gnu.org/software/emacs/manual/html_node/Rmail.html}{Reading
590 Mail with Rmail}{The Rmail manual, with some information on Babyl semantics.}
591 \end{seealso}
593 \subsubsection{\class{MMDF}}
594 \label{mailbox-mmdf}
596 \begin{classdesc}{MMDF}{path\optional{, factory=None\optional{, create=True}}}
597 A subclass of \class{Mailbox} for mailboxes in MMDF format. Parameter
598 \var{factory} is a callable object that accepts a file-like message
599 representation (which behaves as if opened in binary mode) and returns a custom
600 representation. If \var{factory} is \code{None}, \class{MMDFMessage} is used as
601 the default message representation. If \var{create} is \code{True}, the mailbox
602 is created if it does not exist.
603 \end{classdesc}
605 MMDF is a single-file mailbox format invented for the Multichannel Memorandum
606 Distribution Facility, a mail transfer agent. Each message is in the same form
607 as an mbox message but is bracketed before and after by lines containing four
608 Control-A (\character{\textbackslash001}) characters. As with the mbox format,
609 the beginning of each message is indicated by a line whose first five
610 characters are "From~", but additional occurrences of "From~" are not
611 transformed to ">From~" when storing messages because the extra message
612 separator lines prevent mistaking such occurrences for the starts of subsequent
613 messages.
615 Some \class{Mailbox} methods implemented by \class{MMDF} deserve special
616 remarks:
618 \begin{methoddesc}{get_file}{key}
619 Using 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}
623 \begin{methoddesc}{lock}{}
624 \methodline{unlock}{}
625 Three locking mechanisms are used---dot locking and, if available, the
626 \cfunction{flock()} and \cfunction{lockf()} system calls.
627 \end{methoddesc}
629 \begin{seealso}
630 \seelink{http://www.tin.org/bin/man.cgi?section=5\&topic=mmdf}{mmdf man page
631 from tin}{A specification of MMDF format from the documentation of tin, a
632 newsreader.}
633 \seelink{http://en.wikipedia.org/wiki/MMDF}{MMDF}{A Wikipedia article
634 describing the Multichannel Memorandum Distribution Facility.}
635 \end{seealso}
637 \subsection{\class{Message} objects}
638 \label{mailbox-message-objects}
640 \begin{classdesc}{Message}{\optional{message}}
641 A subclass of the \module{email.Message} module's \class{Message}. Subclasses
642 of \class{mailbox.Message} add mailbox-format-specific state and behavior.
644 If \var{message} is omitted, the new instance is created in a default, empty
645 state. If \var{message} is an \class{email.Message.Message} instance, its
646 contents are copied; furthermore, any format-specific information is converted
647 insofar 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
649 message, which is read and parsed.
650 \end{classdesc}
652 The format-specific state and behaviors offered by subclasses vary, but in
653 general it is only the properties that are not specific to a particular mailbox
654 that are supported (although presumably the properties are specific to a
655 particular mailbox format). For example, file offsets for single-file mailbox
656 formats and file names for directory-based mailbox formats are not retained,
657 because they are only applicable to the original mailbox. But state such as
658 whether a message has been read by the user or marked as important is retained,
659 because it applies to the message itself.
661 There is no requirement that \class{Message} instances be used to represent
662 messages retrieved using \class{Mailbox} instances. In some situations, the
663 time and memory required to generate \class{Message} representations might not
664 not acceptable. For such situations, \class{Mailbox} instances also offer
665 string and file-like representations, and a custom message factory may be
666 specified when a \class{Mailbox} instance is initialized.
668 \subsubsection{\class{MaildirMessage}}
669 \label{mailbox-maildirmessage}
671 \begin{classdesc}{MaildirMessage}{\optional{message}}
672 A message with Maildir-specific behaviors. Parameter \var{message}
673 has the same meaning as with the \class{Message} constructor.
674 \end{classdesc}
676 Typically, 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
678 user opens and closes the mailbox, recording that the messages are old whether
679 or not they've actually been read. Each message in \file{cur} has an "info"
680 section added to its file name to store information about its state. (Some mail
681 readers may also add an "info" section to messages in \file{new}.) The "info"
682 section may take one of two forms: it may contain "2," followed by a list of
683 standardized flags (e.g., "2,FR") or it may contain "1," followed by so-called
684 experimental information. Standard flags for Maildir messages are as follows:
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}
695 \class{MaildirMessage} instances offer the following methods:
697 \begin{methoddesc}{get_subdir}{}
698 Return either "new" (if the message should be stored in the \file{new}
699 subdirectory) or "cur" (if the message should be stored in the \file{cur}
700 subdirectory). \note{A message is typically moved from \file{new} to \file{cur}
701 after its mailbox has been accessed, whether or not the message is has been
702 read. A message \code{msg} has been read if \code{"S" not in msg.get_flags()}
703 is \code{True}.}
704 \end{methoddesc}
706 \begin{methoddesc}{set_subdir}{subdir}
707 Set the subdirectory the message should be stored in. Parameter \var{subdir}
708 must be either "new" or "cur".
709 \end{methoddesc}
711 \begin{methoddesc}{get_flags}{}
712 Return a string specifying the flags that are currently set. If the message
713 complies with the standard Maildir format, the result is the concatenation in
714 alphabetical order of zero or one occurrence of each of \character{D},
715 \character{F}, \character{P}, \character{R}, \character{S}, and \character{T}.
716 The empty string is returned if no flags are set or if "info" contains
717 experimental semantics.
718 \end{methoddesc}
720 \begin{methoddesc}{set_flags}{flags}
721 Set the flags specified by \var{flags} and unset all others.
722 \end{methoddesc}
724 \begin{methoddesc}{add_flag}{flag}
725 Set the flag(s) specified by \var{flag} without changing other flags. To add
726 more than one flag at a time, \var{flag} may be a string of more than one
727 character. The current "info" is overwritten whether or not it contains
728 experimental information rather than
729 flags.
730 \end{methoddesc}
732 \begin{methoddesc}{remove_flag}{flag}
733 Unset the flag(s) specified by \var{flag} without changing other flags. To
734 remove more than one flag at a time, \var{flag} maybe a string of more than one
735 character. If "info" contains experimental information rather than flags, the
736 current "info" is not modified.
737 \end{methoddesc}
739 \begin{methoddesc}{get_date}{}
740 Return the delivery date of the message as a floating-point number representing
741 seconds since the epoch.
742 \end{methoddesc}
744 \begin{methoddesc}{set_date}{date}
745 Set the delivery date of the message to \var{date}, a floating-point number
746 representing seconds since the epoch.
747 \end{methoddesc}
749 \begin{methoddesc}{get_info}{}
750 Return a string containing the "info" for a message. This is useful for
751 accessing and modifying "info" that is experimental (i.e., not a list of
752 flags).
753 \end{methoddesc}
755 \begin{methoddesc}{set_info}{info}
756 Set "info" to \var{info}, which should be a string.
757 \end{methoddesc}
759 When a \class{MaildirMessage} instance is created based upon an
760 \class{mboxMessage} or \class{MMDFMessage} instance, the \mailheader{Status}
761 and \mailheader{X-Status} headers are omitted and the following conversions
762 take place:
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}
773 When a \class{MaildirMessage} instance is created based upon an
774 \class{MHMessage} instance, the following conversions take place:
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}
784 When a \class{MaildirMessage} instance is created based upon a
785 \class{BabylMessage} instance, the following conversions take place:
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}
796 \subsubsection{\class{mboxMessage}}
797 \label{mailbox-mboxmessage}
799 \begin{classdesc}{mboxMessage}{\optional{message}}
800 A message with mbox-specific behaviors. Parameter \var{message} has the same
801 meaning as with the \class{Message} constructor.
802 \end{classdesc}
804 Messages in an mbox mailbox are stored together in a single file. The sender's
805 envelope address and the time of delivery are typically stored in a line
806 beginning with "From~" that is used to indicate the start of a message, though
807 there is considerable variation in the exact format of this data among mbox
808 implementations. Flags that indicate the state of the message, such as whether
809 it has been read or marked as important, are typically stored in
810 \mailheader{Status} and \mailheader{X-Status} headers.
812 Conventional flags for mbox messages are as follows:
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}
822 The "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
824 flags and headers typically appear in the order mentioned.
826 \class{mboxMessage} instances offer the following methods:
828 \begin{methoddesc}{get_from}{}
829 Return a string representing the "From~" line that marks the start of the
830 message in an mbox mailbox. The leading "From~" and the trailing newline are
831 excluded.
832 \end{methoddesc}
834 \begin{methoddesc}{set_from}{from_\optional{, time_=None}}
835 Set the "From~" line to \var{from_}, which should be specified without a
836 leading "From~" or trailing newline. For convenience, \var{time_} may be
837 specified 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
839 suitable for passing to \method{time.strftime()}, or \code{True} (to use
840 \method{time.gmtime()}).
841 \end{methoddesc}
843 \begin{methoddesc}{get_flags}{}
844 Return a string specifying the flags that are currently set. If the message
845 complies with the conventional format, the result is the concatenation in the
846 following order of zero or one occurrence of each of \character{R},
847 \character{O}, \character{D}, \character{F}, and \character{A}.
848 \end{methoddesc}
850 \begin{methoddesc}{set_flags}{flags}
851 Set 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
853 occurrences of each of \character{R}, \character{O}, \character{D},
854 \character{F}, and \character{A}.
855 \end{methoddesc}
857 \begin{methoddesc}{add_flag}{flag}
858 Set the flag(s) specified by \var{flag} without changing other flags. To add
859 more than one flag at a time, \var{flag} may be a string of more than one
860 character.
861 \end{methoddesc}
863 \begin{methoddesc}{remove_flag}{flag}
864 Unset the flag(s) specified by \var{flag} without changing other flags. To
865 remove more than one flag at a time, \var{flag} maybe a string of more than one
866 character.
867 \end{methoddesc}
869 When 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
872 take place:
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}
883 When an \class{mboxMessage} instance is created based upon an \class{MHMessage}
884 instance, the following conversions take place:
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}
894 When an \class{mboxMessage} instance is created based upon a
895 \class{BabylMessage} instance, the following conversions take place:
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}
905 When a \class{Message} instance is created based upon an \class{MMDFMessage}
906 instance, the "From~" line is copied and all flags directly correspond:
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}
917 \subsubsection{\class{MHMessage}}
918 \label{mailbox-mhmessage}
920 \begin{classdesc}{MHMessage}{\optional{message}}
921 A message with MH-specific behaviors. Parameter \var{message} has the same
922 meaning as with the \class{Message} constructor.
923 \end{classdesc}
925 MH messages do not support marks or flags in the traditional sense, but they do
926 support sequences, which are logical groupings of arbitrary messages. Some mail
927 reading programs (although not the standard \program{mh} and \program{nmh}) use
928 sequences in much the same way flags are used with other formats, as follows:
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}
936 \class{MHMessage} instances offer the following methods:
938 \begin{methoddesc}{get_sequences}{}
939 Return a list of the names of sequences that include this message.
940 \end{methoddesc}
942 \begin{methoddesc}{set_sequences}{sequences}
943 Set the list of sequences that include this message.
944 \end{methoddesc}
946 \begin{methoddesc}{add_sequence}{sequence}
947 Add \var{sequence} to the list of sequences that include this message.
948 \end{methoddesc}
950 \begin{methoddesc}{remove_sequence}{sequence}
951 Remove \var{sequence} from the list of sequences that include this message.
952 \end{methoddesc}
954 When an \class{MHMessage} instance is created based upon a
955 \class{MaildirMessage} instance, the following conversions take place:
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}
964 When an \class{MHMessage} instance is created based upon an \class{mboxMessage}
965 or \class{MMDFMessage} instance, the \mailheader{Status} and
966 \mailheader{X-Status} headers are omitted and the following conversions take
967 place:
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}
976 When an \class{MHMessage} instance is created based upon a \class{BabylMessage}
977 instance, the following conversions take place:
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}
985 \subsubsection{\class{BabylMessage}}
986 \label{mailbox-babylmessage}
988 \begin{classdesc}{BabylMessage}{\optional{message}}
989 A message with Babyl-specific behaviors. Parameter \var{message} has the same
990 meaning as with the \class{Message} constructor.
991 \end{classdesc}
993 Certain message labels, called \dfn{attributes}, are defined by convention to
994 have special meanings. The attributes are as follows:
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}
1006 By default, Rmail displays only
1007 visible headers. The \class{BabylMessage} class, though, uses the original
1008 headers because they are more complete. Visible headers may be accessed
1009 explicitly if desired.
1011 \class{BabylMessage} instances offer the following methods:
1013 \begin{methoddesc}{get_labels}{}
1014 Return a list of labels on the message.
1015 \end{methoddesc}
1017 \begin{methoddesc}{set_labels}{labels}
1018 Set the list of labels on the message to \var{labels}.
1019 \end{methoddesc}
1021 \begin{methoddesc}{add_label}{label}
1022 Add \var{label} to the list of labels on the message.
1023 \end{methoddesc}
1025 \begin{methoddesc}{remove_label}{label}
1026 Remove \var{label} from the list of labels on the message.
1027 \end{methoddesc}
1029 \begin{methoddesc}{get_visible}{}
1030 Return an \class{Message} instance whose headers are the message's visible
1031 headers and whose body is empty.
1032 \end{methoddesc}
1034 \begin{methoddesc}{set_visible}{visible}
1035 Set 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
1038 should be open in text mode).
1039 \end{methoddesc}
1041 \begin{methoddesc}{update_visible}{}
1042 When a \class{BabylMessage} instance's original headers are modified, the
1043 visible headers are not automatically modified to correspond. This method
1044 updates the visible headers as follows: each visible header with a
1045 corresponding original header is set to the value of the original header, each
1046 visible 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
1049 headers but not the visible headers are added to the visible headers.
1050 \end{methoddesc}
1052 When a \class{BabylMessage} instance is created based upon a
1053 \class{MaildirMessage} instance, the following conversions take place:
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}
1063 When a \class{BabylMessage} instance is created based upon an
1064 \class{mboxMessage} or \class{MMDFMessage} instance, the \mailheader{Status}
1065 and \mailheader{X-Status} headers are omitted and the following conversions
1066 take place:
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}
1075 When a \class{BabylMessage} instance is created based upon an \class{MHMessage}
1076 instance, the following conversions take place:
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}
1084 \subsubsection{\class{MMDFMessage}}
1085 \label{mailbox-mmdfmessage}
1087 \begin{classdesc}{MMDFMessage}{\optional{message}}
1088 A message with MMDF-specific behaviors. Parameter \var{message} has the same
1089 meaning as with the \class{Message} constructor.
1090 \end{classdesc}
1092 As with message in an mbox mailbox, MMDF messages are stored with the sender's
1093 address and the delivery date in an initial line beginning with "From ".
1094 Likewise, flags that indicate the state of the message are typically stored in
1095 \mailheader{Status} and \mailheader{X-Status} headers.
1097 Conventional flags for MMDF messages are identical to those of mbox message and
1098 are as follows:
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}
1108 The "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
1110 flags and headers typically appear in the order mentioned.
1112 \class{MMDFMessage} instances offer the following methods, which are identical
1113 to those offered by \class{mboxMessage}:
1115 \begin{methoddesc}{get_from}{}
1116 Return a string representing the "From~" line that marks the start of the
1117 message in an mbox mailbox. The leading "From~" and the trailing newline are
1118 excluded.
1119 \end{methoddesc}
1121 \begin{methoddesc}{set_from}{from_\optional{, time_=None}}
1122 Set the "From~" line to \var{from_}, which should be specified without a
1123 leading "From~" or trailing newline. For convenience, \var{time_} may be
1124 specified 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
1126 suitable for passing to \method{time.strftime()}, or \code{True} (to use
1127 \method{time.gmtime()}).
1128 \end{methoddesc}
1130 \begin{methoddesc}{get_flags}{}
1131 Return a string specifying the flags that are currently set. If the message
1132 complies with the conventional format, the result is the concatenation in the
1133 following order of zero or one occurrence of each of \character{R},
1134 \character{O}, \character{D}, \character{F}, and \character{A}.
1135 \end{methoddesc}
1137 \begin{methoddesc}{set_flags}{flags}
1138 Set 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
1140 occurrences of each of \character{R}, \character{O}, \character{D},
1141 \character{F}, and \character{A}.
1142 \end{methoddesc}
1144 \begin{methoddesc}{add_flag}{flag}
1145 Set the flag(s) specified by \var{flag} without changing other flags. To add
1146 more than one flag at a time, \var{flag} may be a string of more than one
1147 character.
1148 \end{methoddesc}
1150 \begin{methoddesc}{remove_flag}{flag}
1151 Unset the flag(s) specified by \var{flag} without changing other flags. To
1152 remove more than one flag at a time, \var{flag} maybe a string of more than one
1153 character.
1154 \end{methoddesc}
1156 When 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
1159 take place:
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}
1170 When an \class{MMDFMessage} instance is created based upon an \class{MHMessage}
1171 instance, the following conversions take place:
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}
1181 When an \class{MMDFMessage} instance is created based upon a
1182 \class{BabylMessage} instance, the following conversions take place:
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}
1192 When an \class{MMDFMessage} instance is created based upon an
1193 \class{mboxMessage} instance, the "From~" line is copied and all flags directly
1194 correspond:
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}
1205 \subsection{Exceptions}
1206 \label{mailbox-deprecated}
1208 The following exception classes are defined in the \module{mailbox} module:
1210 \begin{classdesc}{Error}{}
1211 The based class for all other module-specific exceptions.
1212 \end{classdesc}
1214 \begin{classdesc}{NoSuchMailboxError}{}
1215 Raised when a mailbox is expected but is not found, such as when instantiating
1216 a \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
1218 not exist.
1219 \end{classdesc}
1221 \begin{classdesc}{NotEmptyErrorError}{}
1222 Raised when a mailbox is not empty but is expected to be, such as when deleting
1223 a folder that contains messages.
1224 \end{classdesc}
1226 \begin{classdesc}{ExternalClashError}{}
1227 Raised when some mailbox-related condition beyond the control of the program
1228 causes it to be unable to proceed, such as when failing to acquire a lock that
1229 another program already holds a lock, or when a uniquely-generated file name
1230 already exists.
1231 \end{classdesc}
1233 \begin{classdesc}{FormatError}{}
1234 Raised when the data in a file cannot be parsed, such as when an \class{MH}
1235 instance attempts to read a corrupted \file{.mh_sequences} file.
1236 \end{classdesc}
1238 \subsection{Deprecated classes and methods}
1239 \label{mailbox-deprecated}
1241 Older versions of the \module{mailbox} module do not support modification of
1242 mailboxes, such as adding or removing message, and do not provide classes to
1243 represent format-specific message properties. For backward compatibility, the
1244 older mailbox classes are still available, but the newer classes should be used
1245 in preference to them.
1247 Older mailbox objects support only iteration and provide a single public
1248 method:
1250 \begin{methoddesc}{next}{}
1251 Return the next message in the mailbox, created with the optional \var{factory}
1252 argument passed into the mailbox object's constructor. By default this is an
1253 \class{rfc822.Message} object (see the \refmodule{rfc822} module). Depending
1254 on the mailbox implementation the \var{fp} attribute of this object may be a
1255 true file object or a class instance simulating a file object, taking care of
1256 things like message boundaries if multiple mail messages are contained in a
1257 single file, etc. If no more messages are available, this method returns
1258 \code{None}.
1259 \end{methoddesc}
1261 Most of the older mailbox classes have names that differ from the current
1262 mailbox class names, except for \class{Maildir}. For this reason, the new
1263 \class{Maildir} class defines a \method{next()} method and its constructor
1264 differs slightly from those of the other new mailbox classes.
1266 The older mailbox classes whose names are not the same as their newer
1267 counterparts are as follows:
1269 \begin{classdesc}{UnixMailbox}{fp\optional{, factory}}
1270 Access to a classic \UNIX-style mailbox, where all messages are
1271 contained in a single file and separated by \samp{From }
1272 (a.k.a.\ \samp{From_}) lines. The file object \var{fp} points to the
1273 mailbox file. The optional \var{factory} parameter is a callable that
1274 should create new message objects. \var{factory} is called with one
1275 argument, \var{fp} by the \method{next()} method of the mailbox
1276 object. The default is the \class{rfc822.Message} class (see the
1277 \refmodule{rfc822} module -- and the note below).
1279 \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}
1285 For maximum portability, messages in a \UNIX-style mailbox are
1286 separated by any line that begins exactly with the string \code{'From
1287 '} (note the trailing space) if preceded by exactly two newlines.
1288 Because of the wide-range of variations in practice, nothing else on
1289 the From_ line should be considered. However, the current
1290 implementation doesn't check for the leading two newlines. This is
1291 usually fine for most applications.
1293 The \class{UnixMailbox} class implements a more strict version of
1294 From_ line checking, using a regular expression that usually correctly
1295 matched From_ delimiters. It considers delimiter line to be separated
1296 by \samp{From \var{name} \var{time}} lines. For maximum portability,
1297 use the \class{PortableUnixMailbox} class instead. This class is
1298 identical to \class{UnixMailbox} except that individual messages are
1299 separated by only \samp{From } lines.
1301 For more information, see
1302 \citetitle[http://home.netscape.com/eng/mozilla/2.0/relnotes/demo/content-length.html]{Configuring
1303 Netscape Mail on \UNIX: Why the Content-Length Format is Bad}.
1304 \end{classdesc}
1306 \begin{classdesc}{PortableUnixMailbox}{fp\optional{, factory}}
1307 A 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
1310 protect against some variations that are observed in practice. This
1311 works since lines in the message which begin with \code{'From '} are
1312 quoted by mail handling software at delivery-time.
1313 \end{classdesc}
1315 \begin{classdesc}{MmdfMailbox}{fp\optional{, factory}}
1316 Access an MMDF-style mailbox, where all messages are contained
1317 in a single file and separated by lines consisting of 4 control-A
1318 characters. The file object \var{fp} points to the mailbox file.
1319 Optional \var{factory} is as with the \class{UnixMailbox} class.
1320 \end{classdesc}
1322 \begin{classdesc}{MHMailbox}{dirname\optional{, factory}}
1323 Access an MH mailbox, a directory with each message in a separate
1324 file with a numeric name.
1325 The name of the mailbox directory is passed in \var{dirname}.
1326 \var{factory} is as with the \class{UnixMailbox} class.
1327 \end{classdesc}
1329 \begin{classdesc}{BabylMailbox}{fp\optional{, factory}}
1330 Access a Babyl mailbox, which is similar to an MMDF mailbox. In
1331 Babyl format, each message has two sets of headers, the
1332 \emph{original} headers and the \emph{visible} headers. The original
1333 headers appear before a line containing only \code{'*** EOOH ***'}
1334 (End-Of-Original-Headers) and the visible headers appear after the
1335 \code{EOOH} line. Babyl-compliant mail readers will show you only the
1336 visible headers, and \class{BabylMailbox} objects will return messages
1337 containing only the visible headers. You'll have to do your own
1338 parsing of the mailbox file to get at the original headers. Mail
1339 messages 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.
1342 \end{classdesc}
1344 If you wish to use the older mailbox classes with the \module{email} module
1345 rather than the deprecated \module{rfc822} module, you can do so as follows:
1347 \begin{verbatim}
1348 import email
1349 import email.Errors
1350 import mailbox
1352 def msgfactory(fp):
1353 try:
1354 return email.message_from_file(fp)
1355 except email.Errors.MessageParseError:
1356 # Don't return None since that will
1357 # stop the mailbox iterator
1358 return ''
1360 mbox = mailbox.UnixMailbox(fp, msgfactory)
1361 \end{verbatim}
1363 Alternatively, if you know your mailbox contains only well-formed MIME
1364 messages, you can simplify this to:
1366 \begin{verbatim}
1367 import email
1368 import mailbox
1370 mbox = mailbox.UnixMailbox(fp, email.message_from_file)
1371 \end{verbatim}
1373 \subsection{Examples}
1374 \label{mailbox-examples}
1376 A simple example of printing the subjects of all messages in a mailbox that
1377 seem interesting:
1379 \begin{verbatim}
1380 import mailbox
1381 for 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}
1387 To copy all mail from a Babyl mailbox to an MH mailbox, converting all
1388 of the format-specific information that can be converted:
1390 \begin{verbatim}
1391 import mailbox
1392 destination = mailbox.MH('~/Mail')
1393 destination.lock()
1394 for message in mailbox.Babyl('~/RMAIL'):
1395 destination.add(MHMessage(message))
1396 destination.flush()
1397 destination.unlock()
1398 \end{verbatim}
1400 This example sorts mail from several mailing lists into different
1401 mailboxes, being careful to avoid mail corruption due to concurrent
1402 modification by other programs, mail loss due to interruption of the
1403 program, or premature termination due to malformed messages in the
1404 mailbox:
1406 \begin{verbatim}
1407 import mailbox
1408 import email.Errors
1410 list_names = ('python-list', 'python-dev', 'python-bugs')
1412 boxes = dict((name, mailbox.mbox('~/email/%s' % name)) for name in list_names)
1413 inbox = mailbox.Maildir('~/Maildir', factory=None)
1415 for key in inbox.iterkeys():
1416 try:
1417 message = inbox[key]
1418 except email.Errors.MessageParseError:
1419 continue # The message is malformed. Just leave it.
1421 for name in list_names:
1422 list_id = message['list-id']
1423 if list_id and name in list_id:
1424 # Get mailbox to use
1425 box = boxes[name]
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.
1430 box.lock()
1431 box.add(message)
1432 box.flush()
1433 box.unlock()
1435 # Remove original message
1436 inbox.lock()
1437 inbox.discard(key)
1438 inbox.flush()
1439 inbox.unlock()
1440 break # Found destination, so stop looking.
1442 for box in boxes.itervalues():
1443 box.close()
1444 \end{verbatim}