blob: 3e0c6fbff515ff63e54578846778a175d6b98fe9 [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,\
R David Murray2539e672014-08-09 16:40:49 -040032 map=None, enable_SMTPUTF8=False, decode_data=True)
Georg Brandl116aa622007-08-15 14:28:22 +000033
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 Murrayd1a30c92012-05-26 14:33:59 -040039 *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 Murray2539e672014-08-09 16:40:49 -040043 *enable_SMTPUTF8* determins whether the ``SMTPUTF8`` extension (as defined
44 in :RFC:`6531`) should be enabled. The default is ``False``. If
45 *enable_SMTPUTF* is set to ``True``, the :meth:`process_smtputf8_message`
46 method must be defined. A :exc:`ValueError` is raised if both
47 *enable_SMTPUTF8* and *decode_data* are set to ``True`` at the same time.
48
Vinay Sajip30298b42013-06-07 15:21:41 +010049 A dictionary can be specified in *map* to avoid using a global socket map.
50
R David Murray554bcbf2014-06-11 11:18:08 -040051 *decode_data* specifies whether the data portion of the SMTP transaction
52 should be decoded using UTF-8. The default is ``True`` for backward
53 compatibility reasons, but will change to ``False`` in Python 3.6. Specify
54 the keyword value explicitly to avoid the :exc:`DeprecationWarning`.
55
Benjamin Petersone41251e2008-04-25 01:59:09 +000056 .. method:: process_message(peer, mailfrom, rcpttos, data)
Georg Brandl116aa622007-08-15 14:28:22 +000057
R David Murray2539e672014-08-09 16:40:49 -040058 Raise a :exc:`NotImplementedError` exception. Override this in subclasses to
Benjamin Petersone41251e2008-04-25 01:59:09 +000059 do something useful with this message. Whatever was passed in the
60 constructor as *remoteaddr* will be available as the :attr:`_remoteaddr`
61 attribute. *peer* is the remote host's address, *mailfrom* is the envelope
62 originator, *rcpttos* are the envelope recipients and *data* is a string
R David Murray2539e672014-08-09 16:40:49 -040063 containing the contents of the e-mail (which should be in :rfc:`5321`
Benjamin Petersone41251e2008-04-25 01:59:09 +000064 format).
Georg Brandl116aa622007-08-15 14:28:22 +000065
R David Murray554bcbf2014-06-11 11:18:08 -040066 If the *decode_data* constructor keyword is set to ``True``, the *data*
67 argument will be a unicode string. If it is set to ``False``, it
68 will be a bytes object.
69
R David Murray2539e672014-08-09 16:40:49 -040070 Return ``None`` to request a normal ``250 Ok`` response; otherwise
71 return the desired response string in :RFC:`5321` format.
72
73 .. method:: process_smtputf8_message(peer, mailfrom, rcpttos, data)
74
75 Raise a :exc:`NotImplementedError` exception. Override this in
76 subclasses to do something useful with messages when *enable_SMTPUTF8*
77 has been set to ``True`` and the SMTP client requested ``SMTPUTF8``,
78 since this method is called rather than :meth:`process_message` when the
79 client actively requests ``SMTPUTF8``. The *data* argument will always
80 be a bytes object, and any non-``None`` return value should conform to
81 :rfc:`6531`; otherwise, the API is the same as for
82 :meth:`process_message`.
83
Richard Jones803ef8a2010-07-24 09:51:40 +000084 .. attribute:: channel_class
85
86 Override this in subclasses to use a custom :class:`SMTPChannel` for
87 managing SMTP clients.
88
Vinay Sajip30298b42013-06-07 15:21:41 +010089 .. versionchanged:: 3.4
90 The *map* argument was added.
91
R David Murray2539e672014-08-09 16:40:49 -040092 .. versionchanged:: 3.5
93 *localaddr* and *remoteaddr* may now contain IPv6 addresses.
94
95 .. versionadded:: 3.5
96 the *decode_data* and *enable_SMTPUTF8* constructor arguments, and the
97 :meth:`process_smtputf8_message` method.
R David Murray554bcbf2014-06-11 11:18:08 -040098
Georg Brandl116aa622007-08-15 14:28:22 +000099
100DebuggingServer Objects
101-----------------------
102
103
104.. class:: DebuggingServer(localaddr, remoteaddr)
105
106 Create a new debugging server. Arguments are as per :class:`SMTPServer`.
107 Messages will be discarded, and printed on stdout.
108
109
110PureProxy Objects
111-----------------
112
113
114.. class:: PureProxy(localaddr, remoteaddr)
115
116 Create a new pure proxy server. Arguments are as per :class:`SMTPServer`.
117 Everything will be relayed to *remoteaddr*. Note that running this has a good
118 chance to make you into an open relay, so please be careful.
119
120
121MailmanProxy Objects
122--------------------
123
124
125.. class:: MailmanProxy(localaddr, remoteaddr)
126
127 Create a new pure proxy server. Arguments are as per :class:`SMTPServer`.
128 Everything will be relayed to *remoteaddr*, unless local mailman configurations
129 knows about an address, in which case it will be handled via mailman. Note that
130 running this has a good chance to make you into an open relay, so please be
131 careful.
132
Richard Jones803ef8a2010-07-24 09:51:40 +0000133SMTPChannel Objects
134-------------------
135
Serhiy Storchaka98b28fd2013-10-13 23:12:09 +0300136.. class:: SMTPChannel(server, conn, addr, data_size_limit=33554432,\
R David Murray2539e672014-08-09 16:40:49 -0400137 map=None, enable_SMTPUTF8=False, decode_data=True)
Richard Jones803ef8a2010-07-24 09:51:40 +0000138
139 Create a new :class:`SMTPChannel` object which manages the communication
140 between the server and a single SMTP client.
141
Vinay Sajip30298b42013-06-07 15:21:41 +0100142 *conn* and *addr* are as per the instance variables described below.
143
144 *data_size_limit* specifies the maximum number of bytes that will be
145 accepted in a ``DATA`` command. A value of ``None`` or ``0`` means no
146 limit.
147
R David Murray2539e672014-08-09 16:40:49 -0400148 *enable_SMTPUTF8* determins whether the ``SMTPUTF8`` extension (as defined
149 in :RFC:`6531`) should be enabled. The default is ``False``. A
150 :exc:`ValueError` is raised if both *enable_SMTPUTF8* and *decode_data* are
151 set to ``True`` at the same time.
152
Vinay Sajip30298b42013-06-07 15:21:41 +0100153 A dictionary can be specified in *map* to avoid using a global socket map.
154
R David Murray554bcbf2014-06-11 11:18:08 -0400155 *decode_data* specifies whether the data portion of the SMTP transaction
156 should be decoded using UTF-8. The default is ``True`` for backward
157 compatibility reasons, but will change to ``False`` in Python 3.6. Specify
158 the keyword value explicitly to avoid the :exc:`DeprecationWarning`.
159
Richard Jones803ef8a2010-07-24 09:51:40 +0000160 To use a custom SMTPChannel implementation you need to override the
161 :attr:`SMTPServer.channel_class` of your :class:`SMTPServer`.
162
R David Murray554bcbf2014-06-11 11:18:08 -0400163 .. versionchanged:: 3.5
R David Murray2539e672014-08-09 16:40:49 -0400164 the *decode_data* and *enable_SMTPUTF8* arguments were added.
R David Murray554bcbf2014-06-11 11:18:08 -0400165
Richard Jones803ef8a2010-07-24 09:51:40 +0000166 The :class:`SMTPChannel` has the following instance variables:
167
168 .. attribute:: smtp_server
169
170 Holds the :class:`SMTPServer` that spawned this channel.
171
172 .. attribute:: conn
173
174 Holds the socket object connecting to the client.
175
176 .. attribute:: addr
177
178 Holds the address of the client, the second value returned by
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300179 :func:`socket.accept <socket.socket.accept>`
Richard Jones803ef8a2010-07-24 09:51:40 +0000180
181 .. attribute:: received_lines
182
183 Holds a list of the line strings (decoded using UTF-8) received from
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300184 the client. The lines have their ``"\r\n"`` line ending translated to
185 ``"\n"``.
Richard Jones803ef8a2010-07-24 09:51:40 +0000186
187 .. attribute:: smtp_state
188
189 Holds the current state of the channel. This will be either
190 :attr:`COMMAND` initially and then :attr:`DATA` after the client sends
191 a "DATA" line.
192
193 .. attribute:: seen_greeting
194
195 Holds a string containing the greeting sent by the client in its "HELO".
196
197 .. attribute:: mailfrom
198
199 Holds a string containing the address identified in the "MAIL FROM:" line
200 from the client.
201
202 .. attribute:: rcpttos
203
204 Holds a list of strings containing the addresses identified in the
205 "RCPT TO:" lines from the client.
206
207 .. attribute:: received_data
208
209 Holds a string containing all of the data sent by the client during the
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300210 DATA state, up to but not including the terminating ``"\r\n.\r\n"``.
Richard Jones803ef8a2010-07-24 09:51:40 +0000211
212 .. attribute:: fqdn
213
214 Holds the fully-qualified domain name of the server as returned by
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300215 :func:`socket.getfqdn`.
Richard Jones803ef8a2010-07-24 09:51:40 +0000216
217 .. attribute:: peer
218
219 Holds the name of the client peer as returned by ``conn.getpeername()``
220 where ``conn`` is :attr:`conn`.
221
222 The :class:`SMTPChannel` operates by invoking methods named ``smtp_<command>``
223 upon reception of a command line from the client. Built into the base
224 :class:`SMTPChannel` class are methods for handling the following commands
225 (and responding to them appropriately):
226
227 ======== ===================================================================
228 Command Action taken
229 ======== ===================================================================
230 HELO Accepts the greeting from the client and stores it in
R David Murrayd1a30c92012-05-26 14:33:59 -0400231 :attr:`seen_greeting`. Sets server to base command mode.
232 EHLO Accepts the greeting from the client and stores it in
233 :attr:`seen_greeting`. Sets server to extended command mode.
Richard Jones803ef8a2010-07-24 09:51:40 +0000234 NOOP Takes no action.
235 QUIT Closes the connection cleanly.
236 MAIL Accepts the "MAIL FROM:" syntax and stores the supplied address as
R David Murrayd1a30c92012-05-26 14:33:59 -0400237 :attr:`mailfrom`. In extended command mode, accepts the
238 :rfc:`1870` SIZE attribute and responds appropriately based on the
Ezio Melottia58d8b02012-09-20 09:09:24 +0300239 value of *data_size_limit*.
Richard Jones803ef8a2010-07-24 09:51:40 +0000240 RCPT Accepts the "RCPT TO:" syntax and stores the supplied addresses in
241 the :attr:`rcpttos` list.
242 RSET Resets the :attr:`mailfrom`, :attr:`rcpttos`, and
243 :attr:`received_data`, but not the greeting.
244 DATA Sets the internal state to :attr:`DATA` and stores remaining lines
245 from the client in :attr:`received_data` until the terminator
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300246 ``"\r\n.\r\n"`` is received.
R David Murrayd1a30c92012-05-26 14:33:59 -0400247 HELP Returns minimal information on command syntax
248 VRFY Returns code 252 (the server doesn't know if the address is valid)
249 EXPN Reports that the command is not implemented.
Raymond Hettinger469271d2011-01-27 20:38:46 +0000250 ======== ===================================================================