Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :mod:`smtpd` --- SMTP Server |
| 2 | ============================ |
| 3 | |
| 4 | .. module:: smtpd |
| 5 | :synopsis: A SMTP server implementation in Python. |
| 6 | |
Andrew Kuchling | 587e970 | 2013-11-12 10:02:35 -0500 | [diff] [blame] | 7 | .. moduleauthor:: Barry Warsaw <barry@python.org> |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | .. sectionauthor:: Moshe Zadka <moshez@moshez.org> |
| 9 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 10 | **Source code:** :source:`Lib/smtpd.py` |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 12 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 13 | |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 14 | This module offers several classes to implement SMTP (email) servers. |
| 15 | |
| 16 | Several server implementations are present; one is a generic |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | do-nothing implementation, which can be overridden, while the other two offer |
| 18 | specific mail-sending strategies. |
| 19 | |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 20 | Additionally the SMTPChannel may be extended to implement very specific |
| 21 | interaction behaviour with SMTP clients. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 22 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 23 | The code supports :RFC:`5321`, plus the :rfc:`1870` SIZE extension. |
| 24 | |
| 25 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 26 | SMTPServer Objects |
| 27 | ------------------ |
| 28 | |
| 29 | |
Serhiy Storchaka | 98b28fd | 2013-10-13 23:12:09 +0300 | [diff] [blame] | 30 | .. class:: SMTPServer(localaddr, remoteaddr, data_size_limit=33554432,\ |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame^] | 31 | map=None, decode_data=True) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
| 33 | Create a new :class:`SMTPServer` object, which binds to local address |
| 34 | *localaddr*. It will treat *remoteaddr* as an upstream SMTP relayer. It |
| 35 | inherits from :class:`asyncore.dispatcher`, and so will insert itself into |
| 36 | :mod:`asyncore`'s event loop on instantiation. |
| 37 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 38 | *data_size_limit* specifies the maximum number of bytes that will be |
| 39 | accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no |
| 40 | limit. |
| 41 | |
Vinay Sajip | 30298b4 | 2013-06-07 15:21:41 +0100 | [diff] [blame] | 42 | A dictionary can be specified in *map* to avoid using a global socket map. |
| 43 | |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame^] | 44 | *decode_data* specifies whether the data portion of the SMTP transaction |
| 45 | should be decoded using UTF-8. The default is ``True`` for backward |
| 46 | compatibility reasons, but will change to ``False`` in Python 3.6. Specify |
| 47 | the keyword value explicitly to avoid the :exc:`DeprecationWarning`. |
| 48 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 49 | .. method:: process_message(peer, mailfrom, rcpttos, data) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 51 | Raise :exc:`NotImplementedError` exception. Override this in subclasses to |
| 52 | do something useful with this message. Whatever was passed in the |
| 53 | constructor as *remoteaddr* will be available as the :attr:`_remoteaddr` |
| 54 | attribute. *peer* is the remote host's address, *mailfrom* is the envelope |
| 55 | originator, *rcpttos* are the envelope recipients and *data* is a string |
| 56 | containing the contents of the e-mail (which should be in :rfc:`2822` |
| 57 | format). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame^] | 59 | If the *decode_data* constructor keyword is set to ``True``, the *data* |
| 60 | argument will be a unicode string. If it is set to ``False``, it |
| 61 | will be a bytes object. |
| 62 | |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 63 | .. attribute:: channel_class |
| 64 | |
| 65 | Override this in subclasses to use a custom :class:`SMTPChannel` for |
| 66 | managing SMTP clients. |
| 67 | |
Vinay Sajip | 30298b4 | 2013-06-07 15:21:41 +0100 | [diff] [blame] | 68 | .. versionchanged:: 3.4 |
| 69 | The *map* argument was added. |
| 70 | |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame^] | 71 | .. versionchanged:: 3.5 |
| 72 | the *decode_data* argument was added. |
| 73 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 74 | |
| 75 | DebuggingServer Objects |
| 76 | ----------------------- |
| 77 | |
| 78 | |
| 79 | .. class:: DebuggingServer(localaddr, remoteaddr) |
| 80 | |
| 81 | Create a new debugging server. Arguments are as per :class:`SMTPServer`. |
| 82 | Messages will be discarded, and printed on stdout. |
| 83 | |
| 84 | |
| 85 | PureProxy Objects |
| 86 | ----------------- |
| 87 | |
| 88 | |
| 89 | .. class:: PureProxy(localaddr, remoteaddr) |
| 90 | |
| 91 | Create a new pure proxy server. Arguments are as per :class:`SMTPServer`. |
| 92 | Everything will be relayed to *remoteaddr*. Note that running this has a good |
| 93 | chance to make you into an open relay, so please be careful. |
| 94 | |
| 95 | |
| 96 | MailmanProxy Objects |
| 97 | -------------------- |
| 98 | |
| 99 | |
| 100 | .. class:: MailmanProxy(localaddr, remoteaddr) |
| 101 | |
| 102 | Create a new pure proxy server. Arguments are as per :class:`SMTPServer`. |
| 103 | Everything will be relayed to *remoteaddr*, unless local mailman configurations |
| 104 | knows about an address, in which case it will be handled via mailman. Note that |
| 105 | running this has a good chance to make you into an open relay, so please be |
| 106 | careful. |
| 107 | |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 108 | SMTPChannel Objects |
| 109 | ------------------- |
| 110 | |
Serhiy Storchaka | 98b28fd | 2013-10-13 23:12:09 +0300 | [diff] [blame] | 111 | .. class:: SMTPChannel(server, conn, addr, data_size_limit=33554432,\ |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame^] | 112 | map=None, decode_data=True) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 113 | |
| 114 | Create a new :class:`SMTPChannel` object which manages the communication |
| 115 | between the server and a single SMTP client. |
| 116 | |
Vinay Sajip | 30298b4 | 2013-06-07 15:21:41 +0100 | [diff] [blame] | 117 | *conn* and *addr* are as per the instance variables described below. |
| 118 | |
| 119 | *data_size_limit* specifies the maximum number of bytes that will be |
| 120 | accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no |
| 121 | limit. |
| 122 | |
| 123 | A dictionary can be specified in *map* to avoid using a global socket map. |
| 124 | |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame^] | 125 | *decode_data* specifies whether the data portion of the SMTP transaction |
| 126 | should be decoded using UTF-8. The default is ``True`` for backward |
| 127 | compatibility reasons, but will change to ``False`` in Python 3.6. Specify |
| 128 | the keyword value explicitly to avoid the :exc:`DeprecationWarning`. |
| 129 | |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 130 | To use a custom SMTPChannel implementation you need to override the |
| 131 | :attr:`SMTPServer.channel_class` of your :class:`SMTPServer`. |
| 132 | |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame^] | 133 | .. versionchanged:: 3.5 |
| 134 | the *decode_data* argument was added. |
| 135 | |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 136 | The :class:`SMTPChannel` has the following instance variables: |
| 137 | |
| 138 | .. attribute:: smtp_server |
| 139 | |
| 140 | Holds the :class:`SMTPServer` that spawned this channel. |
| 141 | |
| 142 | .. attribute:: conn |
| 143 | |
| 144 | Holds the socket object connecting to the client. |
| 145 | |
| 146 | .. attribute:: addr |
| 147 | |
| 148 | Holds the address of the client, the second value returned by |
Ezio Melotti | 8bbcb58 | 2012-09-20 09:06:51 +0300 | [diff] [blame] | 149 | :func:`socket.accept <socket.socket.accept>` |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 150 | |
| 151 | .. attribute:: received_lines |
| 152 | |
| 153 | Holds a list of the line strings (decoded using UTF-8) received from |
Ezio Melotti | 8bbcb58 | 2012-09-20 09:06:51 +0300 | [diff] [blame] | 154 | the client. The lines have their ``"\r\n"`` line ending translated to |
| 155 | ``"\n"``. |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 156 | |
| 157 | .. attribute:: smtp_state |
| 158 | |
| 159 | Holds the current state of the channel. This will be either |
| 160 | :attr:`COMMAND` initially and then :attr:`DATA` after the client sends |
| 161 | a "DATA" line. |
| 162 | |
| 163 | .. attribute:: seen_greeting |
| 164 | |
| 165 | Holds a string containing the greeting sent by the client in its "HELO". |
| 166 | |
| 167 | .. attribute:: mailfrom |
| 168 | |
| 169 | Holds a string containing the address identified in the "MAIL FROM:" line |
| 170 | from the client. |
| 171 | |
| 172 | .. attribute:: rcpttos |
| 173 | |
| 174 | Holds a list of strings containing the addresses identified in the |
| 175 | "RCPT TO:" lines from the client. |
| 176 | |
| 177 | .. attribute:: received_data |
| 178 | |
| 179 | Holds a string containing all of the data sent by the client during the |
Ezio Melotti | 8bbcb58 | 2012-09-20 09:06:51 +0300 | [diff] [blame] | 180 | DATA state, up to but not including the terminating ``"\r\n.\r\n"``. |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 181 | |
| 182 | .. attribute:: fqdn |
| 183 | |
| 184 | Holds the fully-qualified domain name of the server as returned by |
Ezio Melotti | 8bbcb58 | 2012-09-20 09:06:51 +0300 | [diff] [blame] | 185 | :func:`socket.getfqdn`. |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 186 | |
| 187 | .. attribute:: peer |
| 188 | |
| 189 | Holds the name of the client peer as returned by ``conn.getpeername()`` |
| 190 | where ``conn`` is :attr:`conn`. |
| 191 | |
| 192 | The :class:`SMTPChannel` operates by invoking methods named ``smtp_<command>`` |
| 193 | upon reception of a command line from the client. Built into the base |
| 194 | :class:`SMTPChannel` class are methods for handling the following commands |
| 195 | (and responding to them appropriately): |
| 196 | |
| 197 | ======== =================================================================== |
| 198 | Command Action taken |
| 199 | ======== =================================================================== |
| 200 | HELO Accepts the greeting from the client and stores it in |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 201 | :attr:`seen_greeting`. Sets server to base command mode. |
| 202 | EHLO Accepts the greeting from the client and stores it in |
| 203 | :attr:`seen_greeting`. Sets server to extended command mode. |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 204 | NOOP Takes no action. |
| 205 | QUIT Closes the connection cleanly. |
| 206 | MAIL Accepts the "MAIL FROM:" syntax and stores the supplied address as |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 207 | :attr:`mailfrom`. In extended command mode, accepts the |
| 208 | :rfc:`1870` SIZE attribute and responds appropriately based on the |
Ezio Melotti | a58d8b0 | 2012-09-20 09:09:24 +0300 | [diff] [blame] | 209 | value of *data_size_limit*. |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 210 | RCPT Accepts the "RCPT TO:" syntax and stores the supplied addresses in |
| 211 | the :attr:`rcpttos` list. |
| 212 | RSET Resets the :attr:`mailfrom`, :attr:`rcpttos`, and |
| 213 | :attr:`received_data`, but not the greeting. |
| 214 | DATA Sets the internal state to :attr:`DATA` and stores remaining lines |
| 215 | from the client in :attr:`received_data` until the terminator |
Ezio Melotti | 8bbcb58 | 2012-09-20 09:06:51 +0300 | [diff] [blame] | 216 | ``"\r\n.\r\n"`` is received. |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 217 | HELP Returns minimal information on command syntax |
| 218 | VRFY Returns code 252 (the server doesn't know if the address is valid) |
| 219 | EXPN Reports that the command is not implemented. |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 220 | ======== =================================================================== |