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 | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 23 | The code supports :RFC:`5321`, plus the :rfc:`1870` SIZE and :rfc:`6531` |
| 24 | SMTPUTF8 extensions. |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 25 | |
| 26 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 27 | SMTPServer Objects |
| 28 | ------------------ |
| 29 | |
| 30 | |
Serhiy Storchaka | 98b28fd | 2013-10-13 23:12:09 +0300 | [diff] [blame] | 31 | .. class:: SMTPServer(localaddr, remoteaddr, data_size_limit=33554432,\ |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame^] | 32 | map=None, enable_SMTPUTF8=False, decode_data=False) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 33 | |
| 34 | Create a new :class:`SMTPServer` object, which binds to local address |
| 35 | *localaddr*. It will treat *remoteaddr* as an upstream SMTP relayer. It |
| 36 | inherits from :class:`asyncore.dispatcher`, and so will insert itself into |
| 37 | :mod:`asyncore`'s event loop on instantiation. |
| 38 | |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 39 | *data_size_limit* specifies the maximum number of bytes that will be |
| 40 | accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no |
| 41 | limit. |
| 42 | |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 43 | *map* is the socket map to use for connections (an initially empty |
| 44 | dictionary is a suitable value). If not specified the :mod:`asyncore` |
| 45 | global socket map is used. |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 46 | |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 47 | *enable_SMTPUTF8* determins whether the ``SMTPUTF8`` extension (as defined |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame^] | 48 | in :RFC:`6531`) should be enabled. The default is ``False``. |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 49 | When ``True``, ``SMTPUTF8`` is accepted as a parameter to the ``MAIL`` |
| 50 | command and when present is passed to :meth:`process_message` in the |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame^] | 51 | ``kwargs['mail_options']`` list. *decode_data* and *enable_SMTPUTF8* |
| 52 | cannot be set to ``True`` at the same time. |
Vinay Sajip | 30298b4 | 2013-06-07 15:21:41 +0100 | [diff] [blame] | 53 | |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 54 | *decode_data* specifies whether the data portion of the SMTP transaction |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame^] | 55 | should be decoded using UTF-8. The default is ``False``. When |
| 56 | *decode_data* is not set to ``True`` the server advertises the ``8BITMIME`` |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 57 | extension (:rfc:`6152`), accepts the ``BODY=8BITMIME`` parameter to |
| 58 | the ``MAIL`` command, and when present passes it to :meth:`process_message` |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame^] | 59 | in the ``kwargs['mail_options']`` list. *decode_data* and *enable_SMTPUTF8* |
| 60 | cannot be set to ``True`` at the same time. |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 61 | |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 62 | .. method:: process_message(peer, mailfrom, rcpttos, data, **kwargs) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 63 | |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 64 | Raise a :exc:`NotImplementedError` exception. Override this in subclasses to |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 65 | do something useful with this message. Whatever was passed in the |
| 66 | constructor as *remoteaddr* will be available as the :attr:`_remoteaddr` |
| 67 | attribute. *peer* is the remote host's address, *mailfrom* is the envelope |
| 68 | originator, *rcpttos* are the envelope recipients and *data* is a string |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 69 | containing the contents of the e-mail (which should be in :rfc:`5321` |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 70 | format). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 72 | If the *decode_data* constructor keyword is set to ``True``, the *data* |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame^] | 73 | parameter will be a unicode string. If it is set to ``False``, it |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 74 | will be a bytes object. |
| 75 | |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 76 | *kwargs* is a dictionary containing additional information. It is empty |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame^] | 77 | if ``decode_data=True`` was given as an init argument, otherwise |
| 78 | it contains the following keys: |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 79 | |
| 80 | *mail_options*: |
| 81 | a list of all received parameters to the ``MAIL`` |
| 82 | command (the elements are uppercase strings; example: |
| 83 | ``['BODY=8BITMIME', 'SMTPUTF8']``). |
| 84 | |
| 85 | *rcpt_options*: |
| 86 | same as *mail_options* but for the ``RCPT`` command. |
| 87 | Currently no ``RCPT TO`` options are supported, so for now |
| 88 | this will always be an empty list. |
| 89 | |
R David Murray | 3113765 | 2015-05-16 14:16:33 -0400 | [diff] [blame] | 90 | Implementations of ``process_message`` should use the ``**kwargs`` |
R David Murray | e09b42c | 2015-05-19 07:18:39 -0400 | [diff] [blame] | 91 | signature to accept arbitrary keyword arguments, since future feature |
R David Murray | 3113765 | 2015-05-16 14:16:33 -0400 | [diff] [blame] | 92 | enhancements may add keys to the kwargs dictionary. |
| 93 | |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 94 | Return ``None`` to request a normal ``250 Ok`` response; otherwise |
| 95 | return the desired response string in :RFC:`5321` format. |
| 96 | |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 97 | .. attribute:: channel_class |
| 98 | |
| 99 | Override this in subclasses to use a custom :class:`SMTPChannel` for |
| 100 | managing SMTP clients. |
| 101 | |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 102 | .. versionadded:: 3.4 |
| 103 | The *map* constructor argument. |
Vinay Sajip | 30298b4 | 2013-06-07 15:21:41 +0100 | [diff] [blame] | 104 | |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 105 | .. versionchanged:: 3.5 |
| 106 | *localaddr* and *remoteaddr* may now contain IPv6 addresses. |
| 107 | |
| 108 | .. versionadded:: 3.5 |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame^] | 109 | The *decode_data* and *enable_SMTPUTF8* constructor parameters, and the |
| 110 | *kwargs* parameter to :meth:`process_message` when one or more of these is |
R David Murray | a33df31 | 2015-05-11 12:11:40 -0400 | [diff] [blame] | 111 | specified. |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 112 | |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame^] | 113 | .. versionchanged:: 3.6 |
| 114 | *decode_data* is now ``False`` by default. |
| 115 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 116 | |
| 117 | DebuggingServer Objects |
| 118 | ----------------------- |
| 119 | |
| 120 | |
| 121 | .. class:: DebuggingServer(localaddr, remoteaddr) |
| 122 | |
| 123 | Create a new debugging server. Arguments are as per :class:`SMTPServer`. |
| 124 | Messages will be discarded, and printed on stdout. |
| 125 | |
| 126 | |
| 127 | PureProxy Objects |
| 128 | ----------------- |
| 129 | |
| 130 | |
| 131 | .. class:: PureProxy(localaddr, remoteaddr) |
| 132 | |
| 133 | Create a new pure proxy server. Arguments are as per :class:`SMTPServer`. |
| 134 | Everything will be relayed to *remoteaddr*. Note that running this has a good |
| 135 | chance to make you into an open relay, so please be careful. |
| 136 | |
| 137 | |
| 138 | MailmanProxy Objects |
| 139 | -------------------- |
| 140 | |
| 141 | |
| 142 | .. class:: MailmanProxy(localaddr, remoteaddr) |
| 143 | |
| 144 | Create a new pure proxy server. Arguments are as per :class:`SMTPServer`. |
| 145 | Everything will be relayed to *remoteaddr*, unless local mailman configurations |
| 146 | knows about an address, in which case it will be handled via mailman. Note that |
| 147 | running this has a good chance to make you into an open relay, so please be |
| 148 | careful. |
| 149 | |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 150 | SMTPChannel Objects |
| 151 | ------------------- |
| 152 | |
Serhiy Storchaka | 98b28fd | 2013-10-13 23:12:09 +0300 | [diff] [blame] | 153 | .. class:: SMTPChannel(server, conn, addr, data_size_limit=33554432,\ |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame^] | 154 | map=None, enable_SMTPUTF8=False, decode_data=False) |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 155 | |
| 156 | Create a new :class:`SMTPChannel` object which manages the communication |
| 157 | between the server and a single SMTP client. |
| 158 | |
Vinay Sajip | 30298b4 | 2013-06-07 15:21:41 +0100 | [diff] [blame] | 159 | *conn* and *addr* are as per the instance variables described below. |
| 160 | |
| 161 | *data_size_limit* specifies the maximum number of bytes that will be |
| 162 | accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no |
| 163 | limit. |
| 164 | |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 165 | *enable_SMTPUTF8* determins whether the ``SMTPUTF8`` extension (as defined |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame^] | 166 | in :RFC:`6531`) should be enabled. The default is ``False``. |
| 167 | *decode_data* and *enable_SMTPUTF8* cannot be set to ``True`` at the same |
| 168 | time. |
R David Murray | 2539e67 | 2014-08-09 16:40:49 -0400 | [diff] [blame] | 169 | |
Vinay Sajip | 30298b4 | 2013-06-07 15:21:41 +0100 | [diff] [blame] | 170 | A dictionary can be specified in *map* to avoid using a global socket map. |
| 171 | |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 172 | *decode_data* specifies whether the data portion of the SMTP transaction |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame^] | 173 | should be decoded using UTF-8. The default is ``False``. |
| 174 | *decode_data* and *enable_SMTPUTF8* cannot be set to ``True`` at the same |
| 175 | time. |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 176 | |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 177 | To use a custom SMTPChannel implementation you need to override the |
| 178 | :attr:`SMTPServer.channel_class` of your :class:`SMTPServer`. |
| 179 | |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 180 | .. versionchanged:: 3.5 |
Serhiy Storchaka | cbcc2fd | 2016-05-16 09:36:31 +0300 | [diff] [blame^] | 181 | The *decode_data* and *enable_SMTPUTF8* parameters were added. |
| 182 | |
| 183 | .. versionchanged:: 3.6 |
| 184 | *decode_data* is now ``False`` by default. |
R David Murray | 554bcbf | 2014-06-11 11:18:08 -0400 | [diff] [blame] | 185 | |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 186 | The :class:`SMTPChannel` has the following instance variables: |
| 187 | |
| 188 | .. attribute:: smtp_server |
| 189 | |
| 190 | Holds the :class:`SMTPServer` that spawned this channel. |
| 191 | |
| 192 | .. attribute:: conn |
| 193 | |
| 194 | Holds the socket object connecting to the client. |
| 195 | |
| 196 | .. attribute:: addr |
| 197 | |
| 198 | Holds the address of the client, the second value returned by |
Ezio Melotti | 8bbcb58 | 2012-09-20 09:06:51 +0300 | [diff] [blame] | 199 | :func:`socket.accept <socket.socket.accept>` |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 200 | |
| 201 | .. attribute:: received_lines |
| 202 | |
| 203 | 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] | 204 | the client. The lines have their ``"\r\n"`` line ending translated to |
| 205 | ``"\n"``. |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 206 | |
| 207 | .. attribute:: smtp_state |
| 208 | |
| 209 | Holds the current state of the channel. This will be either |
| 210 | :attr:`COMMAND` initially and then :attr:`DATA` after the client sends |
| 211 | a "DATA" line. |
| 212 | |
| 213 | .. attribute:: seen_greeting |
| 214 | |
| 215 | Holds a string containing the greeting sent by the client in its "HELO". |
| 216 | |
| 217 | .. attribute:: mailfrom |
| 218 | |
| 219 | Holds a string containing the address identified in the "MAIL FROM:" line |
| 220 | from the client. |
| 221 | |
| 222 | .. attribute:: rcpttos |
| 223 | |
| 224 | Holds a list of strings containing the addresses identified in the |
| 225 | "RCPT TO:" lines from the client. |
| 226 | |
| 227 | .. attribute:: received_data |
| 228 | |
| 229 | 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] | 230 | 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] | 231 | |
| 232 | .. attribute:: fqdn |
| 233 | |
| 234 | Holds the fully-qualified domain name of the server as returned by |
Ezio Melotti | 8bbcb58 | 2012-09-20 09:06:51 +0300 | [diff] [blame] | 235 | :func:`socket.getfqdn`. |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 236 | |
| 237 | .. attribute:: peer |
| 238 | |
| 239 | Holds the name of the client peer as returned by ``conn.getpeername()`` |
| 240 | where ``conn`` is :attr:`conn`. |
| 241 | |
| 242 | The :class:`SMTPChannel` operates by invoking methods named ``smtp_<command>`` |
| 243 | upon reception of a command line from the client. Built into the base |
| 244 | :class:`SMTPChannel` class are methods for handling the following commands |
| 245 | (and responding to them appropriately): |
| 246 | |
| 247 | ======== =================================================================== |
| 248 | Command Action taken |
| 249 | ======== =================================================================== |
| 250 | HELO Accepts the greeting from the client and stores it in |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 251 | :attr:`seen_greeting`. Sets server to base command mode. |
| 252 | EHLO Accepts the greeting from the client and stores it in |
| 253 | :attr:`seen_greeting`. Sets server to extended command mode. |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 254 | NOOP Takes no action. |
| 255 | QUIT Closes the connection cleanly. |
| 256 | 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] | 257 | :attr:`mailfrom`. In extended command mode, accepts the |
| 258 | :rfc:`1870` SIZE attribute and responds appropriately based on the |
Ezio Melotti | a58d8b0 | 2012-09-20 09:09:24 +0300 | [diff] [blame] | 259 | value of *data_size_limit*. |
Richard Jones | 803ef8a | 2010-07-24 09:51:40 +0000 | [diff] [blame] | 260 | RCPT Accepts the "RCPT TO:" syntax and stores the supplied addresses in |
| 261 | the :attr:`rcpttos` list. |
| 262 | RSET Resets the :attr:`mailfrom`, :attr:`rcpttos`, and |
| 263 | :attr:`received_data`, but not the greeting. |
| 264 | DATA Sets the internal state to :attr:`DATA` and stores remaining lines |
| 265 | from the client in :attr:`received_data` until the terminator |
Ezio Melotti | 8bbcb58 | 2012-09-20 09:06:51 +0300 | [diff] [blame] | 266 | ``"\r\n.\r\n"`` is received. |
R David Murray | d1a30c9 | 2012-05-26 14:33:59 -0400 | [diff] [blame] | 267 | HELP Returns minimal information on command syntax |
| 268 | VRFY Returns code 252 (the server doesn't know if the address is valid) |
| 269 | EXPN Reports that the command is not implemented. |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 270 | ======== =================================================================== |