<s> from smtplib import SMTPException <EOL> from sellmo . contrib . mailing . handlers import AbstractMailHandler <EOL> from sellmo . contrib . mailing . signals import mail_send , mail_failed <EOL> from django . core import mail <EOL> class DefaultMailHandler ( AbstractMailHandler ) : <EOL> def handle_mail ( <EOL> self , <EOL> message_type , <EOL> message_reference , <EOL> context , <EOL> connection = None <EOL> ) : <EOL> with self . writer . open ( context ) as writer : <EOL> if set ( [ '<STR_LIT:html>' , '<STR_LIT:text>' ] ) == set ( writer . formats ) : <EOL> message = mail . EmailMultiAlternatives ( ) <EOL> message . body = writer . get_body ( '<STR_LIT:text>' ) <EOL> message . attach_alternative ( <EOL> writer . get_body ( '<STR_LIT:html>' ) , '<STR_LIT>' <EOL> ) <EOL> else : <EOL> message = mail . EmailMessage ( ) <EOL> if '<STR_LIT:html>' in writer . formats : <EOL> message . content_subtype = '<STR_LIT:html>' <EOL> message . body = writer . get_body ( '<STR_LIT:html>' ) <EOL> elif '<STR_LIT:text>' in writer . formats : <EOL> message . body = writer . get_body ( '<STR_LIT:text>' ) <EOL> else : <EOL> raise Exception ( <EOL> "<STR_LIT>" . format ( writer . formats ) <EOL> ) <EOL> message . subject = writer . get_subject ( ) <EOL> message . from_email = writer . get_from ( ) <EOL> to = writer . get_to ( ) <EOL> if to and not isinstance ( to , ( list , tuple ) ) : <EOL> to = [ to ] <EOL> message . to = to <EOL> bcc = writer . get_bcc ( ) <EOL> if bcc and not isinstance ( bcc , ( list , tuple ) ) : <EOL> bcc = [ bcc ] <EOL> message . bcc = bcc <EOL> message . bcc = writer . get_bcc ( ) <EOL> message . header = writer . get_headers ( ) <EOL> message . attachments = writer . get_attachments ( ) <EOL> if connection : <EOL> message . connection = connection <EOL> try : <EOL> message . send ( ) <EOL> except SMTPException as exception : <EOL> mail_failed . send ( <EOL> sender = self , <EOL> message_type = message_type , <EOL> message_reference = message_reference , <EOL> context = context , <EOL> reason = str ( exception ) , <EOL> message = message , <EOL> ) <EOL> else : <EOL> mail_send . send ( <EOL> sender = self , <EOL> message_type = message_type , <EOL> message_reference = message_reference , <EOL> context = context , <EOL> message = message <EOL> ) </s>
