email backend that writes messages to a file import datetime import os from django conf import settings from django core exceptions import improperlyconfigured from django core mail backends console import emailbackend as consoleemailbackend class emailbackendconsoleemailbackend def initself args filepathnone kwargs self fname none if filepath is not none self filepath filepath else self filepath getattrsettings emailfilepath none self filepath os path abspathself filepath try os makedirsself filepath existoktrue except fileexistserror raise improperlyconfigured path for saving email messages exists but is not a directory s self filepath except oserror as err raise improperlyconfigured could not create directory for saving email messages s s self filepath err make sure that self filepath is writable if not os accessself filepath os wok raise improperlyconfigured could not write to directory s self filepath finally call super since we re using the consolebased backend as a base force the stream to be none so we don t default to stdout kwargsstream none super initargs kwargs def writemessageself message self stream writemessage message asbytes bn self stream writeb 79 self stream writebn def getfilenameself make sure that self file_path is writable finally call super since we re using the console based backend as a base force the stream to be none so we don t default to stdout return a unique file name
import datetime
import os

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.core.mail.backends.console import EmailBackend as ConsoleEmailBackend


class EmailBackend(ConsoleEmailBackend):
    def __init__(self, *args, file_path=None, **kwargs):
        self._fname = None
        if file_path is not None:
            self.file_path = file_path
        else:
            self.file_path = getattr(settings, "EMAIL_FILE_PATH", None)
        self.file_path = os.path.abspath(self.file_path)
        try:
            os.makedirs(self.file_path, exist_ok=True)
        except FileExistsError:
            raise ImproperlyConfigured(
                "Path for saving email messages exists, but is not a directory: %s"
                % self.file_path
            )
        except OSError as err:
            raise ImproperlyConfigured(
                "Could not create directory for saving email messages: %s (%s)"
                % (self.file_path, err)
            )
        
        if not os.access(self.file_path, os.W_OK):
            raise ImproperlyConfigured(
                "Could not write to directory: %s" % self.file_path
            )
        
        
        
        kwargs["stream"] = None
        super().__init__(*args, **kwargs)

    def write_message(self, message):
        self.stream.write(message.message().as_bytes() + b"\n")
        self.stream.write(b"-" * 79)
        self.stream.write(b"\n")

    def _get_filename(self):
        
        if self._fname is None:
            timestamp = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
            fname = "%s-%s.log" % (timestamp, abs(id(self)))
            self._fname = os.path.join(self.file_path, fname)
        return self._fname

    def open(self):
        if self.stream is None:
            self.stream = open(self._get_filename(), "ab")
            return True
        return False

    def close(self):
        try:
            if self.stream is not None:
                self.stream.close()
        finally:
            self.stream = None