<s> from os . path import basename <EOL> import re <EOL> try : <EOL> from email . MIMEBase import MIMEBase <EOL> except ImportError : <EOL> from email . mime . base import MIMEBase <EOL> from django . conf import settings <EOL> from django . core . mail import EmailMultiAlternatives , SafeMIMEMultipart <EOL> class EmailMultiRelated ( EmailMultiAlternatives ) : <EOL> """<STR_LIT>""" <EOL> related_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 , <EOL> alternatives = None ) : <EOL> self . related_attachments = [ ] <EOL> super ( EmailMultiRelated , self ) . __init__ ( <EOL> subject , body , from_email , to , bcc , connection , <EOL> attachments , headers , alternatives ) <EOL> def attach_related ( self , filename = None , content = None , mimetype = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( filename , MIMEBase ) : <EOL> assert content is None and mimetype is None <EOL> self . related_attachments . append ( filename ) <EOL> else : <EOL> assert content is not None <EOL> self . related_attachments . append ( ( filename , content , mimetype ) ) <EOL> def attach_related_file ( self , path , mimetype = None , filename = None ) : <EOL> """<STR_LIT>""" <EOL> if not filename : <EOL> filename = basename ( path ) <EOL> content = open ( path , '<STR_LIT:rb>' ) . read ( ) <EOL> self . attach_related ( filename , content , mimetype ) <EOL> def _create_message ( self , msg ) : <EOL> return self . _create_attachments ( <EOL> self . _create_related_attachments ( self . _create_alternatives ( msg ) ) ) <EOL> def _create_alternatives ( self , msg ) : <EOL> for i , ( content , mimetype ) in enumerate ( self . alternatives ) : <EOL> if mimetype == '<STR_LIT>' : <EOL> for filename , _ , _ in self . related_attachments : <EOL> content = re . sub ( r'<STR_LIT>' % re . escape ( filename ) , <EOL> '<STR_LIT>' % filename , content ) <EOL> self . alternatives [ i ] = ( content , mimetype ) <EOL> return super ( EmailMultiRelated , self ) . _create_alternatives ( msg ) <EOL> def _create_related_attachments ( self , msg ) : <EOL> encoding = self . encoding or settings . DEFAULT_CHARSET <EOL> if self . related_attachments : <EOL> body_msg = msg <EOL> msg = SafeMIMEMultipart ( _subtype = self . related_subtype , <EOL> encoding = encoding ) <EOL> if self . body : <EOL> msg . attach ( body_msg ) <EOL> for related in self . related_attachments : <EOL> msg . attach ( self . _create_related_attachment ( * related ) ) <EOL> return msg <EOL> def _create_related_attachment ( self , filename , content , mimetype = None ) : <EOL> """<STR_LIT>""" <EOL> attachment = super ( EmailMultiRelated , self ) . _create_attachment ( <EOL> filename , content , mimetype ) <EOL> if filename : <EOL> mimetype = attachment [ '<STR_LIT:Content-Type>' ] <EOL> del ( attachment [ '<STR_LIT:Content-Type>' ] ) <EOL> del ( attachment [ '<STR_LIT>' ] ) <EOL> attachment . add_header ( '<STR_LIT>' , '<STR_LIT>' , <EOL> filename = filename ) <EOL> attachment . add_header ( '<STR_LIT:Content-Type>' , mimetype , name = filename ) <EOL> attachment . add_header ( '<STR_LIT>' , '<STR_LIT>' % filename ) <EOL> return attachment </s>
