blob: c391f710dfa35f74fe901b5d6e59de6e268505a0 [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
107 socket.accept()
108
109 .. attribute:: received_lines
110
111 Holds a list of the line strings (decoded using UTF-8) received from
112 the client. The lines have their "\r\n" line ending translated to "\n".
113
114 .. attribute:: smtp_state
115
116 Holds the current state of the channel. This will be either
117 :attr:`COMMAND` initially and then :attr:`DATA` after the client sends
118 a "DATA" line.
119
120 .. attribute:: seen_greeting
121
122 Holds a string containing the greeting sent by the client in its "HELO".
123
124 .. attribute:: mailfrom
125
126 Holds a string containing the address identified in the "MAIL FROM:" line
127 from the client.
128
129 .. attribute:: rcpttos
130
131 Holds a list of strings containing the addresses identified in the
132 "RCPT TO:" lines from the client.
133
134 .. attribute:: received_data
135
136 Holds a string containing all of the data sent by the client during the
137 DATA state, up to but not including the terminating "\r\n.\r\n".
138
139 .. attribute:: fqdn
140
141 Holds the fully-qualified domain name of the server as returned by
142 ``socket.getfqdn()``.
143
144 .. attribute:: peer
145
146 Holds the name of the client peer as returned by ``conn.getpeername()``
147 where ``conn`` is :attr:`conn`.
148
149 The :class:`SMTPChannel` operates by invoking methods named ``smtp_<command>``
150 upon reception of a command line from the client. Built into the base
151 :class:`SMTPChannel` class are methods for handling the following commands
152 (and responding to them appropriately):
153
154 ======== ===================================================================
155 Command Action taken
156 ======== ===================================================================
157 HELO Accepts the greeting from the client and stores it in
158 :attr:`seen_greeting`.
159 NOOP Takes no action.
160 QUIT Closes the connection cleanly.
161 MAIL Accepts the "MAIL FROM:" syntax and stores the supplied address as
162 :attr:`mailfrom`.
163 RCPT Accepts the "RCPT TO:" syntax and stores the supplied addresses in
164 the :attr:`rcpttos` list.
165 RSET Resets the :attr:`mailfrom`, :attr:`rcpttos`, and
166 :attr:`received_data`, but not the greeting.
167 DATA Sets the internal state to :attr:`DATA` and stores remaining lines
168 from the client in :attr:`received_data` until the terminator
169 "\r\n.\r\n" is received.
Raymond Hettinger469271d2011-01-27 20:38:46 +0000170 ======== ===================================================================