blob: 641ea85e34b4ec36c9e99f9d109ba0cca5a1bfff [file] [log] [blame]
Guido van Rossuma12ef941995-02-27 17:53:25 +00001\section{Built-in module \sectcode{rfc822}}
2\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
24A \code{Message} instance has the following methods:
25
26\begin{funcdesc}{rewindbody}{}
27Seek to the start of the message body. This only works if the file
28object is seekable.
29\end{funcdesc}
30
31\begin{funcdesc}{getallmatchingheaders}{name}
32Return a list of lines consisting of all headers whose header matches
33\var{name}, if any. Each physical line, whether it is a continuation
34line or not, is a separate list item. Return the empty list if no
35header matches \var{name}.
36\end{funcdesc}
37
38\begin{funcdesc}{getfirstmatchingheader}{name}
39Return a list of lines comprising the first header matching
40\var{name}, and its continuation line(s), if any. Return \code{None}
41if there is no header matching \var{name}.
42\end{funcdesc}
43
44\begin{funcdesc}{getrawheader}{name}
45Return a single string consisting of the text after the colon in the
46first header matching \var{name}. This includes leading whitespace,
47the trailing linefeed, and internal linefeeds and whitespace if there
48any continuation line(s) were present. Return \code{None} if there is
49no header matching \var{name}.
50\end{funcdesc}
51
52\begin{funcdesc}{getheader}{name}
53Like \code{getrawheader(\var{name})}, but strip leading and trailing
54whitespace (but not internal whitespace).
55\end{funcdesc}
56
57\begin{funcdesc}{getaddr}{name}
58Return a pair (full name, email address) parsed from the string
59returned by \code{getheader(\var{name})}. If no header matching
60\var{name} exists, return \code{None, None}; otherwise both the full
61name and the address are (possibly empty )strings.
62
63Example: if \code{m}'s first \code{From} header contains the string
64\code{'guido@cwi.nl (Guido van Rossum)'}, then
65\code{m.getaddr('From')} will yield the pair
66\code{('Guido van Rossum', 'guido\@cwi.nl')}.
67If the header contained
68\code{'Guido van Rossum <guido\@cwi.nl>'} instead, it would yield the
69exact same result.
70\end{funcdesc}
71
72\begin{funcdesc}{getaddrlist}{name}
73This is similar to \code{getaddr(\var{list})}, but parses a header
74containing a list of email addresses (e.g. a \code{To} header) and
75returns a list of (full name, email address) pairs (even if there was
76only one address in the header). If there is no header matching
77\var{name}, return an empty list.
78
79XXX The current version of this function is not really correct. It
80yields bogus results if a full name contains a comma.
81\end{funcdesc}
82
83\begin{funcdesc}{getdate}{name}
84Retrieve a header using \code{getheader} and parse it into a 9-tuple
85compatible with \code{time.kmtime()}. If there is no header matching
86\var{name}, or it is unparsable, return \code{None}.
87
88Date parsing appears to be a black art, and not all mailers adhere to
89the standard. While it has been tested and found correct on a large
90collection of email from many sources, it is still possible that this
91function may occasionally yield an incorrect result.
92\end{funcdesc}
93
94\code{Message} instances also support a read-only mapping interface.
95In particular: \code{m[name]} is the same as \code{m.getheader(name)};
96and \code{len(m)}, \code{m.has_key(name)}, \code{m.keys()},
97\code{m.values()} and \code{m.items()} act as expected (and
98consistently).
99
100Finally, \code{Message} instances have two public instance variables:
101
102\begin{datadesc}{headers}
103A list containing the entire set of header lines, in the order in
104which they were read. Each line contains a trailing newline. The
105blank line terminating the headers is not contained in the list.
106\end{datadesc}
107
108\begin{datadesc}{fp}
109The file object passed at instantiation time.
110\end{datadesc}