<!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>11.9 smtplib --
SMTP protocol client</TITLE>
<META NAME="description" CONTENT="11.9 smtplib --
SMTP protocol client">
<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="module-telnetlib.html">
<LINK REL="previous" HREF="module-nntplib.html">
<LINK REL="up" HREF="internet.html">
<LINK REL="next" HREF="SMTP-objects.html">
</HEAD>
<BODY bgcolor="#ffffff">
<div class=navigation>
<table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="SMTP-objects.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="internet.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="nntp-objects.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="SMTP-objects.html">11.9.1 SMTP Objects</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="internet.html">11. Internet Protocols and</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="nntp-objects.html">11.8.1 NNTP Objects</A></span>
<br><hr></div>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION0013900000000000000000">
11.9 <tt class=module>smtplib</tt> --
         SMTP protocol client</A>
</H1>

<P>


<P>
<a name="l2h-1770"></a>
<a name="l2h-1771"></a>

<P>
The <tt class=module>smtplib</tt> module defines an SMTP client session object that
can be used to send mail to any Internet machine with an SMTP or ESMTP
listener daemon.  For details of SMTP and ESMTP operation, consult
<span class=rfc><a name="rfcref-42468"
href="http://info.internet.isi.edu/in-notes/rfc/files/rfc821.txt">RFC 821</a></span> (<i>Simple Mail Transfer Protocol</i>) and <span class=rfc><a name="rfcref-42470"
href="http://info.internet.isi.edu/in-notes/rfc/files/rfc1869.txt">RFC 1869</a></span>
(<i>SMTP Service Extensions</i>).

<P>
<dl><dt><b><a name="l2h-1761"><tt class=class>SMTP</tt></a></b> (<big>[</big><var>host</var><big>[</big><var>, port</var><big>]</big><big>]</big>)
<dd>
A <tt class=class>SMTP</tt> instance encapsulates an SMTP connection.  It has
methods that support a full repertoire of SMTP and ESMTP
operations. If the optional host and port parameters are given, the
SMTP <tt class=method>connect()</tt> method is called with those parameters during
initialization.  An <tt class=exception>SMTPConnectError</tt> is raised if the
specified host doesn't respond correctly.

<P>
For normal use, you should only require the initialization/connect,
<tt class=method>sendmail()</tt>, and <tt class=method>quit()</tt> methods.  An example is
included below.
</dl>

<P>
A nice selection of exceptions is defined as well:

<P>
<dl><dt><b><a name="l2h-1762"><tt class=exception>SMTPException</tt></a></b>
<dd>
  Base exception class for all exceptions raised by this module.
</dl>

<P>
<dl><dt><b><a name="l2h-1763"><tt class=exception>SMTPServerDisconnected</tt></a></b>
<dd>
  This exception is raised when the server unexpectedly disconnects,
  or when an attempt is made to use the <tt class=class>SMTP</tt> instance before
  connecting it to a server.
</dl>

<P>
<dl><dt><b><a name="l2h-1764"><tt class=exception>SMTPResponseException</tt></a></b>
<dd>
  Base class for all exceptions that include an SMTP error code.
  These exceptions are generated in some instances when the SMTP
  server returns an error code.  The error code is stored in the
  <tt class=member>smtp_code</tt> attribute of the error, and the
  <tt class=member>smtp_error</tt> attribute is set to the error message.
</dl>

<P>
<dl><dt><b><a name="l2h-1765"><tt class=exception>SMTPSenderRefused</tt></a></b>
<dd>
  Sender address refused.  In addition to the attributes set by on all 
  <tt class=exception>SMTPResponseException</tt> exceptions, this sets `sender' to
  the string that the SMTP server refused.
</dl>

<P>
<dl><dt><b><a name="l2h-1766"><tt class=exception>SMTPRecipientsRefused</tt></a></b>
<dd>
  All recipient addresses refused.  The errors for each recipient are
  accessable through the attribute <tt class=member>recipients</tt>, which is a
  dictionary of exactly the same sort as <tt class=method>SMTP.sendmail()</tt>
  returns.
</dl>

<P>
<dl><dt><b><a name="l2h-1767"><tt class=exception>SMTPDataError</tt></a></b>
<dd>
  The SMTP server refused to accept the message data.
</dl>

<P>
<dl><dt><b><a name="l2h-1768"><tt class=exception>SMTPConnectError</tt></a></b>
<dd>
  Error occurred during establishment of a connection  with the server.
</dl>

<P>
<dl><dt><b><a name="l2h-1769"><tt class=exception>SMTPHeloError</tt></a></b>
<dd>
  The server refused our "<tt class=samp>HELO</tt>" message.
</dl>

<P>

<p><hr>
<!--Table of Child-Links-->


<UL>
<LI><A NAME="tex2html3154"
 HREF="SMTP-objects.html">11.9.1 SMTP Objects </A>
<LI><A NAME="tex2html3155"
 HREF="SMTP-example.html">11.9.2 SMTP Example </A>
</UL>
<!--End of Table of Child-Links-->
<p>
<div class=navigation><hr><table align=center width="100%" cellpadding=0 cellspacing=2>
<tr>
<td><A HREF="SMTP-objects.html"><img width=32 height=32 align=bottom border=0 alt="next"
 src="../icons/next.gif"></A></td>
<td><A HREF="internet.html"><img width=32 height=32 align=bottom border=0 alt="up"
 src="../icons/up.gif"></A></td>
<td><A HREF="nntp-objects.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="SMTP-objects.html">11.9.1 SMTP Objects</A></span>
<b class=navlabel>Up:</b> <span class=sectref><A HREF="internet.html">11. Internet Protocols and</A></span>
<b class=navlabel>Previous:</b> <span class=sectref><A HREF="nntp-objects.html">11.8.1 NNTP Objects</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>
