smtpd —- SMTP 服务器

源代码:Lib/smtpd.py


This module offers several classes to implement SMTP (email) servers.

参见

The aiosmtpd package is a recommendedreplacement for this module. It is based on asyncio and provides amore straightforward API. smtpd should be considered deprecated.

Several server implementations are present; one is a genericdo-nothing implementation, which can be overridden, while the other two offerspecific mail-sending strategies.

Additionally the SMTPChannel may be extended to implement very specificinteraction behaviour with SMTP clients.

The code supports RFC 5321, plus the RFC 1870 SIZE and RFC 6531SMTPUTF8 extensions.

SMTPServer 对象

  • class smtpd.SMTPServer(localaddr, remoteaddr, data_size_limit=33554432, map=None, enable_SMTPUTF8=False, decode_data=False)
  • Create a new SMTPServer object, which binds to local addresslocaladdr. It will treat remoteaddr as an upstream SMTP relayer. Bothlocaladdr and remoteaddr should be a (host, port)tuple. The object inherits from asyncore.dispatcher, and so willinsert itself into asyncore's event loop on instantiation.

data_size_limit specifies the maximum number of bytes that will beaccepted in a DATA command. A value of None or 0 means nolimit.

map is the socket map to use for connections (an initially emptydictionary is a suitable value). If not specified the asyncoreglobal socket map is used.

enable_SMTPUTF8 determines whether the SMTPUTF8 extension (as definedin RFC 6531) should be enabled. The default is False.When True, SMTPUTF8 is accepted as a parameter to the MAILcommand and when present is passed to process_message() in thekwargs['mailoptions'] list. _decode_data and _enable_SMTPUTF8_cannot be set to True at the same time.

decode_data specifies whether the data portion of the SMTP transactionshould be decoded using UTF-8. When decode_data is False (thedefault), the server advertises the 8BITMIMEextension (RFC 6152), accepts the BODY=8BITMIME parameter tothe MAIL command, and when present passes it to process_message()in the kwargs['mailoptions'] list. _decode_data and _enable_SMTPUTF8_cannot be set to True at the same time.

  • processmessage(_peer, mailfrom, rcpttos, data, **kwargs)
  • Raise a NotImplementedError exception. Override this in subclasses todo something useful with this message. Whatever was passed in theconstructor as remoteaddr will be available as the remoteaddrattribute. _peer is the remote host's address, mailfrom is the envelopeoriginator, rcpttos are the envelope recipients and data is a stringcontaining the contents of the e-mail (which should be in RFC 5321format).

If the decode_data constructor keyword is set to True, the _data_argument will be a unicode string. If it is set to False, itwill be a bytes object.

kwargs is a dictionary containing additional information. It is emptyif decode_data=True was given as an init argument, otherwiseit contains the following keys:

mail_options:

a list of all received parameters to the MAILcommand (the elements are uppercase strings; example:['BODY=8BITMIME', 'SMTPUTF8']).

rcpt_options:

same as mail_options but for the RCPT command.Currently no RCPT TO options are supported, so for nowthis will always be an empty list.

Implementations of process_message should use the **kwargssignature to accept arbitrary keyword arguments, since future featureenhancements may add keys to the kwargs dictionary.

Return None to request a normal 250 Ok response; otherwisereturn the desired response string in RFC 5321 format.

  • channel_class
  • Override this in subclasses to use a custom SMTPChannel formanaging SMTP clients.

3.4 新版功能: The map constructor argument.

在 3.5 版更改: localaddr and remoteaddr may now contain IPv6 addresses.

3.5 新版功能: The decode_data and enable_SMTPUTF8 constructor parameters, and thekwargs parameter to process_message() when decode_data isFalse.

在 3.6 版更改: decode_data is now False by default.

DebuggingServer 对象

  • class smtpd.DebuggingServer(localaddr, remoteaddr)
  • Create a new debugging server. Arguments are as per SMTPServer.Messages will be discarded, and printed on stdout.

