EmailBackend
This class provides an email backend implementation that writes email messages to a specified stream, defaulting to standard output. It ensures thread-safe operations using a reentrant lock when processing multiple messages. The class handles message encoding and formatting, appending a separator between each message written to the stream.
Attributes
| Attribute | Type | Description |
|---|---|---|
| stream | file-like object = sys.stdout | The output destination where email messages are written, defaulting to standard output. |
| _lock | threading.RLock | A reentrant threading lock used to ensure thread-safe access to the output stream during message writing. |
| fail_silently | boolean = False | A boolean flag that determines whether exceptions during the email sending process are suppressed or raised. |
Constructor
Signature
def EmailBackend(
fail_silently: boolean = False,
**kwargs: dict
) - > null
Parameters
| Name | Type | Description |
|---|---|---|
| fail_silently | boolean = False | Whether to suppress exceptions during email sending. |
| **kwargs | dict | Additional keyword arguments, including an optional 'stream' for output. |
Methods
write_message()
@classmethod
def write_message(
message: [EmailMessage](../../message/emailmessage.md?sid=django_core_mail_message_emailmessage)
) - > null
Formats a single email message as a string and writes it to the configured output stream followed by a separator line.
Parameters
| Name | Type | Description |
|---|---|---|
| message | [EmailMessage](../../message/emailmessage.md?sid=django_core_mail_message_emailmessage) | The email message instance to be serialized and written to the stream |
Returns
| Type | Description |
|---|---|
null | None |
send_messages()
@classmethod
def send_messages(
email_messages: list
) - > int
Write all messages to the stream in a thread-safe way.
Parameters
| Name | Type | Description |
|---|---|---|
| email_messages | list | A collection of EmailMessage instances to be dispatched to the output stream |
Returns
| Type | Description |
|---|---|
int | The total number of email messages successfully processed and written to the stream |