blob: 9b2094feb9ffa8e81b3d33124314f2ea54f27e46 [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Standard Module \sectcode{rfc822}}
Guido van Rossuma12ef941995-02-27 17:53:25 +00002\stmodindex{rfc822}
3
Guido van Rossum86751151995-02-28 17:14:32 +00004\renewcommand{\indexsubitem}{(in module rfc822)}
5
Guido van Rossuma12ef941995-02-27 17:53:25 +00006This module defines a class, \code{Message}, which represents a
7collection of ``email headers'' as defined by the Internet standard
8RFC 822. It is used in various contexts, usually to read such headers
9from a file.
10
11A \code{Message} instance is instantiated with an open file object as
12parameter. Instantiation reads headers from the file up to a blank
13line and stores them in the instance; after instantiation, the file is
14positioned directly after the blank line that terminates the headers.
15
16Input lines as read from the file may either be terminated by CR-LF or
17by a single linefeed; a terminating CR-LF is replaced by a single
18linefeed before the line is stored.
19
20All header matching is done independent of upper or lower case;
21e.g. \code{m['From']}, \code{m['from']} and \code{m['FROM']} all yield
22the same result.
23
Guido van Rossum843e7121996-12-06 21:23:53 +000024\begin{funcdesc}{parsedate}{date}
25Attempts to parse a date according to the rules in RFC822. however,
26some mailers don't follow that format as specified, so
27\code{parsedate()} tries to guess correctly in such cases.
28\var{date} is a string containing an RFC822 date, such as
29\code{"Mon, 20 Nov 1995 19:12:08 -0500"}. If it succeeds in parsing
30the date, \code{parsedate()} returns a 9-tuple that can be passed
31directly to \code{time.mktime()}; otherwise \code{None} will be
32returned.
33\end{funcdesc}
34
35\begin{funcdesc}{parsedate_tz}{date}
36Performs the same function as \code{parsedate}, but returns either
37\code{None} or a 10-tuple; the first 9 elements make up a tuple that
38can be passed directly to \code{time.mktime()}, and the tenth is the
39offset of the date's time zone from UTC (which is the official term
40for Greenwich Mean Time).
41\end{funcdesc}
42
Guido van Rossumecde7811995-03-28 13:35:14 +000043\subsection{Message Objects}
44
Guido van Rossuma12ef941995-02-27 17:53:25 +000045A \code{Message} instance has the following methods:
46
47\begin{funcdesc}{rewindbody}{}
48Seek to the start of the message body. This only works if the file
49object is seekable.
50\end{funcdesc}
51
52\begin{funcdesc}{getallmatchingheaders}{name}
Guido van Rossum6c4f0031995-03-07 10:14:09 +000053Return a list of lines consisting of all headers matching
Guido van Rossuma12ef941995-02-27 17:53:25 +000054\var{name}, if any. Each physical line, whether it is a continuation
55line or not, is a separate list item. Return the empty list if no
56header matches \var{name}.
57\end{funcdesc}
58
59\begin{funcdesc}{getfirstmatchingheader}{name}
60Return a list of lines comprising the first header matching
61\var{name}, and its continuation line(s), if any. Return \code{None}
62if there is no header matching \var{name}.
63\end{funcdesc}
64
65\begin{funcdesc}{getrawheader}{name}
66Return a single string consisting of the text after the colon in the
67first header matching \var{name}. This includes leading whitespace,
68the trailing linefeed, and internal linefeeds and whitespace if there
69any continuation line(s) were present. Return \code{None} if there is
70no header matching \var{name}.
71\end{funcdesc}
72
73\begin{funcdesc}{getheader}{name}
74Like \code{getrawheader(\var{name})}, but strip leading and trailing
75whitespace (but not internal whitespace).
76\end{funcdesc}
77
78\begin{funcdesc}{getaddr}{name}
79Return a pair (full name, email address) parsed from the string
80returned by \code{getheader(\var{name})}. If no header matching
81\var{name} exists, return \code{None, None}; otherwise both the full
82name and the address are (possibly empty )strings.
83
Guido van Rossum470be141995-03-17 16:07:09 +000084Example: If \code{m}'s first \code{From} header contains the string\\
85\code{'jack@cwi.nl (Jack Jansen)'}, then
Guido van Rossuma12ef941995-02-27 17:53:25 +000086\code{m.getaddr('From')} will yield the pair
Guido van Rossum470be141995-03-17 16:07:09 +000087\code{('Jack Jansen', 'jack@cwi.nl')}.
Guido van Rossuma12ef941995-02-27 17:53:25 +000088If the header contained
Guido van Rossum470be141995-03-17 16:07:09 +000089\code{'Jack Jansen <jack@cwi.nl>'} instead, it would yield the
Guido van Rossuma12ef941995-02-27 17:53:25 +000090exact same result.
91\end{funcdesc}
92
93\begin{funcdesc}{getaddrlist}{name}
94This is similar to \code{getaddr(\var{list})}, but parses a header
95containing a list of email addresses (e.g. a \code{To} header) and
96returns a list of (full name, email address) pairs (even if there was
97only one address in the header). If there is no header matching
98\var{name}, return an empty list.
99
100XXX The current version of this function is not really correct. It
101yields bogus results if a full name contains a comma.
102\end{funcdesc}
103
104\begin{funcdesc}{getdate}{name}
105Retrieve a header using \code{getheader} and parse it into a 9-tuple
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000106compatible with \code{time.mktime()}. If there is no header matching
Guido van Rossuma12ef941995-02-27 17:53:25 +0000107\var{name}, or it is unparsable, return \code{None}.
108
109Date parsing appears to be a black art, and not all mailers adhere to
110the standard. While it has been tested and found correct on a large
111collection of email from many sources, it is still possible that this
112function may occasionally yield an incorrect result.
113\end{funcdesc}
114
Guido van Rossum843e7121996-12-06 21:23:53 +0000115\begin{funcdesc}{getdate_tz}{name}
116Retrieve a header using \code{getheader} and parse it into a 10-tuple;
117the first 9 elements will make a tuple compatible with
118\code{time.mktime()}, and the 10th is a number giving the offset of
119the date's time zone from UTC. Similarly to \code{getdate()}, if
120there is no header matching \var{name}, or it is unparsable, return
121\code{None}.
122\end{funcdesc}
123
Guido van Rossuma12ef941995-02-27 17:53:25 +0000124\code{Message} instances also support a read-only mapping interface.
125In particular: \code{m[name]} is the same as \code{m.getheader(name)};
126and \code{len(m)}, \code{m.has_key(name)}, \code{m.keys()},
127\code{m.values()} and \code{m.items()} act as expected (and
128consistently).
129
130Finally, \code{Message} instances have two public instance variables:
131
132\begin{datadesc}{headers}
133A list containing the entire set of header lines, in the order in
134which they were read. Each line contains a trailing newline. The
135blank line terminating the headers is not contained in the list.
136\end{datadesc}
137
138\begin{datadesc}{fp}
139The file object passed at instantiation time.
140\end{datadesc}