<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>21.16. smtpd — SMTP Server — Python v3.3.0 documentation</title> <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '3.3.0', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <script type="text/javascript" src="../_static/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python v3.3.0 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python v3.3.0 documentation" href="../index.html" /> <link rel="up" title="21. Internet Protocols and Support" href="internet.html" /> <link rel="next" title="21.17. telnetlib — Telnet client" href="telnetlib.html" /> <link rel="prev" title="21.15. smtplib — SMTP protocol client" href="smtplib.html" /> <link rel="shortcut icon" type="image/png" href="../_static/py.png" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" accesskey="I">index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="telnetlib.html" title="21.17. telnetlib — Telnet client" accesskey="N">next</a> |</li> <li class="right" > <a href="smtplib.html" title="21.15. smtplib — SMTP protocol client" accesskey="P">previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li><a href="../index.html">3.3.0 Documentation</a> »</li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="internet.html" accesskey="U">21. Internet Protocols and Support</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <div class="section" id="module-smtpd"> <span id="smtpd-smtp-server"></span><h1>21.16. <a class="reference internal" href="#module-smtpd" title="smtpd: A SMTP server implementation in Python."><tt class="xref py py-mod docutils literal"><span class="pre">smtpd</span></tt></a> — SMTP Server<a class="headerlink" href="#module-smtpd" title="Permalink to this headline">¶</a></h1> <p><strong>Source code:</strong> <a class="reference external" href="http://hg.python.org/cpython/file/3.3/Lib/smtpd.py">Lib/smtpd.py</a></p> <hr class="docutils" /> <p>This module offers several classes to implement SMTP (email) servers.</p> <p>Several server implementations are present; one is a generic do-nothing implementation, which can be overridden, while the other two offer specific mail-sending strategies.</p> <p>Additionally the SMTPChannel may be extended to implement very specific interaction behaviour with SMTP clients.</p> <p>The code supports <span class="target" id="index-0"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc5321.html"><strong>RFC 5321</strong></a>, plus the <span class="target" id="index-1"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc1870.html"><strong>RFC 1870</strong></a> SIZE extension.</p> <div class="section" id="smtpserver-objects"> <h2>21.16.1. SMTPServer Objects<a class="headerlink" href="#smtpserver-objects" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="smtpd.SMTPServer"> <em class="property">class </em><tt class="descclassname">smtpd.</tt><tt class="descname">SMTPServer</tt><big>(</big><em>localaddr</em>, <em>remoteaddr</em>, <em>data_size_limit=33554432</em><big>)</big><a class="headerlink" href="#smtpd.SMTPServer" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new <a class="reference internal" href="#smtpd.SMTPServer" title="smtpd.SMTPServer"><tt class="xref py py-class docutils literal"><span class="pre">SMTPServer</span></tt></a> object, which binds to local address <em>localaddr</em>. It will treat <em>remoteaddr</em> as an upstream SMTP relayer. It inherits from <a class="reference internal" href="asyncore.html#asyncore.dispatcher" title="asyncore.dispatcher"><tt class="xref py py-class docutils literal"><span class="pre">asyncore.dispatcher</span></tt></a>, and so will insert itself into <a class="reference internal" href="asyncore.html#module-asyncore" title="asyncore: A base class for developing asynchronous socket handling services."><tt class="xref py py-mod docutils literal"><span class="pre">asyncore</span></tt></a>‘s event loop on instantiation.</p> <p><em>data_size_limit</em> specifies the maximum number of bytes that will be accepted in a <tt class="docutils literal"><span class="pre">DATA</span></tt> command. A value of <tt class="xref docutils literal"><span class="pre">None</span></tt> or <tt class="docutils literal"><span class="pre">0</span></tt> means no limit.</p> <dl class="method"> <dt id="smtpd.SMTPServer.process_message"> <tt class="descname">process_message</tt><big>(</big><em>peer</em>, <em>mailfrom</em>, <em>rcpttos</em>, <em>data</em><big>)</big><a class="headerlink" href="#smtpd.SMTPServer.process_message" title="Permalink to this definition">¶</a></dt> <dd><p>Raise <a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><tt class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></tt></a> exception. Override this in subclasses to do something useful with this message. Whatever was passed in the constructor as <em>remoteaddr</em> will be available as the <tt class="xref py py-attr docutils literal"><span class="pre">_remoteaddr</span></tt> attribute. <em>peer</em> is the remote host’s address, <em>mailfrom</em> is the envelope originator, <em>rcpttos</em> are the envelope recipients and <em>data</em> is a string containing the contents of the e-mail (which should be in <span class="target" id="index-2"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc2822.html"><strong>RFC 2822</strong></a> format).</p> </dd></dl> <dl class="attribute"> <dt id="smtpd.SMTPServer.channel_class"> <tt class="descname">channel_class</tt><a class="headerlink" href="#smtpd.SMTPServer.channel_class" title="Permalink to this definition">¶</a></dt> <dd><p>Override this in subclasses to use a custom <a class="reference internal" href="#smtpd.SMTPChannel" title="smtpd.SMTPChannel"><tt class="xref py py-class docutils literal"><span class="pre">SMTPChannel</span></tt></a> for managing SMTP clients.</p> </dd></dl> </dd></dl> </div> <div class="section" id="debuggingserver-objects"> <h2>21.16.2. DebuggingServer Objects<a class="headerlink" href="#debuggingserver-objects" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="smtpd.DebuggingServer"> <em class="property">class </em><tt class="descclassname">smtpd.</tt><tt class="descname">DebuggingServer</tt><big>(</big><em>localaddr</em>, <em>remoteaddr</em><big>)</big><a class="headerlink" href="#smtpd.DebuggingServer" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new debugging server. Arguments are as per <a class="reference internal" href="#smtpd.SMTPServer" title="smtpd.SMTPServer"><tt class="xref py py-class docutils literal"><span class="pre">SMTPServer</span></tt></a>. Messages will be discarded, and printed on stdout.</p> </dd></dl> </div> <div class="section" id="pureproxy-objects"> <h2>21.16.3. PureProxy Objects<a class="headerlink" href="#pureproxy-objects" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="smtpd.PureProxy"> <em class="property">class </em><tt class="descclassname">smtpd.</tt><tt class="descname">PureProxy</tt><big>(</big><em>localaddr</em>, <em>remoteaddr</em><big>)</big><a class="headerlink" href="#smtpd.PureProxy" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new pure proxy server. Arguments are as per <a class="reference internal" href="#smtpd.SMTPServer" title="smtpd.SMTPServer"><tt class="xref py py-class docutils literal"><span class="pre">SMTPServer</span></tt></a>. Everything will be relayed to <em>remoteaddr</em>. Note that running this has a good chance to make you into an open relay, so please be careful.</p> </dd></dl> </div> <div class="section" id="mailmanproxy-objects"> <h2>21.16.4. MailmanProxy Objects<a class="headerlink" href="#mailmanproxy-objects" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="smtpd.MailmanProxy"> <em class="property">class </em><tt class="descclassname">smtpd.</tt><tt class="descname">MailmanProxy</tt><big>(</big><em>localaddr</em>, <em>remoteaddr</em><big>)</big><a class="headerlink" href="#smtpd.MailmanProxy" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new pure proxy server. Arguments are as per <a class="reference internal" href="#smtpd.SMTPServer" title="smtpd.SMTPServer"><tt class="xref py py-class docutils literal"><span class="pre">SMTPServer</span></tt></a>. Everything will be relayed to <em>remoteaddr</em>, unless local mailman configurations knows about an address, in which case it will be handled via mailman. Note that running this has a good chance to make you into an open relay, so please be careful.</p> </dd></dl> </div> <div class="section" id="smtpchannel-objects"> <h2>21.16.5. SMTPChannel Objects<a class="headerlink" href="#smtpchannel-objects" title="Permalink to this headline">¶</a></h2> <dl class="class"> <dt id="smtpd.SMTPChannel"> <em class="property">class </em><tt class="descclassname">smtpd.</tt><tt class="descname">SMTPChannel</tt><big>(</big><em>server</em>, <em>conn</em>, <em>addr</em><big>)</big><a class="headerlink" href="#smtpd.SMTPChannel" title="Permalink to this definition">¶</a></dt> <dd><p>Create a new <a class="reference internal" href="#smtpd.SMTPChannel" title="smtpd.SMTPChannel"><tt class="xref py py-class docutils literal"><span class="pre">SMTPChannel</span></tt></a> object which manages the communication between the server and a single SMTP client.</p> <p>To use a custom SMTPChannel implementation you need to override the <a class="reference internal" href="#smtpd.SMTPServer.channel_class" title="smtpd.SMTPServer.channel_class"><tt class="xref py py-attr docutils literal"><span class="pre">SMTPServer.channel_class</span></tt></a> of your <a class="reference internal" href="#smtpd.SMTPServer" title="smtpd.SMTPServer"><tt class="xref py py-class docutils literal"><span class="pre">SMTPServer</span></tt></a>.</p> <p>The <a class="reference internal" href="#smtpd.SMTPChannel" title="smtpd.SMTPChannel"><tt class="xref py py-class docutils literal"><span class="pre">SMTPChannel</span></tt></a> has the following instance variables:</p> <dl class="attribute"> <dt id="smtpd.SMTPChannel.smtp_server"> <tt class="descname">smtp_server</tt><a class="headerlink" href="#smtpd.SMTPChannel.smtp_server" title="Permalink to this definition">¶</a></dt> <dd><p>Holds the <a class="reference internal" href="#smtpd.SMTPServer" title="smtpd.SMTPServer"><tt class="xref py py-class docutils literal"><span class="pre">SMTPServer</span></tt></a> that spawned this channel.</p> </dd></dl> <dl class="attribute"> <dt id="smtpd.SMTPChannel.conn"> <tt class="descname">conn</tt><a class="headerlink" href="#smtpd.SMTPChannel.conn" title="Permalink to this definition">¶</a></dt> <dd><p>Holds the socket object connecting to the client.</p> </dd></dl> <dl class="attribute"> <dt id="smtpd.SMTPChannel.addr"> <tt class="descname">addr</tt><a class="headerlink" href="#smtpd.SMTPChannel.addr" title="Permalink to this definition">¶</a></dt> <dd><p>Holds the address of the client, the second value returned by socket.accept()</p> </dd></dl> <dl class="attribute"> <dt id="smtpd.SMTPChannel.received_lines"> <tt class="descname">received_lines</tt><a class="headerlink" href="#smtpd.SMTPChannel.received_lines" title="Permalink to this definition">¶</a></dt> <dd><p>Holds a list of the line strings (decoded using UTF-8) received from the client. The lines have their “\r\n” line ending translated to “\n”.</p> </dd></dl> <dl class="attribute"> <dt id="smtpd.SMTPChannel.smtp_state"> <tt class="descname">smtp_state</tt><a class="headerlink" href="#smtpd.SMTPChannel.smtp_state" title="Permalink to this definition">¶</a></dt> <dd><p>Holds the current state of the channel. This will be either <tt class="xref py py-attr docutils literal"><span class="pre">COMMAND</span></tt> initially and then <tt class="xref py py-attr docutils literal"><span class="pre">DATA</span></tt> after the client sends a “DATA” line.</p> </dd></dl> <dl class="attribute"> <dt id="smtpd.SMTPChannel.seen_greeting"> <tt class="descname">seen_greeting</tt><a class="headerlink" href="#smtpd.SMTPChannel.seen_greeting" title="Permalink to this definition">¶</a></dt> <dd><p>Holds a string containing the greeting sent by the client in its “HELO”.</p> </dd></dl> <dl class="attribute"> <dt id="smtpd.SMTPChannel.mailfrom"> <tt class="descname">mailfrom</tt><a class="headerlink" href="#smtpd.SMTPChannel.mailfrom" title="Permalink to this definition">¶</a></dt> <dd><p>Holds a string containing the address identified in the “MAIL FROM:” line from the client.</p> </dd></dl> <dl class="attribute"> <dt id="smtpd.SMTPChannel.rcpttos"> <tt class="descname">rcpttos</tt><a class="headerlink" href="#smtpd.SMTPChannel.rcpttos" title="Permalink to this definition">¶</a></dt> <dd><p>Holds a list of strings containing the addresses identified in the “RCPT TO:” lines from the client.</p> </dd></dl> <dl class="attribute"> <dt id="smtpd.SMTPChannel.received_data"> <tt class="descname">received_data</tt><a class="headerlink" href="#smtpd.SMTPChannel.received_data" title="Permalink to this definition">¶</a></dt> <dd><p>Holds a string containing all of the data sent by the client during the DATA state, up to but not including the terminating “rn.rn”.</p> </dd></dl> <dl class="attribute"> <dt id="smtpd.SMTPChannel.fqdn"> <tt class="descname">fqdn</tt><a class="headerlink" href="#smtpd.SMTPChannel.fqdn" title="Permalink to this definition">¶</a></dt> <dd><p>Holds the fully-qualified domain name of the server as returned by <tt class="docutils literal"><span class="pre">socket.getfqdn()</span></tt>.</p> </dd></dl> <dl class="attribute"> <dt id="smtpd.SMTPChannel.peer"> <tt class="descname">peer</tt><a class="headerlink" href="#smtpd.SMTPChannel.peer" title="Permalink to this definition">¶</a></dt> <dd><p>Holds the name of the client peer as returned by <tt class="docutils literal"><span class="pre">conn.getpeername()</span></tt> where <tt class="docutils literal"><span class="pre">conn</span></tt> is <a class="reference internal" href="#smtpd.SMTPChannel.conn" title="smtpd.SMTPChannel.conn"><tt class="xref py py-attr docutils literal"><span class="pre">conn</span></tt></a>.</p> </dd></dl> <p>The <a class="reference internal" href="#smtpd.SMTPChannel" title="smtpd.SMTPChannel"><tt class="xref py py-class docutils literal"><span class="pre">SMTPChannel</span></tt></a> operates by invoking methods named <tt class="docutils literal"><span class="pre">smtp_<command></span></tt> upon reception of a command line from the client. Built into the base <a class="reference internal" href="#smtpd.SMTPChannel" title="smtpd.SMTPChannel"><tt class="xref py py-class docutils literal"><span class="pre">SMTPChannel</span></tt></a> class are methods for handling the following commands (and responding to them appropriately):</p> <table border="1" class="docutils"> <colgroup> <col width="11%" /> <col width="89%" /> </colgroup> <thead valign="bottom"> <tr><th class="head">Command</th> <th class="head">Action taken</th> </tr> </thead> <tbody valign="top"> <tr><td>HELO</td> <td>Accepts the greeting from the client and stores it in <a class="reference internal" href="#smtpd.SMTPChannel.seen_greeting" title="smtpd.SMTPChannel.seen_greeting"><tt class="xref py py-attr docutils literal"><span class="pre">seen_greeting</span></tt></a>. Sets server to base command mode.</td> </tr> <tr><td>EHLO</td> <td>Accepts the greeting from the client and stores it in <a class="reference internal" href="#smtpd.SMTPChannel.seen_greeting" title="smtpd.SMTPChannel.seen_greeting"><tt class="xref py py-attr docutils literal"><span class="pre">seen_greeting</span></tt></a>. Sets server to extended command mode.</td> </tr> <tr><td>NOOP</td> <td>Takes no action.</td> </tr> <tr><td>QUIT</td> <td>Closes the connection cleanly.</td> </tr> <tr><td>MAIL</td> <td>Accepts the “MAIL FROM:” syntax and stores the supplied address as <a class="reference internal" href="#smtpd.SMTPChannel.mailfrom" title="smtpd.SMTPChannel.mailfrom"><tt class="xref py py-attr docutils literal"><span class="pre">mailfrom</span></tt></a>. In extended command mode, accepts the <span class="target" id="index-3"></span><a class="rfc reference external" href="http://tools.ietf.org/html/rfc1870.html"><strong>RFC 1870</strong></a> SIZE attribute and responds appropriately based on the value of <tt class="docutils literal"><span class="pre">data_size_limit</span></tt>.</td> </tr> <tr><td>RCPT</td> <td>Accepts the “RCPT TO:” syntax and stores the supplied addresses in the <a class="reference internal" href="#smtpd.SMTPChannel.rcpttos" title="smtpd.SMTPChannel.rcpttos"><tt class="xref py py-attr docutils literal"><span class="pre">rcpttos</span></tt></a> list.</td> </tr> <tr><td>RSET</td> <td>Resets the <a class="reference internal" href="#smtpd.SMTPChannel.mailfrom" title="smtpd.SMTPChannel.mailfrom"><tt class="xref py py-attr docutils literal"><span class="pre">mailfrom</span></tt></a>, <a class="reference internal" href="#smtpd.SMTPChannel.rcpttos" title="smtpd.SMTPChannel.rcpttos"><tt class="xref py py-attr docutils literal"><span class="pre">rcpttos</span></tt></a>, and <a class="reference internal" href="#smtpd.SMTPChannel.received_data" title="smtpd.SMTPChannel.received_data"><tt class="xref py py-attr docutils literal"><span class="pre">received_data</span></tt></a>, but not the greeting.</td> </tr> <tr><td>DATA</td> <td>Sets the internal state to <tt class="xref py py-attr docutils literal"><span class="pre">DATA</span></tt> and stores remaining lines from the client in <a class="reference internal" href="#smtpd.SMTPChannel.received_data" title="smtpd.SMTPChannel.received_data"><tt class="xref py py-attr docutils literal"><span class="pre">received_data</span></tt></a> until the terminator “rn.rn” is received.</td> </tr> <tr><td>HELP</td> <td>Returns minimal information on command syntax</td> </tr> <tr><td>VRFY</td> <td>Returns code 252 (the server doesn’t know if the address is valid)</td> </tr> <tr><td>EXPN</td> <td>Reports that the command is not implemented.</td> </tr> </tbody> </table> </dd></dl> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">21.16. <tt class="docutils literal"><span class="pre">smtpd</span></tt> — SMTP Server</a><ul> <li><a class="reference internal" href="#smtpserver-objects">21.16.1. SMTPServer Objects</a></li> <li><a class="reference internal" href="#debuggingserver-objects">21.16.2. DebuggingServer Objects</a></li> <li><a class="reference internal" href="#pureproxy-objects">21.16.3. PureProxy Objects</a></li> <li><a class="reference internal" href="#mailmanproxy-objects">21.16.4. MailmanProxy Objects</a></li> <li><a class="reference internal" href="#smtpchannel-objects">21.16.5. SMTPChannel Objects</a></li> </ul> </li> </ul> <h4>Previous topic</h4> <p class="topless"><a href="smtplib.html" title="previous chapter">21.15. <tt class="docutils literal docutils literal"><span class="pre">smtplib</span></tt> — SMTP protocol client</a></p> <h4>Next topic</h4> <p class="topless"><a href="telnetlib.html" title="next chapter">21.17. <tt class="docutils literal"><span class="pre">telnetlib</span></tt> — Telnet client</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/smtpd.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" size="18" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <h3>Navigation</h3> <ul> <li class="right" style="margin-right: 10px"> <a href="../genindex.html" title="General Index" >index</a></li> <li class="right" > <a href="../py-modindex.html" title="Python Module Index" >modules</a> |</li> <li class="right" > <a href="telnetlib.html" title="21.17. telnetlib — Telnet client" >next</a> |</li> <li class="right" > <a href="smtplib.html" title="21.15. smtplib — SMTP protocol client" >previous</a> |</li> <li><img src="../_static/py.png" alt="" style="vertical-align: middle; margin-top: -1px"/></li> <li><a href="http://www.python.org/">Python</a> »</li> <li><a href="../index.html">3.3.0 Documentation</a> »</li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="internet.html" >21. Internet Protocols and Support</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2012, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Sep 29, 2012. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.0.7. </div> </body> </html>