11# encoding: utf-8
2- import smtplib
2+ from __future__ import annotations
3+
34import logging
5+ import smtplib
6+ from collections .abc import Callable
47from functools import wraps
8+ from types import TracebackType
9+ from typing import Any
10+
511from ..response import SMTPResponse
612from .client import SMTPClientWithResponse , SMTPClientWithResponse_SSL
713from ...utils import DNS_NAME
1319logger = logging .getLogger (__name__ )
1420
1521
16- class SMTPBackend ( object ) :
22+ class SMTPBackend :
1723
1824 """
1925 SMTPBackend manages a smtp connection.
@@ -25,7 +31,8 @@ class SMTPBackend(object):
2531 connection_ssl_cls = SMTPClientWithResponse_SSL
2632 response_cls = SMTPResponse
2733
28- def __init__ (self , ssl = False , fail_silently = True , mail_options = None , ** kwargs ):
34+ def __init__ (self , ssl : bool = False , fail_silently : bool = True ,
35+ mail_options : list [str ] | None = None , ** kwargs : Any ) -> None :
2936
3037 self .smtp_cls = ssl and self .connection_ssl_cls or self .connection_cls
3138
@@ -42,19 +49,19 @@ def __init__(self, ssl=False, fail_silently=True, mail_options=None, **kwargs):
4249
4350 self .smtp_cls_kwargs = kwargs
4451
45- self .host = kwargs .get ('host' )
46- self .port = kwargs .get ('port' )
52+ self .host : str | None = kwargs .get ('host' )
53+ self .port : int = kwargs .get ('port' )
4754 self .fail_silently = fail_silently
4855 self .mail_options = mail_options or []
4956
50- self ._client = None
57+ self ._client : SMTPClientWithResponse | None = None
5158
52- def get_client (self ):
59+ def get_client (self ) -> SMTPClientWithResponse :
5360 if self ._client is None :
5461 self ._client = self .smtp_cls (parent = self , ** self .smtp_cls_kwargs )
5562 return self ._client
5663
57- def close (self ):
64+ def close (self ) -> None :
5865
5966 """
6067 Closes the connection to the email server.
@@ -70,12 +77,12 @@ def close(self):
7077 finally :
7178 self ._client = None
7279
73- def make_response (self , exception = None ):
80+ def make_response (self , exception : Exception | None = None ) -> SMTPResponse :
7481 return self .response_cls (backend = self , exception = exception )
7582
76- def retry_on_disconnect (self , func ) :
83+ def retry_on_disconnect (self , func : Callable [..., SMTPResponse ]) -> Callable [..., SMTPResponse ] :
7784 @wraps (func )
78- def wrapper (* args , ** kwargs ) :
85+ def wrapper (* args : Any , ** kwargs : Any ) -> SMTPResponse :
7986 try :
8087 return func (* args , ** kwargs )
8188 except smtplib .SMTPServerDisconnected :
@@ -85,7 +92,7 @@ def wrapper(*args, **kwargs):
8592 return func (* args , ** kwargs )
8693 return wrapper
8794
88- def _send (self , ** kwargs ) :
95+ def _send (self , ** kwargs : Any ) -> SMTPResponse :
8996
9097 response = None
9198 try :
@@ -106,7 +113,9 @@ def _send(self, **kwargs):
106113 else :
107114 return client .sendmail (** kwargs )
108115
109- def sendmail (self , from_addr , to_addrs , msg , mail_options = None , rcpt_options = None ):
116+ def sendmail (self , from_addr : str , to_addrs : str | list [str ],
117+ msg : Any , mail_options : list [str ] | None = None ,
118+ rcpt_options : list [str ] | None = None ) -> SMTPResponse | None :
110119
111120 if not to_addrs :
112121 return None
@@ -127,8 +136,10 @@ def sendmail(self, from_addr, to_addrs, msg, mail_options=None, rcpt_options=Non
127136
128137 return response
129138
130- def __enter__ (self ):
139+ def __enter__ (self ) -> SMTPBackend :
131140 return self
132141
133- def __exit__ (self , exc_type , exc_value , traceback ):
142+ def __exit__ (self , exc_type : type [BaseException ] | None ,
143+ exc_value : BaseException | None ,
144+ traceback : TracebackType | None ) -> None :
134145 self .close ()
0 commit comments