EmailBackend
A wrapper that manages the SMTP network connection.
Attributes
| Attribute | Type | Description |
|---|---|---|
| fail_silently | boolean = False | Determines whether to suppress exceptions during connection and message sending or raise them to the caller. |
| connection | `smtplib.SMTP | smtplib.SMTP_SSL |
| host | string | The hostname or IP address of the SMTP server used for outgoing mail. |
| port | integer | The port number on which the SMTP server is listening, defaulting to 465 for SSL, 587 for TLS, or 25 otherwise. |
| username | string | The username used to authenticate with the SMTP server if required. |
| password | string | The password used to authenticate with the SMTP server if required. |
| use_tls | boolean = False | Whether to use a TLS (STARTTLS) connection for security; must not be enabled simultaneously with use_ssl. |
| use_ssl | boolean = False | Whether to use an implicit SSL/TLS connection for security; must not be enabled simultaneously with use_tls. |
| timeout | `integer | float` |
| ssl_keyfile | string | The file path to a PEM-formatted private key file for the SSL connection. |
| ssl_certfile | string | The 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
| Name | Type | Description |
|---|---|---|
| host | string = None | The host to use for sending email. |
| port | int = None | The port to use for the SMTP server. |
| username | string = None | The username to use for the SMTP server. |
| password | string = None | The password to use for the SMTP server. |
| use_tls | boolean = None | Whether to use a TLS (STARTTLS) connection. |
| fail_silently | boolean = False | Whether to suppress exceptions during email sending. |
| use_ssl | boolean = None | Whether to use an implicit TLS (SSL) connection. |
| timeout | int = None | The timeout in seconds for connection attempts. |
| ssl_keyfile | string = None | Path to a PEM-formatted file that contains your private key for the SSL connection. |
| ssl_certfile | string = None | Path to a PEM-formatted certificate chain file for the SSL connection. |
| **kwargs | dict | Additional 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
| Type | Description |
|---|---|
type | The 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
| Type | Description |
|---|---|
ssl.SSLContext | A 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
| Type | Description |
|---|---|
boolean | True 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
| Type | Description |
|---|---|
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
| Name | Type | Description |
|---|---|---|
| email_messages | list | A collection of EmailMessage instances to be transmitted |
Returns
| Type | Description |
|---|---|
integer | The 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
| Name | Type | Description |
|---|---|---|
| address | string | The raw email address string to be validated and formatted |
| force_ascii | boolean = True | Whether to enforce ASCII-only local parts and apply IDNA encoding to domains |
Returns
| Type | Description |
|---|---|
string | The sanitized and validated email address string, optionally IDNA encoded |