<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!--Converted with LaTeX2HTML 98.2 beta6 (August 14th, 1998)
original version by:  Nikos Drakos, CBLU, University of Leeds
* revised and updated by:  Marcus Hennecke, Ross Moore, Herb Swan
* with significant contributions from:
  Jens Lippmann, Marek Rouchal, Martin Wilck and others -->
<HTML>
<HEAD>
<TITLE>12.5.1 Message Objects </TITLE>
<META NAME="description" CONTENT="12.5.1 Message Objects ">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<LINK REL="STYLESHEET" HREF="lib.css">
<LINK REL="next" HREF="addresslist-objects.html">
<LINK REL="previous" HREF="module-rfc822.html">
<LINK REL="up" HREF="module-rfc822.html">
<LINK REL="next" HREF="addresslist-objects.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="addresslist-objects.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="module-rfc822.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-rfc822.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="addresslist-objects.html">12.5.2 AddressList Objects</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="module-rfc822.html">12.5 rfc822  </A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-rfc822.html">12.5 rfc822  </A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H2><A NAME="SECTION0014510000000000000000"></A>
<BR>
12.5.1 Message Objects 
</H2>

<P>
A <tt class=class>Message</tt> instance has the following methods:

<P>
<dl><dt><b><a name="l2h-1976"><tt class=method>rewindbody</tt></a></b> ()
<dd>
Seek to the start of the message body.  This only works if the file
object is seekable.
</dl>

<P>
<dl><dt><b><a name="l2h-1977"><tt class=method>isheader</tt></a></b> (<var>line</var>)
<dd>
Returns a line's canonicalized fieldname (the dictionary key that will
be used to index it) if the line is a legal <span class=rfc><a name="rfcref-42756"
href="http://info.internet.isi.edu/in-notes/rfc/files/rfc822.txt">RFC 822</a></span> header; otherwise
returns None (implying that parsing should stop here and the line be
pushed back on the input stream).  It is sometimes useful to override
this method in a subclass.
</dl>

<P>
<dl><dt><b><a name="l2h-1978"><tt class=method>islast</tt></a></b> (<var>line</var>)
<dd>
Return true if the given line is a delimiter on which Message should
stop.  The delimiter line is consumed, and the file object's read
location positioned immediately after it.  By default this method just
checks that the line is blank, but you can override it in a subclass.
</dl>

<P>
<dl><dt><b><a name="l2h-1979"><tt class=method>iscomment</tt></a></b> (<var>line</var>)
<dd>
Return true if the given line should be ignored entirely, just skipped.
By default this is a stub that always returns false, but you can
override it in a subclass.
</dl>

<P>
<dl><dt><b><a name="l2h-1980"><tt class=method>getallmatchingheaders</tt></a></b> (<var>name</var>)
<dd>
Return a list of lines consisting of all headers matching
<var>name</var>, if any.  Each physical line, whether it is a continuation
line or not, is a separate list item.  Return the empty list if no
header matches <var>name</var>.
</dl>

<P>
<dl><dt><b><a name="l2h-1981"><tt class=method>getfirstmatchingheader</tt></a></b> (<var>name</var>)
<dd>
Return a list of lines comprising the first header matching
<var>name</var>, and its continuation line(s), if any.  Return
<tt>None</tt> if there is no header matching <var>name</var>.
</dl>

<P>
<dl><dt><b><a name="l2h-1982"><tt class=method>getrawheader</tt></a></b> (<var>name</var>)
<dd>
Return a single string consisting of the text after the colon in the
first header matching <var>name</var>.  This includes leading whitespace,
the trailing linefeed, and internal linefeeds and whitespace if there
any continuation line(s) were present.  Return <tt>None</tt> if there is
no header matching <var>name</var>.
</dl>

<P>
<dl><dt><b><a name="l2h-1983"><tt class=method>getheader</tt></a></b> (<var>name</var><big>[</big><var>, default</var><big>]</big>)
<dd>
Like <tt>getrawheader(<var>name</var>)</tt>, but strip leading and trailing
whitespace.  Internal whitespace is not stripped.  The optional
<var>default</var> argument can be used to specify a different default to
be returned when there is no header matching <var>name</var>.
</dl>

<P>
<dl><dt><b><a name="l2h-1984"><tt class=method>get</tt></a></b> (<var>name</var><big>[</big><var>, default</var><big>]</big>)
<dd>
An alias for <tt class=method>getheader()</tt>, to make the interface more compatible 
with regular dictionaries.
</dl>

<P>
<dl><dt><b><a name="l2h-1985"><tt class=method>getaddr</tt></a></b> (<var>name</var>)
<dd>
Return a pair <tt>(<var>full name</var>, <var>email address</var>)</tt> parsed
from the string returned by <tt>getheader(<var>name</var>)</tt>.  If no
header matching <var>name</var> exists, return <tt>(None, None)</tt>;
otherwise both the full name and the address are (possibly empty)
strings.

