<s> import mimetypes <EOL> import os <EOL> import random <EOL> import time <EOL> from email import charset as Charset , encoders as Encoders <EOL> from email . generator import Generator <EOL> from email . mime . text import MIMEText <EOL> from email . mime . multipart import MIMEMultipart <EOL> from email . mime . base import MIMEBase <EOL> from email . header import Header <EOL> from email . utils import formatdate , getaddresses , formataddr , parseaddr <EOL> from django . conf import settings <EOL> from django . core . mail . utils import DNS_NAME <EOL> from django . utils . encoding import smart_str , force_unicode <EOL> try : <EOL> from cStringIO import StringIO <EOL> except ImportError : <EOL> from StringIO import StringIO <EOL> Charset . add_charset ( '<STR_LIT:utf-8>' , Charset . SHORTEST , None , '<STR_LIT:utf-8>' ) <EOL> DEFAULT_ATTACHMENT_MIME_TYPE = '<STR_LIT>' <EOL> class BadHeaderError ( ValueError ) : <EOL> pass <EOL> def make_msgid ( idstring = None ) : <EOL> """<STR_LIT>""" <EOL> timeval = time . time ( ) <EOL> utcdate = time . strftime ( '<STR_LIT>' , time . gmtime ( timeval ) ) <EOL> try : <EOL> pid = os . getpid ( ) <EOL> except AttributeError : <EOL> pid = <NUM_LIT:1> <EOL> randint = random . randrange ( <NUM_LIT> ) <EOL> if idstring is None : <EOL> idstring = '<STR_LIT>' <EOL> else : <EOL> idstring = '<STR_LIT:.>' + idstring <EOL> idhost = DNS_NAME <EOL> msgid = '<STR_LIT>' % ( utcdate , pid , randint , idstring , idhost ) <EOL> return msgid <EOL> ADDRESS_HEADERS = set ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:to>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def forbid_multi_line_headers ( name , val , encoding ) : <EOL> """<STR_LIT>""" <EOL> encoding = encoding or settings . DEFAULT_CHARSET <EOL> val = force_unicode ( val ) <EOL> if '<STR_LIT:\n>' in val or '<STR_LIT:\r>' in val : <EOL> raise BadHeaderError ( "<STR_LIT>" % ( val , name ) ) <EOL> try : <EOL> val = val . encode ( '<STR_LIT:ascii>' ) <EOL> except UnicodeEncodeError : <EOL> if name . lower ( ) in ADDRESS_HEADERS : <EOL> val = '<STR_LIT:U+002CU+0020>' . join ( sanitize_address ( addr , encoding ) <EOL> for addr in getaddresses ( ( val , ) ) ) <EOL> else : <EOL> val = str ( Header ( val , encoding ) ) <EOL> else : <EOL> if name . lower ( ) == '<STR_LIT>' : <EOL> val = Header ( val ) <EOL> return name , val <EOL> def sanitize_address ( addr , encoding ) : <EOL> if isinstance ( addr , basestring ) : <EOL> addr = parseaddr ( force_unicode ( addr ) ) <EOL> nm , addr = addr <EOL> nm = str ( Header ( nm , encoding ) ) <EOL> try : <EOL> addr = addr . encode ( '<STR_LIT:ascii>' ) <EOL> except UnicodeEncodeError : <EOL> if u'<STR_LIT:@>' in addr : <EOL> localpart , domain = addr . split ( u'<STR_LIT:@>' , <NUM_LIT:1> ) <EOL> localpart = str ( Header ( localpart , encoding ) ) <EOL> domain = domain . encode ( '<STR_LIT>' ) <EOL> addr = '<STR_LIT:@>' . join ( [ localpart , domain ] ) <EOL> else : <EOL> addr = str ( Header ( addr , encoding ) ) <EOL> return formataddr ( ( nm , addr ) ) <EOL> class SafeMIMEText ( MIMEText ) : <EOL> def __init__ ( self , text , subtype , charset ) : <EOL> self . encoding = charset <EOL> MIMEText . __init__ ( self , text , subtype , charset ) <EOL> def __setitem__ ( self , name , val ) : <EOL> name , val = forbid_multi_line_headers ( name , val , self . encoding ) <EOL> MIMEText . __setitem__ ( self , name , val ) <EOL> def as_string ( self , unixfrom = False ) : <EOL> """<STR_LIT>""" <EOL> fp = StringIO ( ) <EOL> g = Generator ( fp , mangle_from_ = False ) <EOL> g . flatten ( self , unixfrom = unixfrom ) <EOL> return fp . getvalue ( ) <EOL> class SafeMIMEMultipart ( MIMEMultipart ) : <EOL> def __init__ ( self , _subtype = '<STR_LIT>' , boundary = None , _subparts = None , encoding = None , ** _params ) : <EOL> self . encoding = encoding <EOL> MIMEMultipart . __init__ ( self , _subtype , boundary , _subparts , ** _params ) <EOL> def __setitem__ ( self , name , val ) : <EOL> name , val = forbid_multi_line_headers ( name , val , self . encoding ) <EOL> MIMEMultipart . __setitem__ ( self , name , val ) <EOL> def as_string ( self , unixfrom = False ) : <EOL> """<STR_LIT>""" <EOL> fp = StringIO ( ) <EOL> g = Generator ( fp , mangle_from_ = False ) <EOL> g . flatten ( self , unixfrom = unixfrom ) <EOL> return fp . getvalue ( ) <EOL> class EmailMessage ( object ) : <EOL> """<STR_LIT>""" <EOL> content_subtype = '<STR_LIT>' <EOL> mixed_subtype = '<STR_LIT>' <EOL> encoding = None <EOL> def __init__ ( self , subject = '<STR_LIT>' , body = '<STR_LIT>' , from_email = None , to = None , bcc = None , <EOL> connection = None , attachments = None , headers = None , cc = None ) : <EOL> """<STR_LIT>""" <EOL> if to : <EOL> assert not isinstance ( to , basestring ) , '<STR_LIT>' <EOL> self . to = list ( to ) <EOL> else : <EOL> self . to = [ ] <EOL> if cc : <EOL> assert not isinstance ( cc , basestring ) , '<STR_LIT>' <EOL> self . cc = list ( cc ) <EOL> else : <EOL> self . cc = [ ] <EOL> if bcc : <EOL> assert not isinstance ( bcc , basestring ) , '<STR_LIT>' <EOL> self . bcc = list ( bcc ) <EOL> else : <EOL> self . bcc = [ ] <EOL> self . from_email = from_email or settings . DEFAULT_FROM_EMAIL <EOL> self . subject = subject <EOL> self . body = body <EOL> self . attachments = attachments or [ ] <EOL> self . extra_headers = headers or { } <EOL> self . connection = connection <EOL> def get_connection ( self , fail_silently = False ) : <EOL> from django . core . mail import get_connection <EOL> if not self . connection : <EOL> self . connection = get_connection ( fail_silently = fail_silently ) <EOL> return self . connection <EOL> def message ( self ) : <EOL> encoding = self . encoding or settings . DEFAULT_CHARSET <EOL> msg = SafeMIMEText ( smart_str ( self . body , encoding ) , <EOL> self . content_subtype , encoding ) <EOL> msg = self . _create_message ( msg ) <EOL> msg [ '<STR_LIT>' ] = self . subject <EOL> msg [ '<STR_LIT>' ] = self . extra_headers . get ( '<STR_LIT>' , self . from_email ) <EOL> msg [ '<STR_LIT>' ] = self . extra_headers . get ( '<STR_LIT>' , '<STR_LIT:U+002CU+0020>' . join ( self . to ) ) <EOL> if self . cc : <EOL> msg [ '<STR_LIT>' ] = '<STR_LIT:U+002CU+0020>' . join ( self . cc ) <EOL> header_names = [ key . lower ( ) for key in self . extra_headers ] <EOL> if '<STR_LIT:date>' not in header_names : <EOL> msg [ '<STR_LIT>' ] = formatdate ( ) <EOL> if '<STR_LIT>' not in header_names : <EOL> msg [ '<STR_LIT>' ] = make_msgid ( ) <EOL> for name , value in self . extra_headers . items ( ) : <EOL> if name . lower ( ) in ( '<STR_LIT>' , '<STR_LIT:to>' ) : <EOL> continue <EOL> msg [ name ] = value <EOL> return msg <EOL> def recipients ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . to + self . cc + self . bcc <EOL> def send ( self , fail_silently = False ) : <EOL> """<STR_LIT>""" <EOL> if not self . recipients ( ) : <EOL> return <NUM_LIT:0> <EOL> return self . get_connection ( fail_silently ) . send_messages ( [ self ] ) <EOL> def attach ( self , filename = None , content = None , mimetype = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( filename , MIMEBase ) : <EOL> assert content == mimetype == None <EOL> self . attachments . append ( filename ) <EOL> else : <EOL> assert content is not None <EOL> self . attachments . append ( ( filename , content , mimetype ) ) <EOL> def attach_file ( self , path , mimetype = None ) : <EOL> """<STR_LIT>""" <EOL> filename = os . path . basename ( path ) <EOL> content = open ( path , '<STR_LIT:rb>' ) . read ( ) <EOL> self . attach ( filename , content , mimetype ) <EOL> def _create_message ( self , msg ) : <EOL> return self . _create_attachments ( msg ) <EOL> def _create_attachments ( self , msg ) : <EOL> if self . attachments : <EOL> encoding = self . encoding or settings . DEFAULT_CHARSET <EOL> body_msg = msg <EOL> msg = SafeMIMEMultipart ( _subtype = self . mixed_subtype , encoding = encoding ) <EOL> if self . body : <EOL> msg . attach ( body_msg ) <EOL> for attachment in self . attachments : <EOL> if isinstance ( attachment , MIMEBase ) : <EOL> msg . attach ( attachment ) <EOL> else : <EOL> msg . attach ( self . _create_attachment ( * attachment ) ) <EOL> return msg <EOL> def _create_mime_attachment ( self , content , mimetype ) : <EOL> """<STR_LIT>""" <EOL> basetype , subtype = mimetype . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> if basetype == '<STR_LIT:text>' : <EOL> encoding = self . encoding or settings . DEFAULT_CHARSET <EOL> attachment = SafeMIMEText ( smart_str ( content , encoding ) , subtype , encoding ) <EOL> else : <EOL> attachment = MIMEBase ( basetype , subtype ) <EOL> attachment . set_payload ( content ) <EOL> Encoders . encode_base64 ( attachment ) <EOL> return attachment <EOL> def _create_attachment ( self , filename , content , mimetype = None ) : <EOL> """<STR_LIT>""" <EOL> if mimetype is None : <EOL> mimetype , _ = mimetypes . guess_type ( filename ) <EOL> if mimetype is None : <EOL> mimetype = DEFAULT_ATTACHMENT_MIME_TYPE <EOL> attachment = self . _create_mime_attachment ( content , mimetype ) <EOL> if filename : <EOL> try : <EOL> filename = filename . encode ( '<STR_LIT:ascii>' ) <EOL> except UnicodeEncodeError : <EOL> filename = ( '<STR_LIT:utf-8>' , '<STR_LIT>' , filename . encode ( '<STR_LIT:utf-8>' ) ) <EOL> attachment . add_header ( '<STR_LIT>' , '<STR_LIT>' , <EOL> filename = filename ) <EOL> return attachment <EOL> class EmailMultiAlternatives ( EmailMessage ) : <EOL> """<STR_LIT>""" <EOL> alternative_subtype = '<STR_LIT>' <EOL> def __init__ ( self , subject = '<STR_LIT>' , body = '<STR_LIT>' , from_email = None , to = None , bcc = None , <EOL> connection = None , attachments = None , headers = None , alternatives = None , <EOL> cc = None ) : <EOL> """<STR_LIT>""" <EOL> super ( EmailMultiAlternatives , self ) . __init__ ( subject , body , from_email , to , bcc , connection , attachments , headers , cc ) <EOL> self . alternatives = alternatives or [ ] <EOL> def attach_alternative ( self , content , mimetype ) : <EOL> """<STR_LIT>""" <EOL> assert content is not None <EOL> assert mimetype is not None <EOL> self . alternatives . append ( ( content , mimetype ) ) <EOL> def _create_message ( self , msg ) : <EOL> return self . _create_attachments ( self . _create_alternatives ( msg ) ) <EOL> def _create_alternatives ( self , msg ) : <EOL> encoding = self . encoding or settings . DEFAULT_CHARSET <EOL> if self . alternatives : <EOL> body_msg = msg <EOL> msg = SafeMIMEMultipart ( _subtype = self . alternative_subtype , encoding = encoding ) <EOL> if self . body : <EOL> msg . attach ( body_msg ) <EOL> for alternative in self . alternatives : <EOL> msg . attach ( self . _create_mime_attachment ( * alternative ) ) <EOL> return msg </s>