PureProxy对象

  • class smtpd.PureProxy(localaddr, remoteaddr)
  • Create a new pure proxy server. Arguments are as per SMTPServer.Everything will be relayed to remoteaddr. Note that running this has a goodchance to make you into an open relay, so please be careful.

MailmanProxy 对象

  • class smtpd.MailmanProxy(localaddr, remoteaddr)
  • Create a new pure proxy server. Arguments are as per SMTPServer.Everything will be relayed to remoteaddr, unless local mailman configurationsknows about an address, in which case it will be handled via mailman. Note thatrunning this has a good chance to make you into an open relay, so please becareful.

SMTPChannel 对象

  • class smtpd.SMTPChannel(server, conn, addr, data_size_limit=33554432, map=None, enable_SMTPUTF8=False, decode_data=False)
  • Create a new SMTPChannel object which manages the communicationbetween the server and a single SMTP client.

conn and addr are as per the instance variables described below.

data_size_limit specifies the maximum number of bytes that will beaccepted in a DATA command. A value of None or 0 means nolimit.

enable_SMTPUTF8 determines whether the SMTPUTF8 extension (as definedin RFC 6531) should be enabled. The default is False.decode_data and enable_SMTPUTF8 cannot be set to True at the sametime.

A dictionary can be specified in map to avoid using a global socket map.

decode_data specifies whether the data portion of the SMTP transactionshould be decoded using UTF-8. The default is False.decode_data and enable_SMTPUTF8 cannot be set to True at the sametime.

To use a custom SMTPChannel implementation you need to override theSMTPServer.channel_class of your SMTPServer.

在 3.5 版更改: The decode_data and enable_SMTPUTF8 parameters were added.

在 3.6 版更改: decode_data is now False by default.

The SMTPChannel has the following instance variables:

  • smtp_server
  • Holds the SMTPServer that spawned this channel.

  • conn

  • Holds the socket object connecting to the client.

  • addr

  • Holds the address of the client, the second value returned bysocket.accept

  • received_lines

  • Holds a list of the line strings (decoded using UTF-8) received fromthe client. The lines have their "\r\n" line ending translated to"\n".

  • smtp_state

  • Holds the current state of the channel. This will be eitherCOMMAND initially and then DATA after the client sendsa "DATA" line.

  • seen_greeting

  • Holds a string containing the greeting sent by the client in its "HELO".

  • mailfrom

  • Holds a string containing the address identified in the "MAIL FROM:" linefrom the client.

  • rcpttos

  • Holds a list of strings containing the addresses identified in the"RCPT TO:" lines from the client.

  • received_data

  • Holds a string containing all of the data sent by the client during theDATA state, up to but not including the terminating "\r\n.\r\n".

  • fqdn

  • Holds the fully-qualified domain name of the server as returned bysocket.getfqdn().

  • peer

  • Holds the name of the client peer as returned by conn.getpeername()where conn is conn.

The SMTPChannel operates by invoking methods named smtp_<command>upon reception of a command line from the client. Built into the baseSMTPChannel class are methods for handling the following commands(and responding to them appropriately):

命令

所采取的行动

HELO

接受来自客户端的问候语,并将其存储在 seen_greeting 中。将服务器设置为基本命令模式。

EHLO

接受来自客户的问候并将其存储在 seen_greeting 中。将服务器设置为扩展命令模式。

NOOP

不采取任何措施。

QUIT

干净地关闭连接。

MAIL

Accepts the "MAIL FROM:" syntax and stores the supplied address asmailfrom. In extended command mode, accepts theRFC 1870 SIZE attribute and responds appropriately based on thevalue of data_size_limit.

RCPT

Accepts the "RCPT TO:" syntax and stores the supplied addresses inthe rcpttos list.

RSET

重置 mailfrom, rcpttos, 和 received_data ,但不重置问候语。

DATA

Sets the internal state to DATA and stores remaining linesfrom the client in received_data until the terminator"\r\n.\r\n" is received.

HELP

返回有关命令语法的最少信息

VRFY

返回代码252(服务器不知道该地址是否有效)

EXPN

报告该命令未实现。