blob: cd3d271cdf9224b031603bd496d822467247b010 [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\renewcommand{\indexsubitem}{(in module rfc822)}
6
Guido van Rossuma12ef941995-02-27 17:53:25 +00007This module defines a class, \code{Message}, which represents a
8collection of ``email headers'' as defined by the Internet standard
9RFC 822. It is used in various contexts, usually to read such headers
10from a file.
11
Guido van Rossum067a2ac1997-06-02 17:30:03 +000012(Note that there's a separate, currently undocumented, module to read
13Unix style mailbox files: \code{mailbox}.)
14
Guido van Rossuma12ef941995-02-27 17:53:25 +000015A \code{Message} instance is instantiated with an open file object as
Guido van Rossum067a2ac1997-06-02 17:30:03 +000016parameter. The optional \code{seekable} parameter indicates if the
17file object is seekable; the default value is 1 for true.
18Instantiation 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;
27e.g. \code{m['From']}, \code{m['from']} and \code{m['FROM']} all yield
28the same result.
29
Guido van Rossum843e7121996-12-06 21:23:53 +000030\begin{funcdesc}{parsedate}{date}
31Attempts to parse a date according to the rules in RFC822. however,
32some mailers don't follow that format as specified, so
33\code{parsedate()} tries to guess correctly in such cases.
34\var{date} is a string containing an RFC822 date, such as
35\code{"Mon, 20 Nov 1995 19:12:08 -0500"}. If it succeeds in parsing
36the date, \code{parsedate()} returns a 9-tuple that can be passed
37directly to \code{time.mktime()}; otherwise \code{None} will be
38returned.
39\end{funcdesc}
40
41\begin{funcdesc}{parsedate_tz}{date}
42Performs the same function as \code{parsedate}, but returns either
43\code{None} or a 10-tuple; the first 9 elements make up a tuple that
44can be passed directly to \code{time.mktime()}, and the tenth is the
45offset of the date's time zone from UTC (which is the official term
46for Greenwich Mean Time).
47\end{funcdesc}
48
Guido van Rossumecde7811995-03-28 13:35:14 +000049\subsection{Message Objects}
50
Guido van Rossuma12ef941995-02-27 17:53:25 +000051A \code{Message} instance has the following methods:
52
53\begin{funcdesc}{rewindbody}{}
54Seek to the start of the message body. This only works if the file
55object is seekable.
56\end{funcdesc}
57
58\begin{funcdesc}{getallmatchingheaders}{name}
Guido van Rossum6c4f0031995-03-07 10:14:09 +000059Return a list of lines consisting of all headers matching
Guido van Rossuma12ef941995-02-27 17:53:25 +000060\var{name}, if any. Each physical line, whether it is a continuation
61line or not, is a separate list item. Return the empty list if no
62header matches \var{name}.
63\end{funcdesc}
64
65\begin{funcdesc}{getfirstmatchingheader}{name}
66Return a list of lines comprising the first header matching
67\var{name}, and its continuation line(s), if any. Return \code{None}
68if there is no header matching \var{name}.
69\end{funcdesc}
70
71\begin{funcdesc}{getrawheader}{name}
72Return a single string consisting of the text after the colon in the
73first header matching \var{name}. This includes leading whitespace,
74the trailing linefeed, and internal linefeeds and whitespace if there
75any continuation line(s) were present. Return \code{None} if there is
76no header matching \var{name}.
77\end{funcdesc}
78
79\begin{funcdesc}{getheader}{name}
80Like \code{getrawheader(\var{name})}, but strip leading and trailing
81whitespace (but not internal whitespace).
82\end{funcdesc}
83
84\begin{funcdesc}{getaddr}{name}
85Return a pair (full name, email address) parsed from the string
86returned by \code{getheader(\var{name})}. If no header matching
87\var{name} exists, return \code{None, None}; otherwise both the full
88name and the address are (possibly empty )strings.
89
Guido van Rossum470be141995-03-17 16:07:09 +000090Example: If \code{m}'s first \code{From} header contains the string\\
91\code{'jack@cwi.nl (Jack Jansen)'}, then
Guido van Rossuma12ef941995-02-27 17:53:25 +000092\code{m.getaddr('From')} will yield the pair
Guido van Rossum470be141995-03-17 16:07:09 +000093\code{('Jack Jansen', 'jack@cwi.nl')}.
Guido van Rossuma12ef941995-02-27 17:53:25 +000094If the header contained
Guido van Rossum470be141995-03-17 16:07:09 +000095\code{'Jack Jansen <jack@cwi.nl>'} instead, it would yield the
Guido van Rossuma12ef941995-02-27 17:53:25 +000096exact same result.
97\end{funcdesc}
98
99\begin{funcdesc}{getaddrlist}{name}
100This is similar to \code{getaddr(\var{list})}, but parses a header
101containing a list of email addresses (e.g. a \code{To} header) and
102returns a list of (full name, email address) pairs (even if there was
103only one address in the header). If there is no header matching
104\var{name}, return an empty list.
105
106XXX The current version of this function is not really correct. It
107yields bogus results if a full name contains a comma.
108\end{funcdesc}
109
110\begin{funcdesc}{getdate}{name}
111Retrieve a header using \code{getheader} and parse it into a 9-tuple
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000112compatible with \code{time.mktime()}. If there is no header matching
Guido van Rossuma12ef941995-02-27 17:53:25 +0000113\var{name}, or it is unparsable, return \code{None}.
114
115Date parsing appears to be a black art, and not all mailers adhere to
116the standard. While it has been tested and found correct on a large
117collection of email from many sources, it is still possible that this
118function may occasionally yield an incorrect result.
119\end{funcdesc}
120
Guido van Rossum843e7121996-12-06 21:23:53 +0000121\begin{funcdesc}{getdate_tz}{name}
122Retrieve a header using \code{getheader} and parse it into a 10-tuple;
123the first 9 elements will make a tuple compatible with
124\code{time.mktime()}, and the 10th is a number giving the offset of
125the date's time zone from UTC. Similarly to \code{getdate()}, if
126there is no header matching \var{name}, or it is unparsable, return
127\code{None}.
128\end{funcdesc}
129
Guido van Rossuma12ef941995-02-27 17:53:25 +0000130\code{Message} instances also support a read-only mapping interface.
131In particular: \code{m[name]} is the same as \code{m.getheader(name)};
132and \code{len(m)}, \code{m.has_key(name)}, \code{m.keys()},
133\code{m.values()} and \code{m.items()} act as expected (and
134consistently).
135
136Finally, \code{Message} instances have two public instance variables:
137
138\begin{datadesc}{headers}
139A list containing the entire set of header lines, in the order in
140which they were read. Each line contains a trailing newline. The
141blank line terminating the headers is not contained in the list.
142\end{datadesc}
143
144\begin{datadesc}{fp}
145The file object passed at instantiation time.
146\end{datadesc}