blob: e383201aab1461c597e7d0ba0ec05e5d738aa4a8 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`smtpd` --- SMTP Server
2============================
3
4.. module:: smtpd
5 :synopsis: A SMTP server implementation in Python.
6
Andrew Kuchling587e9702013-11-12 10:02:35 -05007.. moduleauthor:: Barry Warsaw <barry@python.org>
Georg Brandl116aa622007-08-15 14:28:22 +00008.. sectionauthor:: Moshe Zadka <moshez@moshez.org>
9
Raymond Hettinger469271d2011-01-27 20:38:46 +000010**Source code:** :source:`Lib/smtpd.py`
Georg Brandl116aa622007-08-15 14:28:22 +000011
Raymond Hettinger469271d2011-01-27 20:38:46 +000012--------------
Georg Brandl116aa622007-08-15 14:28:22 +000013
Richard Jones803ef8a2010-07-24 09:51:40 +000014This module offers several classes to implement SMTP (email) servers.
15
16Several server implementations are present; one is a generic
Georg Brandl116aa622007-08-15 14:28:22 +000017do-nothing implementation, which can be overridden, while the other two offer
18specific mail-sending strategies.
19
Richard Jones803ef8a2010-07-24 09:51:40 +000020Additionally the SMTPChannel may be extended to implement very specific
21interaction behaviour with SMTP clients.
Georg Brandl116aa622007-08-15 14:28:22 +000022
R David Murray2539e672014-08-09 16:40:49 -040023The code supports :RFC:`5321`, plus the :rfc:`1870` SIZE and :rfc:`6531`
24SMTPUTF8 extensions.
R David Murrayd1a30c92012-05-26 14:33:59 -040025
26
Georg Brandl116aa622007-08-15 14:28:22 +000027SMTPServer Objects
28------------------
29
30
Serhiy Storchaka98b28fd2013-10-13 23:12:09 +030031.. class:: SMTPServer(localaddr, remoteaddr, data_size_limit=33554432,\
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +030032 map=None, enable_SMTPUTF8=False, decode_data=False)
Georg Brandl116aa622007-08-15 14:28:22 +000033
34 Create a new :class:`SMTPServer` object, which binds to local address
R David Murray6b46ec72016-09-07 14:01:23 -040035 *localaddr*. It will treat *remoteaddr* as an upstream SMTP relayer. Both
36 *localaddr* and *remoteaddr* should be a :ref:`(host, port) <host_port>`
37 tuple. The object inherits from :class:`asyncore.dispatcher`, and so will
38 insert itself into :mod:`asyncore`'s event loop on instantiation.
Georg Brandl116aa622007-08-15 14:28:22 +000039
R David Murrayd1a30c92012-05-26 14:33:59 -040040 *data_size_limit* specifies the maximum number of bytes that will be
41 accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no
42 limit.
43
R David Murraya33df312015-05-11 12:11:40 -040044 *map* is the socket map to use for connections (an initially empty
45 dictionary is a suitable value). If not specified the :mod:`asyncore`
46 global socket map is used.
R David Murray2539e672014-08-09 16:40:49 -040047
Raymond Hettinger15f44ab2016-08-30 10:47:49 -070048 *enable_SMTPUTF8* determines whether the ``SMTPUTF8`` extension (as defined
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +030049 in :RFC:`6531`) should be enabled. The default is ``False``.
R David Murraya33df312015-05-11 12:11:40 -040050 When ``True``, ``SMTPUTF8`` is accepted as a parameter to the ``MAIL``
51 command and when present is passed to :meth:`process_message` in the
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +030052 ``kwargs['mail_options']`` list. *decode_data* and *enable_SMTPUTF8*
53 cannot be set to ``True`` at the same time.
Vinay Sajip30298b42013-06-07 15:21:41 +010054
R David Murray554bcbf2014-06-11 11:18:08 -040055 *decode_data* specifies whether the data portion of the SMTP transaction
Serhiy Storchaka8c740c42016-05-29 23:43:24 +030056 should be decoded using UTF-8. When *decode_data* is ``False`` (the
57 default), the server advertises the ``8BITMIME``
R David Murraya33df312015-05-11 12:11:40 -040058 extension (:rfc:`6152`), accepts the ``BODY=8BITMIME`` parameter to
59 the ``MAIL`` command, and when present passes it to :meth:`process_message`
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +030060 in the ``kwargs['mail_options']`` list. *decode_data* and *enable_SMTPUTF8*
61 cannot be set to ``True`` at the same time.
R David Murray554bcbf2014-06-11 11:18:08 -040062
R David Murraya33df312015-05-11 12:11:40 -040063 .. method:: process_message(peer, mailfrom, rcpttos, data, **kwargs)
Georg Brandl116aa622007-08-15 14:28:22 +000064
R David Murray2539e672014-08-09 16:40:49 -040065 Raise a :exc:`NotImplementedError` exception. Override this in subclasses to
Benjamin Petersone41251e2008-04-25 01:59:09 +000066 do something useful with this message. Whatever was passed in the
67 constructor as *remoteaddr* will be available as the :attr:`_remoteaddr`
68 attribute. *peer* is the remote host's address, *mailfrom* is the envelope
69 originator, *rcpttos* are the envelope recipients and *data* is a string
R David Murray2539e672014-08-09 16:40:49 -040070 containing the contents of the e-mail (which should be in :rfc:`5321`
Benjamin Petersone41251e2008-04-25 01:59:09 +000071 format).
Georg Brandl116aa622007-08-15 14:28:22 +000072
R David Murray554bcbf2014-06-11 11:18:08 -040073 If the *decode_data* constructor keyword is set to ``True``, the *data*
Serhiy Storchaka8c740c42016-05-29 23:43:24 +030074 argument will be a unicode string. If it is set to ``False``, it
R David Murray554bcbf2014-06-11 11:18:08 -040075 will be a bytes object.
76
R David Murraya33df312015-05-11 12:11:40 -040077 *kwargs* is a dictionary containing additional information. It is empty
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +030078 if ``decode_data=True`` was given as an init argument, otherwise
79 it contains the following keys:
R David Murraya33df312015-05-11 12:11:40 -040080
81 *mail_options*:
82 a list of all received parameters to the ``MAIL``
83 command (the elements are uppercase strings; example:
84 ``['BODY=8BITMIME', 'SMTPUTF8']``).
85
86 *rcpt_options*:
87 same as *mail_options* but for the ``RCPT`` command.
88 Currently no ``RCPT TO`` options are supported, so for now
89 this will always be an empty list.
90
R David Murray31137652015-05-16 14:16:33 -040091 Implementations of ``process_message`` should use the ``**kwargs``
R David Murraye09b42c2015-05-19 07:18:39 -040092 signature to accept arbitrary keyword arguments, since future feature
R David Murray31137652015-05-16 14:16:33 -040093 enhancements may add keys to the kwargs dictionary.
94
R David Murray2539e672014-08-09 16:40:49 -040095 Return ``None`` to request a normal ``250 Ok`` response; otherwise
96 return the desired response string in :RFC:`5321` format.
97
Richard Jones803ef8a2010-07-24 09:51:40 +000098 .. attribute:: channel_class
99
100 Override this in subclasses to use a custom :class:`SMTPChannel` for
101 managing SMTP clients.
102
R David Murraya33df312015-05-11 12:11:40 -0400103 .. versionadded:: 3.4
104 The *map* constructor argument.
Vinay Sajip30298b42013-06-07 15:21:41 +0100105
R David Murray2539e672014-08-09 16:40:49 -0400106 .. versionchanged:: 3.5
107 *localaddr* and *remoteaddr* may now contain IPv6 addresses.
108
109 .. versionadded:: 3.5
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +0300110 The *decode_data* and *enable_SMTPUTF8* constructor parameters, and the
Serhiy Storchaka8c740c42016-05-29 23:43:24 +0300111 *kwargs* parameter to :meth:`process_message` when *decode_data* is
112 ``False``.
R David Murray554bcbf2014-06-11 11:18:08 -0400113
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +0300114 .. versionchanged:: 3.6
115 *decode_data* is now ``False`` by default.
116
Georg Brandl116aa622007-08-15 14:28:22 +0000117
118DebuggingServer Objects
119-----------------------
120
121
122.. class:: DebuggingServer(localaddr, remoteaddr)
123
124 Create a new debugging server. Arguments are as per :class:`SMTPServer`.
125 Messages will be discarded, and printed on stdout.
126
127
128PureProxy Objects
129-----------------
130
131
132.. class:: PureProxy(localaddr, remoteaddr)
133
134 Create a new pure proxy server. Arguments are as per :class:`SMTPServer`.
135 Everything will be relayed to *remoteaddr*. Note that running this has a good
136 chance to make you into an open relay, so please be careful.
137
138
139MailmanProxy Objects
140--------------------
141
142
143.. class:: MailmanProxy(localaddr, remoteaddr)
144
145 Create a new pure proxy server. Arguments are as per :class:`SMTPServer`.
146 Everything will be relayed to *remoteaddr*, unless local mailman configurations
147 knows about an address, in which case it will be handled via mailman. Note that
148 running this has a good chance to make you into an open relay, so please be
149 careful.
150
Richard Jones803ef8a2010-07-24 09:51:40 +0000151SMTPChannel Objects
152-------------------
153
Serhiy Storchaka98b28fd2013-10-13 23:12:09 +0300154.. class:: SMTPChannel(server, conn, addr, data_size_limit=33554432,\
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +0300155 map=None, enable_SMTPUTF8=False, decode_data=False)
Richard Jones803ef8a2010-07-24 09:51:40 +0000156
157 Create a new :class:`SMTPChannel` object which manages the communication
158 between the server and a single SMTP client.
159
Vinay Sajip30298b42013-06-07 15:21:41 +0100160 *conn* and *addr* are as per the instance variables described below.
161
162 *data_size_limit* specifies the maximum number of bytes that will be
163 accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no
164 limit.
165
Raymond Hettinger15f44ab2016-08-30 10:47:49 -0700166 *enable_SMTPUTF8* determines whether the ``SMTPUTF8`` extension (as defined
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +0300167 in :RFC:`6531`) should be enabled. The default is ``False``.
168 *decode_data* and *enable_SMTPUTF8* cannot be set to ``True`` at the same
169 time.
R David Murray2539e672014-08-09 16:40:49 -0400170
Vinay Sajip30298b42013-06-07 15:21:41 +0100171 A dictionary can be specified in *map* to avoid using a global socket map.
172
R David Murray554bcbf2014-06-11 11:18:08 -0400173 *decode_data* specifies whether the data portion of the SMTP transaction
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +0300174 should be decoded using UTF-8. The default is ``False``.
175 *decode_data* and *enable_SMTPUTF8* cannot be set to ``True`` at the same
176 time.
R David Murray554bcbf2014-06-11 11:18:08 -0400177
Richard Jones803ef8a2010-07-24 09:51:40 +0000178 To use a custom SMTPChannel implementation you need to override the
179 :attr:`SMTPServer.channel_class` of your :class:`SMTPServer`.
180
R David Murray554bcbf2014-06-11 11:18:08 -0400181 .. versionchanged:: 3.5
Serhiy Storchakacbcc2fd2016-05-16 09:36:31 +0300182 The *decode_data* and *enable_SMTPUTF8* parameters were added.
183
184 .. versionchanged:: 3.6
185 *decode_data* is now ``False`` by default.
R David Murray554bcbf2014-06-11 11:18:08 -0400186
Richard Jones803ef8a2010-07-24 09:51:40 +0000187 The :class:`SMTPChannel` has the following instance variables:
188
189 .. attribute:: smtp_server
190
191 Holds the :class:`SMTPServer` that spawned this channel.
192
193 .. attribute:: conn
194
195 Holds the socket object connecting to the client.
196
197 .. attribute:: addr
198
199 Holds the address of the client, the second value returned by
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300200 :func:`socket.accept <socket.socket.accept>`
Richard Jones803ef8a2010-07-24 09:51:40 +0000201
202 .. attribute:: received_lines
203
204 Holds a list of the line strings (decoded using UTF-8) received from
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300205 the client. The lines have their ``"\r\n"`` line ending translated to
206 ``"\n"``.
Richard Jones803ef8a2010-07-24 09:51:40 +0000207
208 .. attribute:: smtp_state
209
210 Holds the current state of the channel. This will be either
211 :attr:`COMMAND` initially and then :attr:`DATA` after the client sends
212 a "DATA" line.
213
214 .. attribute:: seen_greeting
215
216 Holds a string containing the greeting sent by the client in its "HELO".
217
218 .. attribute:: mailfrom
219
220 Holds a string containing the address identified in the "MAIL FROM:" line
221 from the client.
222
223 .. attribute:: rcpttos
224
225 Holds a list of strings containing the addresses identified in the
226 "RCPT TO:" lines from the client.
227
228 .. attribute:: received_data
229
230 Holds a string containing all of the data sent by the client during the
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300231 DATA state, up to but not including the terminating ``"\r\n.\r\n"``.
Richard Jones803ef8a2010-07-24 09:51:40 +0000232
233 .. attribute:: fqdn
234
235 Holds the fully-qualified domain name of the server as returned by
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300236 :func:`socket.getfqdn`.
Richard Jones803ef8a2010-07-24 09:51:40 +0000237
238 .. attribute:: peer
239
240 Holds the name of the client peer as returned by ``conn.getpeername()``
241 where ``conn`` is :attr:`conn`.
242
243 The :class:`SMTPChannel` operates by invoking methods named ``smtp_<command>``
244 upon reception of a command line from the client. Built into the base
245 :class:`SMTPChannel` class are methods for handling the following commands
246 (and responding to them appropriately):
247
248 ======== ===================================================================
249 Command Action taken
250 ======== ===================================================================
251 HELO Accepts the greeting from the client and stores it in
R David Murrayd1a30c92012-05-26 14:33:59 -0400252 :attr:`seen_greeting`. Sets server to base command mode.
253 EHLO Accepts the greeting from the client and stores it in
254 :attr:`seen_greeting`. Sets server to extended command mode.
Richard Jones803ef8a2010-07-24 09:51:40 +0000255 NOOP Takes no action.
256 QUIT Closes the connection cleanly.
257 MAIL Accepts the "MAIL FROM:" syntax and stores the supplied address as
R David Murrayd1a30c92012-05-26 14:33:59 -0400258 :attr:`mailfrom`. In extended command mode, accepts the
259 :rfc:`1870` SIZE attribute and responds appropriately based on the
Ezio Melottia58d8b02012-09-20 09:09:24 +0300260 value of *data_size_limit*.
Richard Jones803ef8a2010-07-24 09:51:40 +0000261 RCPT Accepts the "RCPT TO:" syntax and stores the supplied addresses in
262 the :attr:`rcpttos` list.
263 RSET Resets the :attr:`mailfrom`, :attr:`rcpttos`, and
264 :attr:`received_data`, but not the greeting.
265 DATA Sets the internal state to :attr:`DATA` and stores remaining lines
266 from the client in :attr:`received_data` until the terminator
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300267 ``"\r\n.\r\n"`` is received.
R David Murrayd1a30c92012-05-26 14:33:59 -0400268 HELP Returns minimal information on command syntax
269 VRFY Returns code 252 (the server doesn't know if the address is valid)
270 EXPN Reports that the command is not implemented.
Raymond Hettinger469271d2011-01-27 20:38:46 +0000271 ======== ===================================================================