<s> """<STR_LIT>""" <EOL> import os <EOL> import base64 <EOL> import uuid <EOL> import smtplib <EOL> import mimetypes <EOL> from email import mime <EOL> from email . mime . text import MIMEText <EOL> from email . mime . image import MIMEImage <EOL> from email . mime . base import MIMEBase <EOL> from email . mime . multipart import MIMEMultipart <EOL> from email . header import Header <EOL> from email . utils import COMMASPACE , formatdate , formataddr , make_msgid <EOL> from email . encoders import encode_base64 <EOL> from email . charset import Charset <EOL> __author__ = '<STR_LIT>' <EOL> __version__ = '<STR_LIT>' <EOL> mimetypes . init ( ) <EOL> class InvalidConfiguration ( Exception ) : <EOL> pass <EOL> class SMTP ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host , port , username , password , ssl = False , tls = False , <EOL> timeout = None , debug_level = None ) : <EOL> self . host = host <EOL> self . port = port <EOL> self . username = username <EOL> self . password = password <EOL> self . ssl = ssl <EOL> self . tls = tls <EOL> self . timeout = timeout <EOL> self . debug_level = debug_level <EOL> self . client = None <EOL> def __enter__ ( self ) : <EOL> self . login ( ) <EOL> return self <EOL> def __exit__ ( self , exc_type , exc_val , exc_tb ) : <EOL> self . close ( ) <EOL> def _get_client ( self ) : <EOL> """<STR_LIT>""" <EOL> connection_args = { <EOL> '<STR_LIT:host>' : self . host , <EOL> '<STR_LIT:port>' : self . port , <EOL> } <EOL> if self . timeout : <EOL> connection_args [ '<STR_LIT>' ] = self . timeout <EOL> if self . ssl : <EOL> client = smtplib . SMTP_SSL ( ** connection_args ) <EOL> else : <EOL> client = smtplib . SMTP ( ** connection_args ) <EOL> if self . tls : <EOL> client . ehlo ( ) <EOL> client . starttls ( ) <EOL> client . ehlo ( ) <EOL> if self . debug_level : <EOL> client . set_debuglevel ( self . debug_level ) <EOL> return client <EOL> def login ( self ) : <EOL> """<STR_LIT>""" <EOL> self . client = self . _get_client ( ) <EOL> if self . username and self . password : <EOL> try : <EOL> self . client . login ( self . username . encode ( '<STR_LIT:utf-8>' ) , self . password . encode ( '<STR_LIT:utf-8>' ) ) <EOL> except ( smtplib . SMTPException , smtplib . SMTPAuthenticationError ) as e : <EOL> self . client . docmd ( "<STR_LIT>" , base64 . b64encode ( self . username ) ) <EOL> self . client . docmd ( base64 . b64encode ( self . password ) , "<STR_LIT>" ) <EOL> else : <EOL> self . client . connect ( ) <EOL> def close ( self ) : <EOL> self . client . quit ( ) <EOL> def send ( self , email ) : <EOL> """<STR_LIT>""" <EOL> return self . client . sendmail ( email . sender , email . recipients , email . get_payload ( ) ) <EOL> class Email ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , sender = None , recipients = [ ] , cc = [ ] , bcc = [ ] , <EOL> subject = None , text = None , html = None , charset = None , <EOL> attachments = [ ] , embedded = [ ] , headers = [ ] ) : <EOL> self . sender = sender <EOL> self . recipients = recipients if type ( recipients ) in ( list , tuple ) else [ recipients ] <EOL> self . cc = cc <EOL> self . bcc = bcc <EOL> self . subject = subject <EOL> self . text = text <EOL> self . html = html <EOL> self . attachments = attachments <EOL> self . embedded = embedded <EOL> self . charset = charset or '<STR_LIT:utf-8>' <EOL> self . headers = headers <EOL> self . compiled = False <EOL> def is_valid_message ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . sender : <EOL> raise InvalidConfiguration ( '<STR_LIT>' ) <EOL> if not self . recipients and not self . cc and not self . bcc : <EOL> raise InvalidConfiguration ( '<STR_LIT>' ) <EOL> return True <EOL> def get_root_message ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( self . text and self . html ) or self . attachments or self . embedded : <EOL> self . message = MIMEMultipart ( '<STR_LIT>' , None ) <EOL> if self . text : <EOL> self . message . attach ( self . get_email_part ( self . text , '<STR_LIT>' ) ) <EOL> if self . html : <EOL> self . message . attach ( self . get_email_part ( self . html , '<STR_LIT:html>' ) ) <EOL> elif self . text or self . html : <EOL> if self . text : <EOL> self . message = MIMEText ( self . text . encode ( self . charset ) , '<STR_LIT>' , self . charset ) <EOL> else : <EOL> self . message = MIMEText ( self . html . encode ( self . charset ) , '<STR_LIT:html>' , self . charset ) <EOL> else : <EOL> self . message = MIMEText ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . message [ '<STR_LIT>' ] = self . format_email_address ( email_type = '<STR_LIT>' , emails = [ self . sender ] ) <EOL> if self . recipients : <EOL> self . message [ '<STR_LIT>' ] = self . format_email_address ( email_type = '<STR_LIT:to>' , emails = self . recipients ) <EOL> if self . cc : <EOL> self . message [ '<STR_LIT>' ] = self . format_email_address ( email_type = '<STR_LIT>' , emails = self . recipients ) <EOL> self . message [ '<STR_LIT>' ] = self . get_header ( '<STR_LIT>' , self . subject ) <EOL> self . message [ '<STR_LIT>' ] = formatdate ( localtime = True ) <EOL> self . message [ '<STR_LIT>' ] = make_msgid ( str ( uuid . uuid4 ( ) ) ) <EOL> self . message [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> return self . message <EOL> def get_header ( self , name , value = None ) : <EOL> """<STR_LIT>""" <EOL> _header = Header ( header_name = name , charset = self . charset ) <EOL> if value : <EOL> _header . append ( value ) <EOL> return _header <EOL> def format_email_address ( self , email_type , emails = None ) : <EOL> """<STR_LIT>""" <EOL> emails = emails or self . recipients or [ ] <EOL> header = self . get_header ( email_type ) <EOL> for i , address in enumerate ( emails ) : <EOL> if i > <NUM_LIT:0> : <EOL> header . append ( '<STR_LIT:U+002C>' , '<STR_LIT>' ) <EOL> if isinstance ( address , basestring ) : <EOL> try : <EOL> header . append ( address , charset = '<STR_LIT>' ) <EOL> except UnicodeError : <EOL> header . append ( address , charset = self . charset ) <EOL> elif type ( address ) in ( tuple , list ) : <EOL> _name , _address = address <EOL> try : <EOL> _name . encode ( '<STR_LIT>' ) <EOL> formatted_address = formataddr ( address ) <EOL> header . append ( formatted_address , charset = '<STR_LIT>' ) <EOL> except UnicodeError : <EOL> header . append ( _name ) <EOL> header . append ( '<STR_LIT>' . format ( _address ) , charset = '<STR_LIT>' ) <EOL> return header <EOL> def compile_message ( self ) : <EOL> """<STR_LIT>""" <EOL> message = self . get_root_message ( ) <EOL> if self . attachments : <EOL> for attachment in self . attachments : <EOL> message . attach ( self . get_file_attachment ( attachment ) ) <EOL> if self . embedded : <EOL> for embedded in self . embedded : <EOL> message . attach ( self . get_embedded_image ( embedded ) ) <EOL> self . message = message <EOL> self . compiled = True <EOL> return self . message <EOL> def get_payload ( self ) : <EOL> """<STR_LIT>""" <EOL> if not self . compiled : <EOL> self . compile_message ( ) <EOL> return self . message . as_string ( ) <EOL> def get_embedded_image ( self , path ) : <EOL> email_part = self . assert_file_mime_type ( path ) <EOL> with open ( path , '<STR_LIT:r>' ) as embedded_file : <EOL> email_part . set_payload ( embedded_file . read ( ) ) <EOL> encode_base64 ( email_part ) <EOL> email_part . add_header ( '<STR_LIT>' , '<STR_LIT>' . format ( os . path . basename ( path ) ) ) <EOL> return email_part <EOL> def get_file_attachment ( self , path ) : <EOL> """<STR_LIT>""" <EOL> email_part = self . assert_file_mime_type ( path ) <EOL> with open ( path , '<STR_LIT:r>' ) as attached_file : <EOL> email_part . set_payload ( attached_file . read ( ) ) <EOL> if email_part . get_content_maintype ( ) != "<STR_LIT:text>" : <EOL> encode_base64 ( email_part ) <EOL> email_part . add_header ( '<STR_LIT>' , '<STR_LIT>' % os . path . basename ( path ) ) <EOL> return email_part <EOL> def assert_file_mime_type ( self , path , fallback = None ) : <EOL> """<STR_LIT>""" <EOL> fallback = fallback or [ '<STR_LIT:text>' , '<STR_LIT>' ] <EOL> asserted_mimetype = mimetypes . guess_type ( path , False ) [ <NUM_LIT:0> ] <EOL> if asserted_mimetype is None : <EOL> mimetype = MIMEBase ( * fallback ) <EOL> elif asserted_mimetype . startswith ( '<STR_LIT:image>' ) : <EOL> with open ( path , '<STR_LIT:r>' ) as embedded_file : <EOL> mimetype = MIMEImage ( embedded_file . read ( ) , _subtype = asserted_mimetype . split ( '<STR_LIT:/>' ) [ <NUM_LIT:1> ] ) <EOL> else : <EOL> mimetype = MIMEBase ( * asserted_mimetype . split ( '<STR_LIT:/>' ) ) <EOL> return mimetype <EOL> def get_email_part ( self , body , format = '<STR_LIT:html>' ) : <EOL> """<STR_LIT>""" <EOL> charset = Charset ( self . charset ) <EOL> email_part = MIMEText ( body , format , self . charset ) <EOL> email_part . set_charset ( charset ) <EOL> return email_part <EOL> def add_header ( self , header ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( header , basestring ) : <EOL> header = Header ( header ) <EOL> self . headers . append ( header ) <EOL> def add_recipient ( self , recipient ) : <EOL> """<STR_LIT>""" <EOL> self . recipients . append ( recipient ) <EOL> def add_attachment ( self , file ) : <EOL> """<STR_LIT>""" <EOL> self . attachments . append ( file ) <EOL> def add_embedded_image ( self , image ) : <EOL> """<STR_LIT>""" <EOL> self . embedded . append ( image ) </s>
