Skip to main content

EmailBackend

A wrapper that manages the SMTP network connection.

Attributes

AttributeTypeDescription
fail_silentlyboolean = FalseDetermines whether to suppress exceptions during connection and message sending or raise them to the caller.
connection`smtplib.SMTPsmtplib.SMTP_SSL
hoststringThe hostname or IP address of the SMTP server used for outgoing mail.
portintegerThe port number on which the SMTP server is listening, defaulting to 465 for SSL, 587 for TLS, or 25 otherwise.
usernamestringThe username used to authenticate with the SMTP server if required.
passwordstringThe password used to authenticate with the SMTP server if required.
use_tlsboolean = FalseWhether to use a TLS (STARTTLS) connection for security; must not be enabled simultaneously with use_ssl.
use_sslboolean = FalseWhether to use an implicit SSL/TLS connection for security; must not be enabled simultaneously with use_tls.
timeout`integerfloat`
ssl_keyfilestringThe file path to a PEM-formatted private key file for the SSL connection.
ssl_certfilestringThe file path to a PEM-formatted certificate chain file for the SSL connection.

Constructor

Signature

def EmailBackend(
host: string = None,
port: int = None,
username: string = None,
password: string = None,
use_tls: boolean = None,
fail_silently: boolean = False,
use_ssl: boolean = None,
timeout: int = None,
ssl_keyfile: string = None,
ssl_certfile: string = None,
**kwargs: dict
) - > null

Parameters

NameTypeDescription
hoststring = NoneThe host to use for sending email.
portint = NoneThe port to use for the SMTP server.
usernamestring = NoneThe username to use for the SMTP server.
passwordstring = NoneThe password to use for the SMTP server.
use_tlsboolean = NoneWhether to use a TLS (STARTTLS) connection.
fail_silentlyboolean = FalseWhether to suppress exceptions during email sending.
use_sslboolean = NoneWhether to use an implicit TLS (SSL) connection.
timeoutint = NoneThe timeout in seconds for connection attempts.
ssl_keyfilestring = NonePath to a PEM-formatted file that contains your private key for the SSL connection.
ssl_certfilestring = NonePath to a PEM-formatted certificate chain file for the SSL connection.
**kwargsdictAdditional keyword arguments passed to the base class.

Methods


connection_class()

@classmethod
def connection_class() - > type

Determines the appropriate SMTP class to use based on whether SSL is enabled.

Returns

TypeDescription
typeThe smtplib.SMTP_SSL class if use_ssl is True, otherwise the smtplib.SMTP class

ssl_context()

@classmethod
def ssl_context() - > ssl.SSLContext

Creates or retrieves the SSL context used for secure connections, loading custom certificates if provided.

Returns

TypeDescription
ssl.SSLContextA configured SSL context for secure socket communication

open()

@classmethod
def open() - > boolean

Ensure an open connection to the email server. Return whether or not a new connection was required (True or False) or None if an exception passed silently.

Returns

TypeDescription
booleanTrue if a new connection was established, False if already open, or None if an error occurred while fail_silently is enabled

close()

@classmethod
def close() - > null

Close the connection to the email server.

Returns

TypeDescription
null

send_messages()

@classmethod
def send_messages(
email_messages: list
) - > integer

Send one or more EmailMessage objects and return the number of email messages sent.

Parameters

NameTypeDescription
email_messageslistA collection of EmailMessage instances to be transmitted

Returns

TypeDescription
integerThe total count of email messages successfully dispatched to the SMTP server

prep_address()

@classmethod
def prep_address(
address: string,
force_ascii: boolean = True
) - > string

Return the addr-spec portion of an email address. Raises ValueError for invalid addresses, including CR/NL injection.

Parameters

NameTypeDescription
addressstringThe raw email address string to be validated and formatted
force_asciiboolean = TrueWhether to enforce ASCII-only local parts and apply IDNA encoding to domains

Returns

TypeDescription
stringThe sanitized and validated email address string, optionally IDNA encoded