Plain is headed towards 1.0! Subscribe for development updates →

 1"""Base email backend class."""
 2
 3
 4class BaseEmailBackend:
 5    """
 6    Base class for email backend implementations.
 7
 8    Subclasses must at least overwrite send_messages().
 9
10    open() and close() can be called indirectly by using a backend object as a
11    context manager:
12
13       with backend as connection:
14           # do something with connection
15           pass
16    """
17
18    def __init__(self, fail_silently=False, **kwargs):
19        self.fail_silently = fail_silently
20
21    def open(self):
22        """
23        Open a network connection.
24
25        This method can be overwritten by backend implementations to
26        open a network connection.
27
28        It's up to the backend implementation to track the status of
29        a network connection if it's needed by the backend.
30
31        This method can be called by applications to force a single
32        network connection to be used when sending mails. See the
33        send_messages() method of the SMTP backend for a reference
34        implementation.
35
36        The default implementation does nothing.
37        """
38        pass
39
40    def close(self):
41        """Close a network connection."""
42        pass
43
44    def __enter__(self):
45        try:
46            self.open()
47        except Exception:
48            self.close()
49            raise
50        return self
51
52    def __exit__(self, exc_type, exc_value, traceback):
53        self.close()
54
55    def send_messages(self, email_messages):
56        """
57        Send one or more EmailMessage objects and return the number of email
58        messages sent.
59        """
60        raise NotImplementedError(
61            "subclasses of BaseEmailBackend must override send_messages() method"
62        )