<s> import smtplib <EOL> from textwrap import dedent <EOL> from email . mime . text import MIMEText <EOL> from base import BaseBackend <EOL> class MailBackend ( BaseBackend ) : <EOL> """<STR_LIT>""" <EOL> subject = '<STR_LIT>' <EOL> recipients = [ ] <EOL> from_user = None <EOL> smtp_host = None <EOL> smtp_port = <NUM_LIT> <EOL> smtp_tls = False <EOL> smtp_user = None <EOL> smtp_password = None <EOL> def save ( self , resq = None ) : <EOL> if not self . recipients or not self . smtp_host or not self . from_user : <EOL> return <EOL> message = self . create_message ( ) <EOL> subject = self . format_subject ( ) <EOL> message [ '<STR_LIT>' ] = subject <EOL> message [ '<STR_LIT>' ] = self . from_user <EOL> message [ '<STR_LIT>' ] = "<STR_LIT:U+002CU+0020>" . join ( self . recipients ) <EOL> self . send_message ( message ) <EOL> def format_subject ( self ) : <EOL> return self . subject . format ( queue = self . _queue , <EOL> worker = self . _worker , <EOL> exception = self . _exception ) <EOL> def create_message ( self ) : <EOL> """<STR_LIT>""" <EOL> body = dedent ( """<STR_LIT>""" ) . format ( exception = self . _exception , <EOL> traceback = self . _traceback , <EOL> queue = self . _queue , <EOL> payload = self . _payload , <EOL> worker = self . _worker ) <EOL> return MIMEText ( body ) <EOL> def send_message ( self , message ) : <EOL> smtp = smtplib . SMTP ( self . smtp_host , self . smtp_port ) <EOL> try : <EOL> smtp . ehlo ( ) <EOL> if self . smtp_tls : <EOL> smtp . starttls ( ) <EOL> if self . smtp_user : <EOL> smtp . login ( self . smtp_user , self . smtp_password ) <EOL> smtp . sendmail ( self . from_user , self . recipients , message . as_string ( ) ) <EOL> finally : <EOL> smtp . close ( ) </s>
