blob: bfdc7271f769ed74ed57fa8da86546920521d619 [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
7.. moduleauthor:: Barry Warsaw <barry@zope.com>
8.. 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
23SMTPServer Objects
24------------------
25
26
27.. class:: SMTPServer(localaddr, remoteaddr)
28
29 Create a new :class:`SMTPServer` object, which binds to local address
30 *localaddr*. It will treat *remoteaddr* as an upstream SMTP relayer. It
31 inherits from :class:`asyncore.dispatcher`, and so will insert itself into
32 :mod:`asyncore`'s event loop on instantiation.
33
Benjamin Petersone41251e2008-04-25 01:59:09 +000034 .. method:: process_message(peer, mailfrom, rcpttos, data)
Georg Brandl116aa622007-08-15 14:28:22 +000035
Benjamin Petersone41251e2008-04-25 01:59:09 +000036 Raise :exc:`NotImplementedError` exception. Override this in subclasses to
37 do something useful with this message. Whatever was passed in the
38 constructor as *remoteaddr* will be available as the :attr:`_remoteaddr`
39 attribute. *peer* is the remote host's address, *mailfrom* is the envelope
40 originator, *rcpttos* are the envelope recipients and *data* is a string
41 containing the contents of the e-mail (which should be in :rfc:`2822`
42 format).
Georg Brandl116aa622007-08-15 14:28:22 +000043
Richard Jones803ef8a2010-07-24 09:51:40 +000044 .. attribute:: channel_class
45
46 Override this in subclasses to use a custom :class:`SMTPChannel` for
47 managing SMTP clients.
48
Georg Brandl116aa622007-08-15 14:28:22 +000049
50DebuggingServer Objects
51-----------------------
52
53
54.. class:: DebuggingServer(localaddr, remoteaddr)
55
56 Create a new debugging server. Arguments are as per :class:`SMTPServer`.
57 Messages will be discarded, and printed on stdout.
58
59
60PureProxy Objects
61-----------------
62
63
64.. class:: PureProxy(localaddr, remoteaddr)
65
66 Create a new pure proxy server. Arguments are as per :class:`SMTPServer`.
67 Everything will be relayed to *remoteaddr*. Note that running this has a good
68 chance to make you into an open relay, so please be careful.
69
70
71MailmanProxy Objects
72--------------------
73
74
75.. class:: MailmanProxy(localaddr, remoteaddr)
76
77 Create a new pure proxy server. Arguments are as per :class:`SMTPServer`.
78 Everything will be relayed to *remoteaddr*, unless local mailman configurations
79 knows about an address, in which case it will be handled via mailman. Note that
80 running this has a good chance to make you into an open relay, so please be
81 careful.
82
Richard Jones803ef8a2010-07-24 09:51:40 +000083SMTPChannel Objects
84-------------------
85
86.. class:: SMTPChannel(server, conn, addr)
87
88 Create a new :class:`SMTPChannel` object which manages the communication
89 between the server and a single SMTP client.
90
91 To use a custom SMTPChannel implementation you need to override the
92 :attr:`SMTPServer.channel_class` of your :class:`SMTPServer`.
93
94 The :class:`SMTPChannel` has the following instance variables:
95
96 .. attribute:: smtp_server
97
98 Holds the :class:`SMTPServer` that spawned this channel.
99
100 .. attribute:: conn
101
102 Holds the socket object connecting to the client.
103
104 .. attribute:: addr
105
106 Holds the address of the client, the second value returned by
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300107 :func:`socket.accept <socket.socket.accept>`
Richard Jones803ef8a2010-07-24 09:51:40 +0000108
109 .. attribute:: received_lines
110
111 Holds a list of the line strings (decoded using UTF-8) received from
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300112 the client. The lines have their ``"\r\n"`` line ending translated to
113 ``"\n"``.
Richard Jones803ef8a2010-07-24 09:51:40 +0000114
115 .. attribute:: smtp_state
116
117 Holds the current state of the channel. This will be either
118 :attr:`COMMAND` initially and then :attr:`DATA` after the client sends
119 a "DATA" line.
120
121 .. attribute:: seen_greeting
122
123 Holds a string containing the greeting sent by the client in its "HELO".
124
125 .. attribute:: mailfrom
126
127 Holds a string containing the address identified in the "MAIL FROM:" line
128 from the client.
129
130 .. attribute:: rcpttos
131
132 Holds a list of strings containing the addresses identified in the
133 "RCPT TO:" lines from the client.
134
135 .. attribute:: received_data
136
137 Holds a string containing all of the data sent by the client during the
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300138 DATA state, up to but not including the terminating ``"\r\n.\r\n"``.
Richard Jones803ef8a2010-07-24 09:51:40 +0000139
140 .. attribute:: fqdn
141
142 Holds the fully-qualified domain name of the server as returned by
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300143 :func:`socket.getfqdn`.
Richard Jones803ef8a2010-07-24 09:51:40 +0000144
145 .. attribute:: peer
146
147 Holds the name of the client peer as returned by ``conn.getpeername()``
148 where ``conn`` is :attr:`conn`.
149
150 The :class:`SMTPChannel` operates by invoking methods named ``smtp_<command>``
151 upon reception of a command line from the client. Built into the base
152 :class:`SMTPChannel` class are methods for handling the following commands
153 (and responding to them appropriately):
154
155 ======== ===================================================================
156 Command Action taken
157 ======== ===================================================================
158 HELO Accepts the greeting from the client and stores it in
159 :attr:`seen_greeting`.
160 NOOP Takes no action.
161 QUIT Closes the connection cleanly.
162 MAIL Accepts the "MAIL FROM:" syntax and stores the supplied address as
163 :attr:`mailfrom`.
164 RCPT Accepts the "RCPT TO:" syntax and stores the supplied addresses in
165 the :attr:`rcpttos` list.
166 RSET Resets the :attr:`mailfrom`, :attr:`rcpttos`, and
167 :attr:`received_data`, but not the greeting.
168 DATA Sets the internal state to :attr:`DATA` and stores remaining lines
169 from the client in :attr:`received_data` until the terminator
Ezio Melotti8bbcb582012-09-20 09:06:51 +0300170 ``"\r\n.\r\n"`` is received.
Raymond Hettinger469271d2011-01-27 20:38:46 +0000171 ======== ===================================================================