blob: 547923d869a0a83346a74957128543d8e088b3f1 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{rfc822} ---
Fred Drakeffbe6871999-04-22 21:23:22 +00002 Parse RFC 822 mail headers}
Guido van Rossuma12ef941995-02-27 17:53:25 +00003
Fred Drakeffbe6871999-04-22 21:23:22 +00004\declaremodule{standard}{rfc822}
Fred Drakeb91e9341998-07-23 17:59:49 +00005\modulesynopsis{Parse \rfc{822} style mail headers.}
6
Fred Drakecdea8a31998-03-14 06:17:43 +00007This module defines a class, \class{Message}, which represents a
Guido van Rossuma12ef941995-02-27 17:53:25 +00008collection of ``email headers'' as defined by the Internet standard
Fred Drakec5891241998-02-09 19:16:20 +00009\rfc{822}. It is used in various contexts, usually to read such
Guido van Rossum87294831998-06-16 22:27:40 +000010headers from a file. This module also defines a helper class
Fred Drake38e5d272000-04-03 20:13:55 +000011\class{AddressList} for parsing \rfc{822} addresses. Please refer to
12the RFC for information on the specific syntax of \rfc{822} headers.
Guido van Rossuma12ef941995-02-27 17:53:25 +000013
Fred Drake38e5d272000-04-03 20:13:55 +000014The \refmodule{mailbox}\refstmodindex{mailbox} module provides classes
15to read mailboxes produced by various end-user mail programs.
Guido van Rossum067a2ac1997-06-02 17:30:03 +000016
Fred Drakecdea8a31998-03-14 06:17:43 +000017\begin{classdesc}{Message}{file\optional{, seekable}}
Guido van Rossum12991001998-06-10 21:34:27 +000018A \class{Message} instance is instantiated with an input object as
19parameter. Message relies only on the input object having a
Fred Drake23329d41998-08-10 17:46:22 +000020\method{readline()} method; in particular, ordinary file objects
21qualify. Instantiation reads headers from the input object up to a
22delimiter line (normally a blank line) and stores them in the
23instance.
Guido van Rossum12991001998-06-10 21:34:27 +000024
Fred Drake23329d41998-08-10 17:46:22 +000025This class can work with any input object that supports a
26\method{readline()} method. If the input object has seek and tell
27capability, the \method{rewindbody()} method will work; also, illegal
28lines will be pushed back onto the input stream. If the input object
29lacks seek but has an \method{unread()} method that can push back a
30line of input, \class{Message} will use that to push back illegal
31lines. Thus this class can be used to parse messages coming from a
32buffered stream.
Guido van Rossum12991001998-06-10 21:34:27 +000033
Fred Drake23329d41998-08-10 17:46:22 +000034The optional \var{seekable} argument is provided as a workaround for
35certain stdio libraries in which \cfunction{tell()} discards buffered
36data before discovering that the \cfunction{lseek()} system call
37doesn't work. For maximum portability, you should set the seekable
38argument to zero to prevent that initial \method{tell()} when passing
39in an unseekable object such as a a file object created from a socket
40object.
Guido van Rossuma12ef941995-02-27 17:53:25 +000041
42Input lines as read from the file may either be terminated by CR-LF or
43by a single linefeed; a terminating CR-LF is replaced by a single
44linefeed before the line is stored.
45
46All header matching is done independent of upper or lower case;
Fred Drake23329d41998-08-10 17:46:22 +000047e.g.\ \code{\var{m}['From']}, \code{\var{m}['from']} and
Fred Drakecdea8a31998-03-14 06:17:43 +000048\code{\var{m}['FROM']} all yield the same result.
49\end{classdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000050
Guido van Rossum87294831998-06-16 22:27:40 +000051\begin{classdesc}{AddressList}{field}
Fred Drakeae0f2921999-06-10 15:03:07 +000052You may instantiate the \class{AddressList} helper class using a single
Fred Drake23329d41998-08-10 17:46:22 +000053string parameter, a comma-separated list of \rfc{822} addresses to be
54parsed. (The parameter \code{None} yields an empty list.)
Guido van Rossum87294831998-06-16 22:27:40 +000055\end{classdesc}
56
Guido van Rossum843e7121996-12-06 21:23:53 +000057\begin{funcdesc}{parsedate}{date}
Fred Drakecdea8a31998-03-14 06:17:43 +000058Attempts to parse a date according to the rules in \rfc{822}.
59however, some mailers don't follow that format as specified, so
60\function{parsedate()} tries to guess correctly in such cases.
Fred Drakec5891241998-02-09 19:16:20 +000061\var{date} is a string containing an \rfc{822} date, such as
Fred Drakecdea8a31998-03-14 06:17:43 +000062\code{'Mon, 20 Nov 1995 19:12:08 -0500'}. If it succeeds in parsing
63the date, \function{parsedate()} returns a 9-tuple that can be passed
64directly to \function{time.mktime()}; otherwise \code{None} will be
Fred Drake38e5d272000-04-03 20:13:55 +000065returned. Note that fields 6, 7, and 8 of the result tuple are not
66usable.
Guido van Rossum843e7121996-12-06 21:23:53 +000067\end{funcdesc}
68
69\begin{funcdesc}{parsedate_tz}{date}
Fred Drakecdea8a31998-03-14 06:17:43 +000070Performs the same function as \function{parsedate()}, but returns
71either \code{None} or a 10-tuple; the first 9 elements make up a tuple
72that can be passed directly to \function{time.mktime()}, and the tenth
73is the offset of the date's timezone from UTC (which is the official
74term for Greenwich Mean Time). (Note that the sign of the timezone
75offset is the opposite of the sign of the \code{time.timezone}
76variable for the same timezone; the latter variable follows the
77\POSIX{} standard while this module follows \rfc{822}.) If the input
78string has no timezone, the last element of the tuple returned is
Fred Drake38e5d272000-04-03 20:13:55 +000079\code{None}. Note that fields 6, 7, and 8 of the result tuple are not
80usable.
Guido van Rossum843e7121996-12-06 21:23:53 +000081\end{funcdesc}
82
Guido van Rossum8cf94e61998-02-18 05:09:14 +000083\begin{funcdesc}{mktime_tz}{tuple}
Fred Drakecdea8a31998-03-14 06:17:43 +000084Turn a 10-tuple as returned by \function{parsedate_tz()} into a UTC
85timestamp. It the timezone item in the tuple is \code{None}, assume
86local time. Minor deficiency: this first interprets the first 8
87elements as a local time and then compensates for the timezone
88difference; this may yield a slight error around daylight savings time
Guido van Rossum8cf94e61998-02-18 05:09:14 +000089switch dates. Not enough to worry about for common use.
90\end{funcdesc}
91
Fred Drakeea002051999-04-28 18:11:09 +000092
Fred Drake38e5d272000-04-03 20:13:55 +000093\begin{seealso}
94 \seemodule{mailbox}{Classes to read various mailbox formats produced
95 by end-user mail programs.}
96\end{seealso}
97
98
Fred Drakeea002051999-04-28 18:11:09 +000099\subsection{Message Objects \label{message-objects}}
Guido van Rossumecde7811995-03-28 13:35:14 +0000100
Fred Drakecdea8a31998-03-14 06:17:43 +0000101A \class{Message} instance has the following methods:
Guido van Rossuma12ef941995-02-27 17:53:25 +0000102
Fred Drakee14dde21998-04-04 06:19:30 +0000103\begin{methoddesc}{rewindbody}{}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000104Seek to the start of the message body. This only works if the file
105object is seekable.
Fred Drakee14dde21998-04-04 06:19:30 +0000106\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000107
Guido van Rossum444d0f81998-06-11 13:50:02 +0000108\begin{methoddesc}{isheader}{line}
109Returns a line's canonicalized fieldname (the dictionary key that will
Fred Drakeea002051999-04-28 18:11:09 +0000110be used to index it) if the line is a legal \rfc{822} header; otherwise
Guido van Rossum444d0f81998-06-11 13:50:02 +0000111returns None (implying that parsing should stop here and the line be
112pushed back on the input stream). It is sometimes useful to override
113this method in a subclass.
114\end{methoddesc}
115
Guido van Rossum12991001998-06-10 21:34:27 +0000116\begin{methoddesc}{islast}{line}
117Return true if the given line is a delimiter on which Message should
Guido van Rossum444d0f81998-06-11 13:50:02 +0000118stop. The delimiter line is consumed, and the file object's read
119location positioned immediately after it. By default this method just
120checks that the line is blank, but you can override it in a subclass.
Guido van Rossum12991001998-06-10 21:34:27 +0000121\end{methoddesc}
122
123\begin{methoddesc}{iscomment}{line}
124Return true if the given line should be ignored entirely, just skipped.
125By default this is a stub that always returns false, but you can
126override it in a subclass.
127\end{methoddesc}
128
Fred Drakee14dde21998-04-04 06:19:30 +0000129\begin{methoddesc}{getallmatchingheaders}{name}
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000130Return a list of lines consisting of all headers matching
Guido van Rossuma12ef941995-02-27 17:53:25 +0000131\var{name}, if any. Each physical line, whether it is a continuation
132line or not, is a separate list item. Return the empty list if no
133header matches \var{name}.
Fred Drakee14dde21998-04-04 06:19:30 +0000134\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000135
Fred Drakee14dde21998-04-04 06:19:30 +0000136\begin{methoddesc}{getfirstmatchingheader}{name}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000137Return a list of lines comprising the first header matching
Fred Drakeea002051999-04-28 18:11:09 +0000138\var{name}, and its continuation line(s), if any. Return
139\code{None} if there is no header matching \var{name}.
Fred Drakee14dde21998-04-04 06:19:30 +0000140\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000141
Fred Drakee14dde21998-04-04 06:19:30 +0000142\begin{methoddesc}{getrawheader}{name}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000143Return a single string consisting of the text after the colon in the
144first header matching \var{name}. This includes leading whitespace,
145the trailing linefeed, and internal linefeeds and whitespace if there
146any continuation line(s) were present. Return \code{None} if there is
147no header matching \var{name}.
Fred Drakee14dde21998-04-04 06:19:30 +0000148\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000149
Guido van Rossum12991001998-06-10 21:34:27 +0000150\begin{methoddesc}{getheader}{name\optional{, default}}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000151Like \code{getrawheader(\var{name})}, but strip leading and trailing
Guido van Rossum12991001998-06-10 21:34:27 +0000152whitespace. Internal whitespace is not stripped. The optional
153\var{default} argument can be used to specify a different default to
154be returned when there is no header matching \var{name}.
155\end{methoddesc}
156
157\begin{methoddesc}{get}{name\optional{, default}}
Fred Drake23329d41998-08-10 17:46:22 +0000158An alias for \method{getheader()}, to make the interface more compatible
Guido van Rossum12991001998-06-10 21:34:27 +0000159with regular dictionaries.
Fred Drakee14dde21998-04-04 06:19:30 +0000160\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000161
Fred Drakee14dde21998-04-04 06:19:30 +0000162\begin{methoddesc}{getaddr}{name}
Fred Drakecdea8a31998-03-14 06:17:43 +0000163Return a pair \code{(\var{full name}, \var{email address})} parsed
164from the string returned by \code{getheader(\var{name})}. If no
165header matching \var{name} exists, return \code{(None, None)};
166otherwise both the full name and the address are (possibly empty)
167strings.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000168
Fred Drakecdea8a31998-03-14 06:17:43 +0000169Example: If \var{m}'s first \code{From} header contains the string
Guido van Rossum470be141995-03-17 16:07:09 +0000170\code{'jack@cwi.nl (Jack Jansen)'}, then
Guido van Rossuma12ef941995-02-27 17:53:25 +0000171\code{m.getaddr('From')} will yield the pair
Guido van Rossum470be141995-03-17 16:07:09 +0000172\code{('Jack Jansen', 'jack@cwi.nl')}.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000173If the header contained
Guido van Rossum470be141995-03-17 16:07:09 +0000174\code{'Jack Jansen <jack@cwi.nl>'} instead, it would yield the
Guido van Rossuma12ef941995-02-27 17:53:25 +0000175exact same result.
Fred Drakee14dde21998-04-04 06:19:30 +0000176\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000177
Fred Drakee14dde21998-04-04 06:19:30 +0000178\begin{methoddesc}{getaddrlist}{name}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000179This is similar to \code{getaddr(\var{list})}, but parses a header
Fred Drake23329d41998-08-10 17:46:22 +0000180containing a list of email addresses (e.g.\ a \code{To} header) and
Fred Drakecdea8a31998-03-14 06:17:43 +0000181returns a list of \code{(\var{full name}, \var{email address})} pairs
182(even if there was only one address in the header). If there is no
183header matching \var{name}, return an empty list.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000184
Barry Warsaw53610ca1999-01-14 21:26:54 +0000185If multiple headers exist that match the named header (e.g. if there
186are several \code{Cc} headers), all are parsed for addresses. Any
187continuation lines the named headers contain are also parsed.
Fred Drakee14dde21998-04-04 06:19:30 +0000188\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000189
Fred Drakee14dde21998-04-04 06:19:30 +0000190\begin{methoddesc}{getdate}{name}
Fred Drakecdea8a31998-03-14 06:17:43 +0000191Retrieve a header using \method{getheader()} and parse it into a 9-tuple
Fred Drake38e5d272000-04-03 20:13:55 +0000192compatible with \function{time.mktime()}; note that fields 6, 7, and 8
193are not usable. If there is no header matching
Guido van Rossuma12ef941995-02-27 17:53:25 +0000194\var{name}, or it is unparsable, return \code{None}.
195
196Date parsing appears to be a black art, and not all mailers adhere to
197the standard. While it has been tested and found correct on a large
198collection of email from many sources, it is still possible that this
199function may occasionally yield an incorrect result.
Fred Drakee14dde21998-04-04 06:19:30 +0000200\end{methoddesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000201
Fred Drakee14dde21998-04-04 06:19:30 +0000202\begin{methoddesc}{getdate_tz}{name}
Fred Drakecdea8a31998-03-14 06:17:43 +0000203Retrieve a header using \method{getheader()} and parse it into a
20410-tuple; the first 9 elements will make a tuple compatible with
205\function{time.mktime()}, and the 10th is a number giving the offset
Fred Drake38e5d272000-04-03 20:13:55 +0000206of the date's timezone from UTC. Note that fields 6, 7, and 8
207are not usable. Similarly to \method{getdate()}, if
Guido van Rossum843e7121996-12-06 21:23:53 +0000208there is no header matching \var{name}, or it is unparsable, return
209\code{None}.
Fred Drakee14dde21998-04-04 06:19:30 +0000210\end{methoddesc}
Guido van Rossum843e7121996-12-06 21:23:53 +0000211
Fred Drakecdea8a31998-03-14 06:17:43 +0000212\class{Message} instances also support a read-only mapping interface.
Fred Drakee14dde21998-04-04 06:19:30 +0000213In particular: \code{\var{m}[name]} is like
214\code{\var{m}.getheader(name)} but raises \exception{KeyError} if
215there is no matching header; and \code{len(\var{m})},
Fred Drakecdea8a31998-03-14 06:17:43 +0000216\code{\var{m}.has_key(name)}, \code{\var{m}.keys()},
217\code{\var{m}.values()} and \code{\var{m}.items()} act as expected
218(and consistently).
Guido van Rossuma12ef941995-02-27 17:53:25 +0000219
Fred Drakecdea8a31998-03-14 06:17:43 +0000220Finally, \class{Message} instances have two public instance variables:
Guido van Rossuma12ef941995-02-27 17:53:25 +0000221
Fred Drakee14dde21998-04-04 06:19:30 +0000222\begin{memberdesc}{headers}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000223A list containing the entire set of header lines, in the order in
Guido van Rossum87294831998-06-16 22:27:40 +0000224which they were read (except that setitem calls may disturb this
225order). Each line contains a trailing newline. The
Guido van Rossuma12ef941995-02-27 17:53:25 +0000226blank line terminating the headers is not contained in the list.
Fred Drakee14dde21998-04-04 06:19:30 +0000227\end{memberdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +0000228
Fred Drakee14dde21998-04-04 06:19:30 +0000229\begin{memberdesc}{fp}
Fred Drakeea002051999-04-28 18:11:09 +0000230The file or file-like object passed at instantiation time. This can
231be used to read the message content.
Fred Drakee14dde21998-04-04 06:19:30 +0000232\end{memberdesc}
Guido van Rossum87294831998-06-16 22:27:40 +0000233
Fred Drakeea002051999-04-28 18:11:09 +0000234
235\subsection{AddressList Objects \label{addresslist-objects}}
Guido van Rossum87294831998-06-16 22:27:40 +0000236
237An \class{AddressList} instance has the following methods:
238
239\begin{methoddesc}{__len__}{name}
240Return the number of addresses in the address list.
241\end{methoddesc}
242
243\begin{methoddesc}{__str__}{name}
244Return a canonicalized string representation of the address list.
245Addresses are rendered in "name" <host@domain> form, comma-separated.
246\end{methoddesc}
247
248\begin{methoddesc}{__add__}{name}
Fred Drakeae0f2921999-06-10 15:03:07 +0000249Return an \class{AddressList} instance that contains all addresses in
250both \class{AddressList} operands, with duplicates removed (set union).
Guido van Rossum87294831998-06-16 22:27:40 +0000251\end{methoddesc}
252
253\begin{methoddesc}{__sub__}{name}
Fred Drakeae0f2921999-06-10 15:03:07 +0000254Return an \class{AddressList} instance that contains every address in the
255left-hand \class{AddressList} operand that is not present in the right-hand
Guido van Rossum87294831998-06-16 22:27:40 +0000256address operand (set difference).
257\end{methoddesc}
258
259
260Finally, \class{AddressList} instances have one public instance variable:
261
262\begin{memberdesc}{addresslist}
263A list of tuple string pairs, one per address. In each member, the
264first is the canonicalized name part of the address, the second is the
265route-address (@-separated host-domain pair).
266\end{memberdesc}