Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{rfc822}} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 2 | \label{module-rfc822} |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 3 | \stmodindex{rfc822} |
| 4 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 5 | \setindexsubitem{(in module rfc822)} |
Guido van Rossum | 8675115 | 1995-02-28 17:14:32 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 7 | This module defines a class, \code{Message}, which represents a |
| 8 | collection of ``email headers'' as defined by the Internet standard |
Fred Drake | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 9 | \rfc{822}. It is used in various contexts, usually to read such |
| 10 | headers from a file. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 11 | |
Fred Drake | 5ca9033 | 1997-12-16 15:19:47 +0000 | [diff] [blame] | 12 | Note that there's a separate module to read \UNIX{}, MH, and MMDF |
| 13 | style mailbox files: \code{mailbox}. |
| 14 | \refstmodindex{mailbox} |
Guido van Rossum | 067a2ac | 1997-06-02 17:30:03 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 16 | A \code{Message} instance is instantiated with an open file object as |
Guido van Rossum | 067a2ac | 1997-06-02 17:30:03 +0000 | [diff] [blame] | 17 | parameter. The optional \code{seekable} parameter indicates if the |
| 18 | file object is seekable; the default value is 1 for true. |
| 19 | Instantiation reads headers from the file up to a blank line and |
| 20 | stores them in the instance; after instantiation, the file is |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 21 | positioned directly after the blank line that terminates the headers. |
| 22 | |
| 23 | Input lines as read from the file may either be terminated by CR-LF or |
| 24 | by a single linefeed; a terminating CR-LF is replaced by a single |
| 25 | linefeed before the line is stored. |
| 26 | |
| 27 | All header matching is done independent of upper or lower case; |
| 28 | e.g. \code{m['From']}, \code{m['from']} and \code{m['FROM']} all yield |
| 29 | the same result. |
| 30 | |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 31 | \begin{funcdesc}{parsedate}{date} |
Fred Drake | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 32 | Attempts to parse a date according to the rules in \rfc{822}. however, |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 33 | some mailers don't follow that format as specified, so |
| 34 | \code{parsedate()} tries to guess correctly in such cases. |
Fred Drake | c589124 | 1998-02-09 19:16:20 +0000 | [diff] [blame] | 35 | \var{date} is a string containing an \rfc{822} date, such as |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 36 | \code{"Mon, 20 Nov 1995 19:12:08 -0500"}. If it succeeds in parsing |
| 37 | the date, \code{parsedate()} returns a 9-tuple that can be passed |
| 38 | directly to \code{time.mktime()}; otherwise \code{None} will be |
| 39 | returned. |
| 40 | \end{funcdesc} |
| 41 | |
| 42 | \begin{funcdesc}{parsedate_tz}{date} |
Guido van Rossum | 8cf94e6 | 1998-02-18 05:09:14 +0000 | [diff] [blame^] | 43 | Performs the same function as \code{parsedate()}, but returns either |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 44 | \code{None} or a 10-tuple; the first 9 elements make up a tuple that |
| 45 | can be passed directly to \code{time.mktime()}, and the tenth is the |
| 46 | offset of the date's time zone from UTC (which is the official term |
| 47 | for Greenwich Mean Time). |
| 48 | \end{funcdesc} |
| 49 | |
Guido van Rossum | 8cf94e6 | 1998-02-18 05:09:14 +0000 | [diff] [blame^] | 50 | \begin{funcdesc}{mktime_tz}{tuple} |
| 51 | Turn a 10-tuple as returned by \code{parsedate_tz()} into a UTC timestamp. |
| 52 | Minor deficiency: this first interprets the first 8 elements as a |
| 53 | local time and then compensates for the timezone difference; |
| 54 | this may yield a slight error around daylight savings time |
| 55 | switch dates. Not enough to worry about for common use. |
| 56 | \end{funcdesc} |
| 57 | |
Guido van Rossum | ecde781 | 1995-03-28 13:35:14 +0000 | [diff] [blame] | 58 | \subsection{Message Objects} |
| 59 | |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 60 | A \code{Message} instance has the following methods: |
| 61 | |
| 62 | \begin{funcdesc}{rewindbody}{} |
| 63 | Seek to the start of the message body. This only works if the file |
| 64 | object is seekable. |
| 65 | \end{funcdesc} |
| 66 | |
| 67 | \begin{funcdesc}{getallmatchingheaders}{name} |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 68 | Return a list of lines consisting of all headers matching |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 69 | \var{name}, if any. Each physical line, whether it is a continuation |
| 70 | line or not, is a separate list item. Return the empty list if no |
| 71 | header matches \var{name}. |
| 72 | \end{funcdesc} |
| 73 | |
| 74 | \begin{funcdesc}{getfirstmatchingheader}{name} |
| 75 | Return a list of lines comprising the first header matching |
| 76 | \var{name}, and its continuation line(s), if any. Return \code{None} |
| 77 | if there is no header matching \var{name}. |
| 78 | \end{funcdesc} |
| 79 | |
| 80 | \begin{funcdesc}{getrawheader}{name} |
| 81 | Return a single string consisting of the text after the colon in the |
| 82 | first header matching \var{name}. This includes leading whitespace, |
| 83 | the trailing linefeed, and internal linefeeds and whitespace if there |
| 84 | any continuation line(s) were present. Return \code{None} if there is |
| 85 | no header matching \var{name}. |
| 86 | \end{funcdesc} |
| 87 | |
| 88 | \begin{funcdesc}{getheader}{name} |
| 89 | Like \code{getrawheader(\var{name})}, but strip leading and trailing |
| 90 | whitespace (but not internal whitespace). |
| 91 | \end{funcdesc} |
| 92 | |
| 93 | \begin{funcdesc}{getaddr}{name} |
| 94 | Return a pair (full name, email address) parsed from the string |
| 95 | returned by \code{getheader(\var{name})}. If no header matching |
| 96 | \var{name} exists, return \code{None, None}; otherwise both the full |
| 97 | name and the address are (possibly empty )strings. |
| 98 | |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 99 | Example: If \code{m}'s first \code{From} header contains the string\\ |
| 100 | \code{'jack@cwi.nl (Jack Jansen)'}, then |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 101 | \code{m.getaddr('From')} will yield the pair |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 102 | \code{('Jack Jansen', 'jack@cwi.nl')}. |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 103 | If the header contained |
Guido van Rossum | 470be14 | 1995-03-17 16:07:09 +0000 | [diff] [blame] | 104 | \code{'Jack Jansen <jack@cwi.nl>'} instead, it would yield the |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 105 | exact same result. |
| 106 | \end{funcdesc} |
| 107 | |
| 108 | \begin{funcdesc}{getaddrlist}{name} |
| 109 | This is similar to \code{getaddr(\var{list})}, but parses a header |
| 110 | containing a list of email addresses (e.g. a \code{To} header) and |
| 111 | returns a list of (full name, email address) pairs (even if there was |
| 112 | only one address in the header). If there is no header matching |
| 113 | \var{name}, return an empty list. |
| 114 | |
| 115 | XXX The current version of this function is not really correct. It |
| 116 | yields bogus results if a full name contains a comma. |
| 117 | \end{funcdesc} |
| 118 | |
| 119 | \begin{funcdesc}{getdate}{name} |
| 120 | Retrieve a header using \code{getheader} and parse it into a 9-tuple |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 121 | compatible with \code{time.mktime()}. If there is no header matching |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 122 | \var{name}, or it is unparsable, return \code{None}. |
| 123 | |
| 124 | Date parsing appears to be a black art, and not all mailers adhere to |
| 125 | the standard. While it has been tested and found correct on a large |
| 126 | collection of email from many sources, it is still possible that this |
| 127 | function may occasionally yield an incorrect result. |
| 128 | \end{funcdesc} |
| 129 | |
Guido van Rossum | 843e712 | 1996-12-06 21:23:53 +0000 | [diff] [blame] | 130 | \begin{funcdesc}{getdate_tz}{name} |
| 131 | Retrieve a header using \code{getheader} and parse it into a 10-tuple; |
| 132 | the first 9 elements will make a tuple compatible with |
| 133 | \code{time.mktime()}, and the 10th is a number giving the offset of |
| 134 | the date's time zone from UTC. Similarly to \code{getdate()}, if |
| 135 | there is no header matching \var{name}, or it is unparsable, return |
| 136 | \code{None}. |
| 137 | \end{funcdesc} |
| 138 | |
Guido van Rossum | a12ef94 | 1995-02-27 17:53:25 +0000 | [diff] [blame] | 139 | \code{Message} instances also support a read-only mapping interface. |
| 140 | In particular: \code{m[name]} is the same as \code{m.getheader(name)}; |
| 141 | and \code{len(m)}, \code{m.has_key(name)}, \code{m.keys()}, |
| 142 | \code{m.values()} and \code{m.items()} act as expected (and |
| 143 | consistently). |
| 144 | |
| 145 | Finally, \code{Message} instances have two public instance variables: |
| 146 | |
| 147 | \begin{datadesc}{headers} |
| 148 | A list containing the entire set of header lines, in the order in |
| 149 | which they were read. Each line contains a trailing newline. The |
| 150 | blank line terminating the headers is not contained in the list. |
| 151 | \end{datadesc} |
| 152 | |
| 153 | \begin{datadesc}{fp} |
| 154 | The file object passed at instantiation time. |
| 155 | \end{datadesc} |