There are two general ways you can run the SMTP server, via the 命令行 or programmatically .
There are several dimensions in which you can extend the basic functionality of the SMTP server. You can implement an event handler which uses well defined handler hooks that are called during the various steps in the SMTP dialog. If such a hook is implemented, it assumes responsibility for the status messages returned to the client.
You can also
subclass
the core
SMTP
class to implement new commands, or change the semantics of existing commands.
For example, if you wanted to print the received message on the console, you could implement a handler that hooks into the
DATA
command. The contents of the message will be available on one of the hook’s arguments, and your handler could print this content to stdout.
On the other hand, if you wanted to implement an SMTP-like server that adds a new command called
PING
, you would do this by subclassing
SMTP
, adding a method that implements whatever semantics for
PING
that you want.
Two classes are used during the SMTP dialog with clients. Instances of these are passed to the handler hooks.
注意
Handler Hooks MAY add new attributes to these classes for inter-hook coordination.
The session represents the state built up during a client’s socket connection to the server. Each time a client connects to the server, a new session object is created.
Session
(
loop
)
¶
loop
– asyncio event loop currently running
SMTP
.
ssl
¶
Defaulting to None, this attribute will contain some extra information, as a dictionary, from the
asyncio.sslproto.SSLProtocol
instance. This dictionary provides additional information about the connection. It contains implementation-specific information so its contents may change, but it should roughly correspond to the information available through
asyncio.BaseTransport.get_extra_info()
host_name
¶
Defaulting to None, this attribute will contain the host name argument as seen in the
HELO
or
EHLO
(or for
LMTP
,
LHLO
) command.
extended_smtp
¶
Defaulting to False, this flag will be True when the
EHLO
greeting was seen, indicating
ESMTP
.
loop
¶
This is the asyncio event loop instance.
处理程序挂钩
can utilize this if needed, for instance invoking
call_later()
to set some timers.
login_data
¶
Contains the login information gathered during the
AUTH
procedure. If it contains
None
, that means authentication has not taken place or has failed.
警告
This is the “legacy” login_data, populated only if
auth_callback
parameter is set.
Deprecated since version 1.3: This attribute will be removed in version 2.0 .
auth_data
¶
Contains the authentication data returned by the
authenticator
callback.
authenticated
: Optional
[
bool
]
¶
A tri-state flag indicating status of authentication:
None
:= Authentication has not been performed
False
:= Authentication has been performed, but failed
True
:= Authentication has been performed, and succeeded
The envelope represents state built up during the client’s SMTP dialog. Each time the protocol state is reset, a new envelope is created. E.g. when the SMTP
RSET
command is sent, the state is reset and a new envelope is created. A new envelope is also created after the
DATA
command is completed, or in certain error conditions as mandated by
RFC 5321
.
Envelope
¶
mail_from
:
str
¶
Defaulting to None, this attribute holds the email address given in the
MAIL FROM
命令。
mail_options
: List
[
str
]
¶
Defaulting to None, this attribute contains a list of any ESMTP mail options provided by the client, such as those passed in by
smtplib.SMTP.sendmail()
content
: AnyStr
¶
Defaulting to None, this attribute will contain the contents of the message as provided by the
DATA
command. If the
decode_data
参数用于
SMTP
constructor was True, then this attribute will contain the UTF-8 decoded string, otherwise it will contain the raw bytes.
original_content
:
bytes
¶
Defaulting to None, this attribute will contain the contents of the message as provided by the
DATA
command. Unlike the
content
attribute, this attribute will always contain the raw bytes.
rcpt_tos
: List
[
str
]
¶
Defaulting to the empty list, this attribute will contain a list of the email addresses provided in the
RCPT TO
命令。
rcpt_options
: List
[
str
]
¶
Defaulting to the empty list, this attribute will contain the list of any recipient options provided by the client, such as those passed in by
smtplib.SMTP.sendmail()