EmailBackend
This class implements an email backend that writes email messages to a file on the local filesystem. It ensures the specified storage directory exists and is writable, generating unique filenames for each session to prevent data loss. The backend extends console-based functionality to redirect output from standard streams to persistent file storage.
Attributes
| Attribute | Type | Description |
|---|---|---|
| _fname | string = null | Internal cache for the absolute file path of the current log file to ensure consistent writing during a session. |
| file_path | string = null | Absolute path to the directory where email message files will be saved. |
Constructor
Signature
def EmailBackend(
fail_silently: boolean = False,
file_path: string = None,
**kwargs: dict
)
Parameters
| Name | Type | Description |
|---|---|---|
| fail_silently | boolean = False | Whether to suppress exceptions during email sending. |
| file_path | string = None | The directory path where email files will be saved. |
| **kwargs | dict | Additional keyword arguments passed to the parent class. |
Methods
write_message()
@classmethod
def write_message(
message: [EmailMessage](../../message/emailmessage.md?sid=django_core_mail_message_emailmessage)
) - > null
Writes the raw byte content of an email message to the current file stream, followed by a separator line.
Parameters
| Name | Type | Description |
|---|---|---|
| message | [EmailMessage](../../message/emailmessage.md?sid=django_core_mail_message_emailmessage) | The email message object whose content will be serialized and written to the file. |
Returns
| Type | Description |
|---|---|
null |
open()
@classmethod
def open() - > boolean
Opens the output file in append-binary mode if it is not already open.
Returns
| Type | Description |
|---|---|
boolean | Returns True if a new file stream was successfully opened, or False if the stream was already active. |
close()
@classmethod
def close() - > null
Closes the current file stream and resets the stream attribute to None.
Returns
| Type | Description |
|---|---|
null |