<P>
Example: If <var>m</var>'s first <tt>From</tt> header contains the string
<tt>'jack@cwi.nl (Jack Jansen)'</tt>, then
<tt>m.getaddr('From')</tt> will yield the pair
<tt>('Jack Jansen', 'jack@cwi.nl')</tt>.
If the header contained
<tt>'Jack Jansen &lt;jack@cwi.nl&gt;'</tt> instead, it would yield the
exact same result.
</dl>

<P>
<dl><dt><b><a name="l2h-1986"><tt class=method>getaddrlist</tt></a></b> (<var>name</var>)
<dd>
This is similar to <tt>getaddr(<var>list</var>)</tt>, but parses a header
containing a list of email addresses (e.g. a <tt>To</tt> header) and
returns a list of <tt>(<var>full name</var>, <var>email address</var>)</tt> pairs
(even if there was only one address in the header).  If there is no
header matching <var>name</var>, return an empty list.

<P>
If multiple headers exist that match the named header (e.g. if there
are several <tt>Cc</tt> headers), all are parsed for addresses.  Any
continuation lines the named headers contain are also parsed.
</dl>

<P>
<dl><dt><b><a name="l2h-1987"><tt class=method>getdate</tt></a></b> (<var>name</var>)
<dd>
Retrieve a header using <tt class=method>getheader()</tt> and parse it into a 9-tuple
compatible with <tt class=function>time.mktime()</tt>.  If there is no header matching
<var>name</var>, or it is unparsable, return <tt>None</tt>.

<P>
Date parsing appears to be a black art, and not all mailers adhere to
the standard.  While it has been tested and found correct on a large
collection of email from many sources, it is still possible that this
function may occasionally yield an incorrect result.
</dl>

<P>
<dl><dt><b><a name="l2h-1988"><tt class=method>getdate_tz</tt></a></b> (<var>name</var>)
<dd>
Retrieve a header using <tt class=method>getheader()</tt> and parse it into a
10-tuple; the first 9 elements will make a tuple compatible with
<tt class=function>time.mktime()</tt>, and the 10th is a number giving the offset
of the date's timezone from UTC.  Similarly to <tt class=method>getdate()</tt>, if
there is no header matching <var>name</var>, or it is unparsable, return
<tt>None</tt>. 
</dl>

<P>
<tt class=class>Message</tt> instances also support a read-only mapping interface.
In particular: <tt><var>m</var>[name]</tt> is like
<tt><var>m</var>.getheader(name)</tt> but raises <tt class=exception>KeyError</tt> if
there is no matching header; and <tt>len(<var>m</var>)</tt>,
<tt><var>m</var>.has_key(name)</tt>, <tt><var>m</var>.keys()</tt>,
<tt><var>m</var>.values()</tt> and <tt><var>m</var>.items()</tt> act as expected
(and consistently).

<P>
Finally, <tt class=class>Message</tt> instances have two public instance variables:

<P>
<dl><dt><b><a name="l2h-1989"><tt class=member>headers</tt></a></b>
<dd>
A list containing the entire set of header lines, in the order in
which they were read (except that setitem calls may disturb this
order). Each line contains a trailing newline.  The
blank line terminating the headers is not contained in the list.
</dl>

<P>
<dl><dt><b><a name="l2h-1990"><tt class=member>fp</tt></a></b>
<dd>
The file or file-like object passed at instantiation time.  This can
be used to read the message content.
</dl>

<P>
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="addresslist-objects.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="module-rfc822.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="module-rfc822.html"><img width=32 height=32 align=bottom border=0 alt="previous"
 src="../icons/previous.gif"></A></td>
<td align=center bgcolor="#99CCFF" width="100%">
 <b class=title>Python Library Reference</b></td>
<td><A HREF="contents.html"><img width=32 height=32 align=bottom border=0 alt="contents"
 src="../icons/contents.gif"></A></td>
<td><a
 href="modindex.html"><img width=32 height=32 align=bottom border=0 alt="modules"
 src="../icons/modules.gif"></a></td>
<td><A HREF="genindex.html"><img width=32 height=32 align=bottom border=0 alt="index"
 src="../icons/index.gif"></A></td>
</tr></table><b class=navlabel>Next:</b> <span class=sectref><A HREF="addresslist-objects.html">12.5.2 AddressList Objects</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="module-rfc822.html">12.5 rfc822  </A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="module-rfc822.html">12.5 rfc822  </A></span>
</div>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>Send comments on this document to <a href="mailto:python-docs@python.org">python-docs@python.org</a>.
</ADDRESS>
</BODY>
</HTML>
