<s> """<STR_LIT>""" <EOL> import smtplib <EOL> import ssl <EOL> import threading <EOL> from django . conf import settings <EOL> from django . core . mail . backends . base import BaseEmailBackend <EOL> from django . core . mail . utils import DNS_NAME <EOL> from django . core . mail . message import sanitize_address <EOL> class EmailBackend ( BaseEmailBackend ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host = None , port = None , username = None , password = None , <EOL> use_tls = None , fail_silently = False , ** kwargs ) : <EOL> super ( EmailBackend , self ) . __init__ ( fail_silently = fail_silently ) <EOL> self . host = host or settings . EMAIL_HOST <EOL> self . port = port or settings . EMAIL_PORT <EOL> if username is None : <EOL> self . username = settings . EMAIL_HOST_USER <EOL> else : <EOL> self . username = username <EOL> if password is None : <EOL> self . password = settings . EMAIL_HOST_PASSWORD <EOL> else : <EOL> self . password = password <EOL> if use_tls is None : <EOL> self . use_tls = settings . EMAIL_USE_TLS <EOL> else : <EOL> self . use_tls = use_tls <EOL> self . connection = None <EOL> self . _lock = threading . RLock ( ) <EOL> def open ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . connection : <EOL> return False <EOL> try : <EOL> self . connection = smtplib . SMTP ( self . host , self . port , <EOL> local_hostname = DNS_NAME . get_fqdn ( ) ) <EOL> if self . use_tls : <EOL> self . connection . ehlo ( ) <EOL> self . connection . starttls ( ) <EOL> self . connection . ehlo ( ) <EOL> if self . username and self . password : <EOL> self . connection . login ( self . username , self . password ) <EOL> return True <EOL> except : <EOL> if not self . fail_silently : <EOL> raise <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . connection is None : <EOL> return <EOL> try : <EOL> try : <EOL> self . connection . quit ( ) <EOL> except ( ssl . SSLError , smtplib . SMTPServerDisconnected ) : <EOL> self . connection . close ( ) <EOL> except : <EOL> if self . fail_silently : <EOL> return <EOL> raise <EOL> finally : <EOL> self . connection = None <EOL> def send_messages ( self , email_messages ) : <EOL> """<STR_LIT>""" <EOL> if not email_messages : <EOL> return <EOL> with self . _lock : <EOL> new_conn_created = self . open ( ) <EOL> if not self . connection : <EOL> return <EOL> num_sent = <NUM_LIT:0> <EOL> for message in email_messages : <EOL> sent = self . _send ( message ) <EOL> if sent : <EOL> num_sent += <NUM_LIT:1> <EOL> if new_conn_created : <EOL> self . close ( ) <EOL> return num_sent <EOL> def _send ( self , email_message ) : <EOL> """<STR_LIT>""" <EOL> if not email_message . recipients ( ) : <EOL> return False <EOL> from_email = sanitize_address ( email_message . from_email , email_message . encoding ) <EOL> recipients = [ sanitize_address ( addr , email_message . encoding ) <EOL> for addr in email_message . recipients ( ) ] <EOL> message = email_message . message ( ) <EOL> try : <EOL> self . connection . sendmail ( from_email , recipients , message . as_bytes ( ) ) <EOL> except smtplib . SMTPException : <EOL> if not self . fail_silently : <EOL> raise <EOL> return False <EOL> return True </s>
