blob: bb192cd8c3ab75c7de088e46109f6c3b7c8decad [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Standard Module \sectcode{rfc822}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-rfc822}
Guido van Rossuma12ef941995-02-27 17:53:25 +00003\stmodindex{rfc822}
4
Guido van Rossum86751151995-02-28 17:14:32 +00005
Fred Drakecdea8a31998-03-14 06:17:43 +00006This module defines a class, \class{Message}, which represents a
Guido van Rossuma12ef941995-02-27 17:53:25 +00007collection of ``email headers'' as defined by the Internet standard
Fred Drakec5891241998-02-09 19:16:20 +00008\rfc{822}. It is used in various contexts, usually to read such
9headers from a file.
Guido van Rossuma12ef941995-02-27 17:53:25 +000010
Fred Drake5ca90331997-12-16 15:19:47 +000011Note that there's a separate module to read \UNIX{}, MH, and MMDF
Fred Drakecdea8a31998-03-14 06:17:43 +000012style mailbox files: \module{mailbox}\refstmodindex{mailbox}.
Guido van Rossum067a2ac1997-06-02 17:30:03 +000013
Fred Drakecdea8a31998-03-14 06:17:43 +000014\begin{classdesc}{Message}{file\optional{, seekable}}
15A \class{Message} instance is instantiated with an open file object as
16parameter. The optional \var{seekable} parameter indicates if the
17file object is seekable; the default value is \code{1} for true.
Guido van Rossum067a2ac1997-06-02 17:30:03 +000018Instantiation reads headers from the file up to a blank line and
19stores them in the instance; after instantiation, the file is
Guido van Rossuma12ef941995-02-27 17:53:25 +000020positioned directly after the blank line that terminates the headers.
21
22Input lines as read from the file may either be terminated by CR-LF or
23by a single linefeed; a terminating CR-LF is replaced by a single
24linefeed before the line is stored.
25
26All header matching is done independent of upper or lower case;
Fred Drakecdea8a31998-03-14 06:17:43 +000027e.g. \code{\var{m}['From']}, \code{\var{m}['from']} and
28\code{\var{m}['FROM']} all yield the same result.
29\end{classdesc}
Guido van Rossuma12ef941995-02-27 17:53:25 +000030
Guido van Rossum843e7121996-12-06 21:23:53 +000031\begin{funcdesc}{parsedate}{date}
Fred Drakecdea8a31998-03-14 06:17:43 +000032Attempts to parse a date according to the rules in \rfc{822}.
33however, some mailers don't follow that format as specified, so
34\function{parsedate()} tries to guess correctly in such cases.
Fred Drakec5891241998-02-09 19:16:20 +000035\var{date} is a string containing an \rfc{822} date, such as
Fred Drakecdea8a31998-03-14 06:17:43 +000036\code{'Mon, 20 Nov 1995 19:12:08 -0500'}. If it succeeds in parsing
37the date, \function{parsedate()} returns a 9-tuple that can be passed
38directly to \function{time.mktime()}; otherwise \code{None} will be
Guido van Rossum843e7121996-12-06 21:23:53 +000039returned.
40\end{funcdesc}
41
42\begin{funcdesc}{parsedate_tz}{date}
Fred Drakecdea8a31998-03-14 06:17:43 +000043Performs the same function as \function{parsedate()}, but returns
44either \code{None} or a 10-tuple; the first 9 elements make up a tuple
45that can be passed directly to \function{time.mktime()}, and the tenth
46is the offset of the date's timezone from UTC (which is the official
47term for Greenwich Mean Time). (Note that the sign of the timezone
48offset is the opposite of the sign of the \code{time.timezone}
49variable for the same timezone; the latter variable follows the
50\POSIX{} standard while this module follows \rfc{822}.) If the input
51string has no timezone, the last element of the tuple returned is
52\code{None}.
Guido van Rossum843e7121996-12-06 21:23:53 +000053\end{funcdesc}
54
Guido van Rossum8cf94e61998-02-18 05:09:14 +000055\begin{funcdesc}{mktime_tz}{tuple}
Fred Drakecdea8a31998-03-14 06:17:43 +000056Turn a 10-tuple as returned by \function{parsedate_tz()} into a UTC
57timestamp. It the timezone item in the tuple is \code{None}, assume
58local time. Minor deficiency: this first interprets the first 8
59elements as a local time and then compensates for the timezone
60difference; this may yield a slight error around daylight savings time
Guido van Rossum8cf94e61998-02-18 05:09:14 +000061switch dates. Not enough to worry about for common use.
62\end{funcdesc}
63
Guido van Rossumecde7811995-03-28 13:35:14 +000064\subsection{Message Objects}
65
Fred Drakecdea8a31998-03-14 06:17:43 +000066A \class{Message} instance has the following methods:
Guido van Rossuma12ef941995-02-27 17:53:25 +000067
68\begin{funcdesc}{rewindbody}{}
69Seek to the start of the message body. This only works if the file
70object is seekable.
71\end{funcdesc}
72
73\begin{funcdesc}{getallmatchingheaders}{name}
Guido van Rossum6c4f0031995-03-07 10:14:09 +000074Return a list of lines consisting of all headers matching
Guido van Rossuma12ef941995-02-27 17:53:25 +000075\var{name}, if any. Each physical line, whether it is a continuation
76line or not, is a separate list item. Return the empty list if no
77header matches \var{name}.
78\end{funcdesc}
79
80\begin{funcdesc}{getfirstmatchingheader}{name}
81Return a list of lines comprising the first header matching
82\var{name}, and its continuation line(s), if any. Return \code{None}
83if there is no header matching \var{name}.
84\end{funcdesc}
85
86\begin{funcdesc}{getrawheader}{name}
87Return a single string consisting of the text after the colon in the
88first header matching \var{name}. This includes leading whitespace,
89the trailing linefeed, and internal linefeeds and whitespace if there
90any continuation line(s) were present. Return \code{None} if there is
91no header matching \var{name}.
92\end{funcdesc}
93
94\begin{funcdesc}{getheader}{name}
95Like \code{getrawheader(\var{name})}, but strip leading and trailing
Fred Drakecdea8a31998-03-14 06:17:43 +000096whitespace. Internal whitespace is not stripped.
Guido van Rossuma12ef941995-02-27 17:53:25 +000097\end{funcdesc}
98
99\begin{funcdesc}{getaddr}{name}
Fred Drakecdea8a31998-03-14 06:17:43 +0000100Return a pair \code{(\var{full name}, \var{email address})} parsed
101from the string returned by \code{getheader(\var{name})}. If no
102header matching \var{name} exists, return \code{(None, None)};
103otherwise both the full name and the address are (possibly empty)
104strings.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000105
Fred Drakecdea8a31998-03-14 06:17:43 +0000106Example: If \var{m}'s first \code{From} header contains the string
Guido van Rossum470be141995-03-17 16:07:09 +0000107\code{'jack@cwi.nl (Jack Jansen)'}, then
Guido van Rossuma12ef941995-02-27 17:53:25 +0000108\code{m.getaddr('From')} will yield the pair
Guido van Rossum470be141995-03-17 16:07:09 +0000109\code{('Jack Jansen', 'jack@cwi.nl')}.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000110If the header contained
Guido van Rossum470be141995-03-17 16:07:09 +0000111\code{'Jack Jansen <jack@cwi.nl>'} instead, it would yield the
Guido van Rossuma12ef941995-02-27 17:53:25 +0000112exact same result.
113\end{funcdesc}
114
115\begin{funcdesc}{getaddrlist}{name}
116This is similar to \code{getaddr(\var{list})}, but parses a header
117containing a list of email addresses (e.g. a \code{To} header) and
Fred Drakecdea8a31998-03-14 06:17:43 +0000118returns a list of \code{(\var{full name}, \var{email address})} pairs
119(even if there was only one address in the header). If there is no
120header matching \var{name}, return an empty list.
Guido van Rossuma12ef941995-02-27 17:53:25 +0000121
122XXX The current version of this function is not really correct. It
123yields bogus results if a full name contains a comma.
124\end{funcdesc}
125
126\begin{funcdesc}{getdate}{name}
Fred Drakecdea8a31998-03-14 06:17:43 +0000127Retrieve a header using \method{getheader()} and parse it into a 9-tuple
128compatible with \function{time.mktime()}. If there is no header matching
Guido van Rossuma12ef941995-02-27 17:53:25 +0000129\var{name}, or it is unparsable, return \code{None}.
130
131Date parsing appears to be a black art, and not all mailers adhere to
132the standard. While it has been tested and found correct on a large
133collection of email from many sources, it is still possible that this
134function may occasionally yield an incorrect result.
135\end{funcdesc}
136
Guido van Rossum843e7121996-12-06 21:23:53 +0000137\begin{funcdesc}{getdate_tz}{name}
Fred Drakecdea8a31998-03-14 06:17:43 +0000138Retrieve a header using \method{getheader()} and parse it into a
13910-tuple; the first 9 elements will make a tuple compatible with
140\function{time.mktime()}, and the 10th is a number giving the offset
141of the date's timezone from UTC. Similarly to \method{getdate()}, if
Guido van Rossum843e7121996-12-06 21:23:53 +0000142there is no header matching \var{name}, or it is unparsable, return
143\code{None}.
144\end{funcdesc}
145
Fred Drakecdea8a31998-03-14 06:17:43 +0000146\class{Message} instances also support a read-only mapping interface.
147In particular: \code{\var{m}[name]} is the same as
148\code{\var{m}.getheader(name)}; and \code{len(\var{m})},
149\code{\var{m}.has_key(name)}, \code{\var{m}.keys()},
150\code{\var{m}.values()} and \code{\var{m}.items()} act as expected
151(and consistently).
Guido van Rossuma12ef941995-02-27 17:53:25 +0000152
Fred Drakecdea8a31998-03-14 06:17:43 +0000153Finally, \class{Message} instances have two public instance variables:
Guido van Rossuma12ef941995-02-27 17:53:25 +0000154
155\begin{datadesc}{headers}
156A list containing the entire set of header lines, in the order in
157which they were read. Each line contains a trailing newline. The
158blank line terminating the headers is not contained in the list.
159\end{datadesc}
160
161\begin{datadesc}{fp}
162The file object passed at instantiation time.
163\end{datadesc}