blob: e028fcd7366732f41d4bb248dd2ae7f086dc1d40 [file] [log] [blame]
Barry Warsaw5e634632001-09-26 05:23:47 +00001\section{\module{email.Utils} ---
2 Miscellaneous email package utilities}
3
4\declaremodule{standard}{email.Utils}
5\modulesynopsis{Miscellaneous email package utilities.}
6\sectionauthor{Barry A. Warsaw}{barry@zope.com}
7
8\versionadded{2.2}
9
10There are several useful utilities provided with the \module{email}
11package.
12
13\begin{funcdesc}{quote}{str}
14Return a new string with backslashes in \var{str} replaced by two
15backslashes and double quotes replaced by backslash-double quote.
16\end{funcdesc}
17
18\begin{funcdesc}{unquote}{str}
19Return a new string which is an \emph{unquoted} version of \var{str}.
20If \var{str} ends and begins with double quotes, they are stripped
21off. Likewise if \var{str} ends and begins with angle brackets, they
22are stripped off.
23\end{funcdesc}
24
25\begin{funcdesc}{parseaddr}{address}
26Parse address -- which should be the value of some address-containing
27field such as \code{To:} or \code{Cc:} -- into its constituent
28``realname'' and ``email address'' parts. Returns a tuple of that
29information, unless the parse fails, in which case a 2-tuple of
30\code{(None, None)} is returned.
31\end{funcdesc}
32
33\begin{funcdesc}{dump_address_pair}{pair}
34The inverse of \method{parseaddr()}, this takes a 2-tuple of the form
35\code{(realname, email_address)} and returns the string value suitable
36for a \code{To:} or \code{Cc:} header. If the first element of
37\var{pair} is false, then the second element is returned unmodified.
38\end{funcdesc}
39
40\begin{funcdesc}{getaddresses}{fieldvalues}
41This method returns a list of 2-tuples of the form returned by
42\code{parseaddr()}. \var{fieldvalues} is a sequence of header field
43values as might be returned by \method{Message.getall()}. Here's a
44simple example that gets all the recipients of a message:
45
46\begin{verbatim}
47from email.Utils import getaddresses
48
49tos = msg.get_all('to')
50ccs = msg.get_all('cc')
51resent_tos = msg.get_all('resent-to')
52resent_ccs = msg.get_all('resent-cc')
53all_recipients = getaddresses(tos + ccs + resent_tos + resent_ccs)
54\end{verbatim}
55\end{funcdesc}
56
57\begin{funcdesc}{decode}{s}
58This method decodes a string according to the rules in \rfc{2047}. It
59returns the decoded string as a Python unicode string.
60\end{funcdesc}
61
62\begin{funcdesc}{encode}{s\optional{, charset\optional{, encoding}}}
63This method encodes a string according to the rules in \rfc{2047}. It
64is not actually the inverse of \function{decode()} since it doesn't
65handle multiple character sets or multiple string parts needing
66encoding. In fact, the input string \var{s} must already be encoded
67in the \var{charset} character set (Python can't reliably guess what
68character set a string might be encoded in). The default
69\var{charset} is \samp{iso-8859-1}.
70
71\var{encoding} must be either the letter \samp{q} for
72Quoted-Printable or \samp{b} for Base64 encoding. If
73neither, a \code{ValueError} is raised. Both the \var{charset} and
74the \var{encoding} strings are case-insensitive, and coerced to lower
75case in the returned string.
76\end{funcdesc}
77
78\begin{funcdesc}{parsedate}{date}
79Attempts to parse a date according to the rules in \rfc{2822}.
80however, some mailers don't follow that format as specified, so
81\function{parsedate()} tries to guess correctly in such cases.
82\var{date} is a string containing an \rfc{2822} date, such as
83\code{"Mon, 20 Nov 1995 19:12:08 -0500"}. If it succeeds in parsing
84the date, \function{parsedate()} returns a 9-tuple that can be passed
85directly to \function{time.mktime()}; otherwise \code{None} will be
86returned. Note that fields 6, 7, and 8 of the result tuple are not
87usable.
88\end{funcdesc}
89
90\begin{funcdesc}{parsedate_tz}{date}
91Performs the same function as \function{parsedate()}, but returns
92either \code{None} or a 10-tuple; the first 9 elements make up a tuple
93that can be passed directly to \function{time.mktime()}, and the tenth
94is the offset of the date's timezone from UTC (which is the official
95term for Greenwich Mean Time)\footnote{Note that the sign of the timezone
96offset is the opposite of the sign of the \code{time.timezone}
97variable for the same timezone; the latter variable follows the
98\POSIX{} standard while this module follows \rfc{2822}.}. If the input
99string has no timezone, the last element of the tuple returned is
100\code{None}. Note that fields 6, 7, and 8 of the result tuple are not
101usable.
102\end{funcdesc}
103
104\begin{funcdesc}{mktime_tz}{tuple}
105Turn a 10-tuple as returned by \function{parsedate_tz()} into a UTC
106timestamp. It the timezone item in the tuple is \code{None}, assume
107local time. Minor deficiency: \function{mktime_tz()} interprets the
108first 8 elements of \var{tuple} as a local time and then compensates
109for the timezone difference. This may yield a slight error around
110changes in daylight savings time, though not worth worring about for
111common use.
112\end{funcdesc}
113
114\begin{funcdesc}{formatdate}{\optional{timeval}}
115Returns the time formatted as per Internet standards \rfc{2822}
116and updated by \rfc{1123}. If \var{timeval} is provided, then it
117should be a floating point time value as expected by
118\method{time.gmtime()}, otherwise the current time is used.
119\end{funcdesc}