Source code: Lib/smtpd.py
This module offers several classes to implement SMTP (email) servers.
Several server implementations are present; one is a generic do-nothing implementation, which can be overridden, while the other two offer specific mail-sending strategies.
Additionally the SMTPChannel may be extended to implement very specific interaction behaviour with SMTP clients.
Create a new SMTPServer object, which binds to local address localaddr. It will treat remoteaddr as an upstream SMTP relayer. It inherits from asyncore.dispatcher, and so will insert itself into asyncore‘s event loop on instantiation.
data_size_limit specifies the maximum number of bytes that will be accepted in a DATA command. A value of None or 0 means no limit.
Raise NotImplementedError exception. Override this in subclasses to do something useful with this message. Whatever was passed in the constructor as remoteaddr will be available as the _remoteaddr attribute. peer is the remote host’s address, mailfrom is the envelope originator, rcpttos are the envelope recipients and data is a string containing the contents of the e-mail (which should be in RFC 2822 format).
Create a new pure proxy server. Arguments are as per SMTPServer. Everything will be relayed to remoteaddr, unless local mailman configurations knows about an address, in which case it will be handled via mailman. Note that running this has a good chance to make you into an open relay, so please be careful.
Create a new SMTPChannel object which manages the communication between the server and a single SMTP client.
The SMTPChannel has the following instance variables:
Holds the socket object connecting to the client.
Holds a list of the line strings (decoded using UTF-8) received from the client. The lines have their "\r\n" line ending translated to "\n".
Holds the current state of the channel. This will be either COMMAND initially and then DATA after the client sends a “DATA” line.
Holds a string containing the greeting sent by the client in its “HELO”.
Holds a string containing the address identified in the “MAIL FROM:” line from the client.
Holds a list of strings containing the addresses identified in the “RCPT TO:” lines from the client.
Holds a string containing all of the data sent by the client during the DATA state, up to but not including the terminating "\r\n.\r\n".
The SMTPChannel operates by invoking methods named smtp_<command> upon reception of a command line from the client. Built into the base SMTPChannel class are methods for handling the following commands (and responding to them appropriately):
|HELO||Accepts the greeting from the client and stores it in seen_greeting. Sets server to base command mode.|
|EHLO||Accepts the greeting from the client and stores it in seen_greeting. Sets server to extended command mode.|
|NOOP||Takes no action.|
|QUIT||Closes the connection cleanly.|
|Accepts the “MAIL FROM:” syntax and stores the supplied address as mailfrom. In extended command mode, accepts the RFC 1870 SIZE attribute and responds appropriately based on the value of data_size_limit.|
|RCPT||Accepts the “RCPT TO:” syntax and stores the supplied addresses in the rcpttos list.|
|RSET||Resets the mailfrom, rcpttos, and received_data, but not the greeting.|
|DATA||Sets the internal state to DATA and stores remaining lines from the client in received_data until the terminator "\r\n.\r\n" is received.|
|HELP||Returns minimal information on command syntax|
|VRFY||Returns code 252 (the server doesn’t know if the address is valid)|
|EXPN||Reports that the command is not implemented.|