1"""Base email backend class.""" 2 3from__future__importannotations 4 5fromtypingimportTYPE_CHECKING,Any 6 7ifTYPE_CHECKING: 8fromtypesimportTracebackType 910from..messageimportEmailMessage111213classBaseEmailBackend:14"""15 Base class for email backend implementations.1617 Subclasses must at least overwrite send_messages().1819 open() and close() can be called indirectly by using a backend object as a20 context manager:2122 with backend as connection:23 # do something with connection24 pass25 """2627def__init__(self,fail_silently:bool=False,**kwargs:Any)->None:28self.fail_silently=fail_silently2930defopen(self)->None:31"""32 Open a network connection.3334 This method can be overwritten by backend implementations to35 open a network connection.3637 It's up to the backend implementation to track the status of38 a network connection if it's needed by the backend.3940 This method can be called by applications to force a single41 network connection to be used when sending mails. See the42 send_messages() method of the SMTP backend for a reference43 implementation.4445 The default implementation does nothing.46 """47pass4849defclose(self)->None:50"""Close a network connection."""51pass5253def__enter__(self)->BaseEmailBackend:54try:55self.open()56exceptException:57self.close()58raise59returnself6061def__exit__(62self,63exc_type:type[BaseException]|None,64exc_value:BaseException|None,65traceback:TracebackType|None,66)->None:67self.close()6869defsend_messages(self,email_messages:list[EmailMessage])->int:70"""71 Send one or more EmailMessage objects and return the number of email72 messages sent.73 """74raiseNotImplementedError(75"subclasses of BaseEmailBackend must override send_messages() method"76)