Skip to main content

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

AttributeTypeDescription
_fnamestring = nullInternal cache for the absolute file path of the current log file to ensure consistent writing during a session.
file_pathstring = nullAbsolute 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

NameTypeDescription
fail_silentlyboolean = FalseWhether to suppress exceptions during email sending.
file_pathstring = NoneThe directory path where email files will be saved.
**kwargsdictAdditional 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

NameTypeDescription
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

TypeDescription
null

open()

@classmethod
def open() - > boolean

Opens the output file in append-binary mode if it is not already open.

Returns

TypeDescription
booleanReturns 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

TypeDescription
null