<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <link rel="STYLESHEET" href="lib.css" type='text/css' /> <link rel="SHORTCUT ICON" href="../icons/pyfav.gif" /> <link rel='start' href='../index.html' title='Python Documentation Index' /> <link rel="first" href="lib.html" title='Python Library Reference' /> <link rel='contents' href='contents.html' title="Contents" /> <link rel='index' href='genindex.html' title='Index' /> <link rel='last' href='about.html' title='About this document...' /> <link rel='help' href='about.html' title='About this document...' /> <LINK rel="next" href="module-email.Iterators.html"> <LINK rel="prev" href="module-email.Errors.html"> <LINK rel="parent" href="module-email.html"> <LINK rel="next" href="module-email.Iterators.html"> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <meta name='aesop' content='information' /> <META name="description" content="Miscellaneous utilities"> <META name="keywords" content="lib"> <META name="resource-type" content="document"> <META name="distribution" content="global"> <title>12.2.9 Miscellaneous utilities</title> </head> <body> <DIV CLASS="navigation"> <div id='top-navigation-panel'> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td class='online-navigation'><a rel="prev" title="12.2.8 Exception classes" href="module-email.Errors.html"><img src='../icons/previous.png' border='0' height='32' alt='Previous Page' width='32' /></A></td> <td class='online-navigation'><a rel="parent" title="12.2 email " href="module-email.html"><img src='../icons/up.png' border='0' height='32' alt='Up One Level' width='32' /></A></td> <td class='online-navigation'><a rel="next" title="12.2.10 Iterators" href="module-email.Iterators.html"><img src='../icons/next.png' border='0' height='32' alt='Next Page' width='32' /></A></td> <td align="center" width="100%">Python Library Reference</td> <td class='online-navigation'><a rel="contents" title="Table of Contents" href="contents.html"><img src='../icons/contents.png' border='0' height='32' alt='Contents' width='32' /></A></td> <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' border='0' height='32' alt='Module Index' width='32' /></a></td> <td class='online-navigation'><a rel="index" title="Index" href="genindex.html"><img src='../icons/index.png' border='0' height='32' alt='Index' width='32' /></A></td> </tr></table> <div class='online-navigation'> <b class="navlabel">Previous:</b> <a class="sectref" rel="prev" href="module-email.Errors.html">12.2.8 Exception classes</A> <b class="navlabel">Up:</b> <a class="sectref" rel="parent" href="module-email.html">12.2 email </A> <b class="navlabel">Next:</b> <a class="sectref" rel="next" href="module-email.Iterators.html">12.2.10 Iterators</A> </div> <hr /></div> </DIV> <!--End of Navigation Panel--> <H2><A NAME="SECTION0014290000000000000000"> 12.2.9 Miscellaneous utilities</A> </H2> <A NAME="module-email.Utils"><!--z--></A> <P> There are several useful utilities provided with the <tt class="module">email</tt> package. <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3650' class="function">quote</tt></b>(</nobr></td> <td><var>str</var>)</td></tr></table></dt> <dd> Return a new string with backslashes in <var>str</var> replaced by two backslashes, and double quotes replaced by backslash-double quote. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3651' class="function">unquote</tt></b>(</nobr></td> <td><var>str</var>)</td></tr></table></dt> <dd> Return a new string which is an <i>unquoted</i> version of <var>str</var>. If <var>str</var> ends and begins with double quotes, they are stripped off. Likewise if <var>str</var> ends and begins with angle brackets, they are stripped off. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3652' class="function">parseaddr</tt></b>(</nobr></td> <td><var>address</var>)</td></tr></table></dt> <dd> Parse address - which should be the value of some address-containing field such as <span class="mailheader">To:</span> or <span class="mailheader">Cc:</span> - into its constituent <i>realname</i> and <i>email address</i> parts. Returns a tuple of that information, unless the parse fails, in which case a 2-tuple of <code>('', '')</code> is returned. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3653' class="function">formataddr</tt></b>(</nobr></td> <td><var>pair</var>)</td></tr></table></dt> <dd> The inverse of <tt class="method">parseaddr()</tt>, this takes a 2-tuple of the form <code>(realname, email_address)</code> and returns the string value suitable for a <span class="mailheader">To:</span> or <span class="mailheader">Cc:</span> header. If the first element of <var>pair</var> is false, then the second element is returned unmodified. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3654' class="function">getaddresses</tt></b>(</nobr></td> <td><var>fieldvalues</var>)</td></tr></table></dt> <dd> This method returns a list of 2-tuples of the form returned by <code>parseaddr()</code>. <var>fieldvalues</var> is a sequence of header field values as might be returned by <tt class="method">Message.get_all()</tt>. Here's a simple example that gets all the recipients of a message: <P> <div class="verbatim"><pre> from email.Utils import getaddresses tos = msg.get_all('to', []) ccs = msg.get_all('cc', []) resent_tos = msg.get_all('resent-to', []) resent_ccs = msg.get_all('resent-cc', []) all_recipients = getaddresses(tos + ccs + resent_tos + resent_ccs) </pre></div> </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3655' class="function">parsedate</tt></b>(</nobr></td> <td><var>date</var>)</td></tr></table></dt> <dd> Attempts to parse a date according to the rules in <a class="rfc" id='rfcref-81482' href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>. however, some mailers don't follow that format as specified, so <tt class="function">parsedate()</tt> tries to guess correctly in such cases. <var>date</var> is a string containing an <a class="rfc" id='rfcref-81484' href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a> date, such as <code>"Mon, 20 Nov 1995 19:12:08 -0500"</code>. If it succeeds in parsing the date, <tt class="function">parsedate()</tt> returns a 9-tuple that can be passed directly to <tt class="function">time.mktime()</tt>; otherwise <code>None</code> will be returned. Note that fields 6, 7, and 8 of the result tuple are not usable. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3656' class="function">parsedate_tz</tt></b>(</nobr></td> <td><var>date</var>)</td></tr></table></dt> <dd> Performs the same function as <tt class="function">parsedate()</tt>, but returns either <code>None</code> or a 10-tuple; the first 9 elements make up a tuple that can be passed directly to <tt class="function">time.mktime()</tt>, and the tenth is the offset of the date's timezone from UTC (which is the official term for Greenwich Mean Time)<A NAME="tex2html111" HREF="#foot50537"><SUP>12.2</SUP></A>. If the input string has no timezone, the last element of the tuple returned is <code>None</code>. Note that fields 6, 7, and 8 of the result tuple are not usable. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3657' class="function">mktime_tz</tt></b>(</nobr></td> <td><var>tuple</var>)</td></tr></table></dt> <dd> Turn a 10-tuple as returned by <tt class="function">parsedate_tz()</tt> into a UTC timestamp. It the timezone item in the tuple is <code>None</code>, assume local time. Minor deficiency: <tt class="function">mktime_tz()</tt> interprets the first 8 elements of <var>tuple</var> as a local time and then compensates for the timezone difference. This may yield a slight error around changes in daylight savings time, though not worth worrying about for common use. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3658' class="function">formatdate</tt></b>(</nobr></td> <td><big>[</big><var>timeval</var><big>[</big><var>, localtime</var><big>]</big><big>]</big>)</td></tr></table></dt> <dd> Returns a date string as per <a class="rfc" id='rfcref-81486' href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>, e.g.: <P> <div class="verbatim"><pre> Fri, 09 Nov 2001 01:08:47 -0000 </pre></div> <P> Optional <var>timeval</var> if given is a floating point time value as accepted by <tt class="function">time.gmtime()</tt> and <tt class="function">time.localtime()</tt>, otherwise the current time is used. <P> Optional <var>localtime</var> is a flag that when <code>True</code>, interprets <var>timeval</var>, and returns a date relative to the local timezone instead of UTC, properly taking daylight savings time into account. The default is <code>False</code> meaning UTC is used. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3659' class="function">make_msgid</tt></b>(</nobr></td> <td><big>[</big><var>idstring</var><big>]</big>)</td></tr></table></dt> <dd> Returns a string suitable for an <a class="rfc" id='rfcref-81488' href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>-compliant <span class="mailheader">Message-ID:</span> header. Optional <var>idstring</var> if given, is a string used to strengthen the uniqueness of the message id. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3660' class="function">decode_rfc2231</tt></b>(</nobr></td> <td><var>s</var>)</td></tr></table></dt> <dd> Decode the string <var>s</var> according to <a class="rfc" id='rfcref-81490' href="http://www.faqs.org/rfcs/rfc2231.html">RFC 2231</a>. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3661' class="function">encode_rfc2231</tt></b>(</nobr></td> <td><var>s</var><big>[</big><var>, charset</var><big>[</big><var>, language</var><big>]</big><big>]</big>)</td></tr></table></dt> <dd> Encode the string <var>s</var> according to <a class="rfc" id='rfcref-81492' href="http://www.faqs.org/rfcs/rfc2231.html">RFC 2231</a>. Optional <var>charset</var> and <var>language</var>, if given is the character set name and language name to use. If neither is given, <var>s</var> is returned as-is. If <var>charset</var> is given but <var>language</var> is not, the string is encoded using the empty string for <var>language</var>. </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3662' class="function">decode_params</tt></b>(</nobr></td> <td><var>params</var>)</td></tr></table></dt> <dd> Decode parameters list according to <a class="rfc" id='rfcref-81494' href="http://www.faqs.org/rfcs/rfc2231.html">RFC 2231</a>. <var>params</var> is a sequence of 2-tuples containing elements of the form <code>(content-type, string-value)</code>. </dl> <P> The following functions have been deprecated: <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3663' class="function">dump_address_pair</tt></b>(</nobr></td> <td><var>pair</var>)</td></tr></table></dt> <dd> <div class="versionnote"><b>Deprecated since release 2.2.2.</b> Use <tt class="function">formataddr()</tt> instead.</div> </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3664' class="function">decode</tt></b>(</nobr></td> <td><var>s</var>)</td></tr></table></dt> <dd> <div class="versionnote"><b>Deprecated since release 2.2.2.</b> Use <tt class="method">Header.decode_header()</tt> instead.</div> </dl> <P> <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> <td><nobr><b><tt id='l2h-3665' class="function">encode</tt></b>(</nobr></td> <td><var>s</var><big>[</big><var>, charset</var><big>[</big><var>, encoding</var><big>]</big><big>]</big>)</td></tr></table></dt> <dd> <div class="versionnote"><b>Deprecated since release 2.2.2.</b> Use <tt class="method">Header.encode()</tt> instead.</div> </dl> <P> <BR><HR><H4>Footnotes</H4> <DL> <DT><A NAME="foot50537">... Time)</A><A href="module-email.Utils.html#tex2html111"><SUP>12.2</SUP></A></DT> <DD>Note that the sign of the timezone offset is the opposite of the sign of the <code>time.timezone</code> variable for the same timezone; the latter variable follows the POSIX standard while this module follows <a class="rfc" id='rfcref-81471' href="http://www.faqs.org/rfcs/rfc2822.html">RFC 2822</a>. </DD> </DL> <DIV CLASS="navigation"> <div class='online-navigation'><hr /> <table align="center" width="100%" cellpadding="0" cellspacing="2"> <tr> <td class='online-navigation'><a rel="prev" title="12.2.8 Exception classes" rel="prev" title="12.2.8 Exception classes" href="module-email.Errors.html"><img src='../icons/previous.png' border='0' height='32' alt='Previous Page' width='32' /></A></td> <td class='online-navigation'><a rel="parent" title="12.2 email " rel="parent" title="12.2 email " href="module-email.html"><img src='../icons/up.png' border='0' height='32' alt='Up One Level' width='32' /></A></td> <td class='online-navigation'><a rel="next" title="12.2.10 Iterators" rel="next" title="12.2.10 Iterators" href="module-email.Iterators.html"><img src='../icons/next.png' border='0' height='32' alt='Next Page' width='32' /></A></td> <td align="center" width="100%">Python Library Reference</td> <td class='online-navigation'><a rel="contents" title="Table of Contents" rel="contents" title="Table of Contents" href="contents.html"><img src='../icons/contents.png' border='0' height='32' alt='Contents' width='32' /></A></td> <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' border='0' height='32' alt='Module Index' width='32' /></a></td> <td class='online-navigation'><a rel="index" title="Index" rel="index" title="Index" href="genindex.html"><img src='../icons/index.png' border='0' height='32' alt='Index' width='32' /></A></td> </tr></table> <div class='online-navigation'> <b class="navlabel">Previous:</b> <a class="sectref" rel="prev" href="module-email.Errors.html">12.2.8 Exception classes</A> <b class="navlabel">Up:</b> <a class="sectref" rel="parent" href="module-email.html">12.2 email </A> <b class="navlabel">Next:</b> <a class="sectref" rel="next" href="module-email.Iterators.html">12.2.10 Iterators</A> </div> </div> <hr /> <span class="release-info">Release 2.3.4, documentation updated on May 20, 2004.</span> </DIV> <!--End of Navigation Panel--> <ADDRESS> See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. </ADDRESS> </BODY> </HTML>