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