API Reference
Protocol
- class smtpproto.protocol.ClientState(value)
Bases:
Enum
Enumerates all possible protocol states.
- greeting_expected = 1
expecting a greeting from the server
- greeting_received = 2
received a greeting from the server, ready to authenticate
- authenticating = 3
authentication in progress
- authenticated = 4
authentication done
- ready = 5
ready to send commands
- mailtx = 6
in a mail transaction
- recipient_sent = 7
sent at least one recipient
- send_data = 8
ready to send the message data
- data_sent = 9
message data sent
- finished = 10
session finished
- exception smtpproto.protocol.SMTPMissingExtension
Bases:
SMTPException
Raised when a required SMTP extension is not present on the server.
- exception smtpproto.protocol.SMTPUnsupportedAuthMechanism
Bases:
SMTPException
Raised when trying to authenticate using a mechanism not supported by the server.
- exception smtpproto.protocol.SMTPProtocolViolation
Bases:
SMTPException
Raised when there has been a violation of the (E)SMTP protocol by either side.
- class smtpproto.protocol.SMTPResponse(code, message)
Bases:
object
Represents a response from the server.
- raise_as_exception()
Raise an
SMTPException
from this response.- Return type:
- class smtpproto.protocol.SMTPClientProtocol
Bases:
object
The (E)SMTP protocol state machine.
- property state: ClientState
The current state of the protocol.
- property max_message_size: int | None
The maximum size of the email message (in bytes) accepted by the server.
- property auth_mechanisms: frozenset[str]
The set of authentication mechanisms supported on the server.
- authenticate(mechanism, secret=None)
Authenticate to the server using the given mechanism and an accompanying secret.
- Parameters:
mechanism (str) – the authentication mechanism (e.g.
PLAIN
orGSSAPI
)secret (str | None) – an optional string (usually containing the credentials) that is added as an argument to the
AUTH XXX
command
- Return type:
None
- send_authentication_data(data)
Send authentication data to the server.
This method can be called when the server responds with a 334 to an AUTH command.
- send_greeting(domain)
Send the initial greeting (EHLO or HELO).
- mail(sender, *, smtputf8=True)
Send the MAIL FROM command (starts a mail transaction).
- Parameters:
sender (str | Address) – the sender’s email address
smtputf8 (bool) – send the SMTPUTF8 option, if available on the server
- Return type:
None
- recipient(recipient)
Send the RCPT TO command (declare an intended recipient).
Requires an active mail transaction.
- Parameters:
recipient (str | Address) – the recipient’s email address
- Return type:
None
- start_data()
Send the DATA command (prepare for sending the email payload).
Requires an active mail transaction, and that at least one recipient has been declared.
- Return type:
- data(message)
Send the actual email payload.
Requires that the DATA command has been sent first.
- Parameters:
message (
EmailMessage
) – the email message- Return type:
- start_tls()
Send the STARTTLS command (signal the server to initiate a TLS handshake).
- Return type:
- feed_bytes(data)
Feed received bytes from the transport into the state machine.
if this method raises
SMTPProtocolViolation
, the state machine is transitioned to thefinished
state, and the connection should be closed.- Parameters:
data (bytes) – received bytes
- Return type:
SMTPResponse | None
- Returns:
a response object if a complete response was received,
None
otherwise- Raises:
SMTPProtocolViolation – if the server sent an invalid response
Authentication
- class smtpproto.auth.LoginAuthenticator(username, password)
Bases:
SMTPAuthenticator
Authenticates against the server with a username/password combination using the LOGIN method.
- Parameters:
- async authenticate()
Performs authentication against the SMTP server.
This method must return an async generator. Any non-empty values the generator yields are sent to the server as authentication data. The response messages from any 334 responses are sent to the generator.
- class smtpproto.auth.OAuth2Authenticator(username, *, grace_period=600)
Bases:
SMTPAuthenticator
Authenticates against the server using OAUTH2.
In order to use this authenticator, you must subclass it and implement the
get_token()
method.- Parameters:
- async authenticate()
Performs authentication against the SMTP server.
This method must return an async generator. Any non-empty values the generator yields are sent to the server as authentication data. The response messages from any 334 responses are sent to the generator.
- abstract async get_token()
Obtain a new access token.
This method will be called only when there either is no cached token, or the cached token is expired or nearing expiration. You can also use
clear_cached_token()
to manually erase the cached token. Theexpires_in
field in the returned dict is the number of seconds after which the token will expire.Note
If the backing server does not provide a value for
expires_in
, the implementor must fill in the value by other means.- Return type:
- Returns:
a dict containing the
access_token
andexpires_in
fields
- class smtpproto.auth.PlainAuthenticator(username, password, authorization_id='')
Bases:
SMTPAuthenticator
Authenticates against the server with a username/password combination using the PLAIN method.
- Parameters:
- async authenticate()
Performs authentication against the SMTP server.
This method must return an async generator. Any non-empty values the generator yields are sent to the server as authentication data. The response messages from any 334 responses are sent to the generator.
- class smtpproto.auth.SMTPAuthenticator
Bases:
object
Interface for providing credentials for authenticating against SMTP servers.
- abstract authenticate()
Performs authentication against the SMTP server.
This method must return an async generator. Any non-empty values the generator yields are sent to the server as authentication data. The response messages from any 334 responses are sent to the generator.
Concrete client implementation
- class smtpproto.client.AsyncSMTPClient(host, port=587, connect_timeout=30, timeout=60, domain=None, ssl_context=None, authenticator=None, max_concurrent_connections=50)
Bases:
object
An asynchronous SMTP client.
This runs on asyncio or any other backend supported by AnyIO.
- Parameters:
host (str) – host name or IP address of the SMTP server
port (int) – port on the SMTP server to connect to
connect_timeout (float) – connection timeout (in seconds)
timeout (float) – timeout for sending requests and reading responses (in seconds)
domain (str | None) – domain name to send to the server as part of the greeting message
ssl_context (SSLContext | None) – SSL context to use for establishing TLS encrypted sessions
authenticator (SMTPAuthenticator | None) – authenticator to use for authenticating with the SMTP server
max_concurrent_connections (int) – maximum number of connections to allows to the SMTP server before blocking
- connect()
Establish a session with the SMTP server.
The returned async context manager connects to the SMTP server and performs the protocol handshake. After that, it optionally establishes an encrypted session with
STARTTLS
, and then logs in (if an authenticator was provided).- Returns:
a context manager yielding an
AsyncSMTPSession
- async send_message(message, *, sender=None, recipients=None)
Open a session with the SMTP server, send an email and then close the session.
This is a convenience method for the following:
async with client.connect() as session: return await session.send_message(message, sender=sender, recipients=recipients)
- Parameters:
message (EmailMessage) – the message to send
sender (str | Address | None) – override the sender address in the
MAIL FROM
commandrecipients (Iterable[str] | None) – override the destination addresses in the
RCPT TO
commands
- Return type:
SMTPResponse
- Returns:
the SMTP response
- class smtpproto.client.AsyncSMTPSession(host, port, connect_timeout, timeout, domain, authenticator, ssl_context)
Bases:
object
Encapsulates a live connection to an SMTP server.
- Variables:
protocol (SMTPClientProtocol) – the protocol state machine
- async send_command(command, /, *args, **kwargs)
Send a command to the SMTP server and return the response.
- Parameters:
command – a callable from
SMTPClientProtocol
args – positional arguments to
command
kwargs – keyword arguments to
command
- async send_message(message, *, sender=None, recipients=None)
Send an email message.
- Parameters:
message (EmailMessage) – the message to send
sender (str | Address | None) – override the sender address in the
MAIL FROM
commandrecipients (Iterable[str] | None) – override the destination addresses in the
RCPT TO
commands
- Return type:
SMTPResponse
- Returns:
the SMTP response
- class smtpproto.client.SyncSMTPClient(host, port=587, connect_timeout=30, timeout=60, domain=None, ssl_context=None, authenticator=None, max_concurrent_connections=50, async_backend='asyncio', async_backend_options=None)
Bases:
object
A synchronous (blocking) SMTP client.
- Parameters:
host (str) – host name or IP address of the SMTP server
port (int) – port on the SMTP server to connect to
connect_timeout (float) – connection timeout (in seconds)
timeout (float) – timeout for sending requests and reading responses (in seconds)
domain (str | None) – domain name to send to the server as part of the greeting message
ssl_context (SSLContext | None) – SSL context to use for establishing TLS encrypted sessions
authenticator (SMTPAuthenticator | None) – authenticator to use for authenticating with the SMTP server
max_concurrent_connections (int) – maximum number of connections to allows to the SMTP server before blocking
async_backend (str) – name of the AnyIO-supported asynchronous backend
async_backend_options (dict[str, Any] | None) – dictionary of keyword arguments passed to
anyio.from_thread.start_blocking_portal()
- connect()
Establish a session with the SMTP server.
The returned context manager connects to the SMTP server and performs the protocol handshake. After that, it optionally establishes an encrypted session with
STARTTLS
, and then logs in (if an authenticator was provided).- Returns:
a context manager yielding a
SyncSMTPSession
- send_message(message, *, sender=None, recipients=None)
Open a session with the SMTP server, send an email and then close the session.
This is a convenience method for the following:
with client.connect() as session: return session.send_message(message, sender=sender, recipients=recipients)
- Parameters:
message (EmailMessage) – the message to send
sender (str | Address | None) – override the sender address in the
MAIL FROM
commandrecipients (Iterable[str] | None) – override the destination addresses in the
RCPT TO
commands
- Return type:
SMTPResponse
- Returns:
the SMTP response
- class smtpproto.client.SyncSMTPSession(portal, async_session)
Bases:
object
- send_command(command, /, *args, **kwargs)
Send a command to the SMTP server and return the response.
- Parameters:
command – a callable from
SMTPClientProtocol
args – positional arguments to
command
kwargs – keyword arguments to
command
- send_message(message, *, sender=None, recipients=None)
Send an email message.
- Parameters:
message (EmailMessage) – the message to send
sender (str | Address | None) – override the sender address in the
MAIL FROM
commandrecipients (Iterable[str] | None) – override the destination addresses in the
RCPT TO
commands
- Return type:
SMTPResponse
- Returns:
the SMTP response