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