<s> from email . mime . multipart import MIMEMultipart <EOL> from email . mime . text import MIMEText <EOL> import smtplib <EOL> from clay import config <EOL> log = config . get_logger ( '<STR_LIT>' ) <EOL> def _string_or_list ( obj ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( obj , basestring ) : <EOL> return [ obj ] <EOL> else : <EOL> return obj <EOL> def sendmail ( mailto , subject , message , subtype = '<STR_LIT:html>' , charset = '<STR_LIT:utf-8>' , smtpconfig = None , ** headers ) : <EOL> '''<STR_LIT>''' <EOL> if not smtpconfig : <EOL> smtpconfig = config . get ( '<STR_LIT>' ) or config . get ( '<STR_LIT>' ) <EOL> headers [ '<STR_LIT>' ] = _string_or_list ( mailto ) <EOL> msg = MIMEMultipart ( '<STR_LIT>' ) <EOL> msg [ '<STR_LIT>' ] = subject <EOL> for key , value in headers . iteritems ( ) : <EOL> for val in _string_or_list ( value ) : <EOL> msg . add_header ( key , val ) <EOL> text = MIMEText ( message , subtype , charset ) <EOL> msg . attach ( text ) <EOL> if not '<STR_LIT>' in msg : <EOL> msg [ '<STR_LIT>' ] = smtpconfig . get ( '<STR_LIT>' ) <EOL> mailfrom = msg [ '<STR_LIT>' ] <EOL> assert isinstance ( mailfrom , basestring ) <EOL> recipients = [ ] <EOL> for toheader in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) : <EOL> recipients += msg . get_all ( toheader , [ ] ) <EOL> if '<STR_LIT>' in msg : <EOL> del msg [ '<STR_LIT>' ] <EOL> smtp = smtplib . SMTP ( smtpconfig . get ( '<STR_LIT:host>' ) , smtpconfig . get ( '<STR_LIT:port>' ) ) <EOL> if smtpconfig . get ( '<STR_LIT:username>' , None ) is not None and smtpconfig . get ( '<STR_LIT:password>' , None ) is not None : <EOL> smtp . login ( smtpconfig . get ( '<STR_LIT:username>' ) , smtpconfig . get ( '<STR_LIT:password>' ) ) <EOL> smtp . sendmail ( mailfrom , recipients , msg . as_string ( ) ) <EOL> smtp . quit ( ) <EOL> log . info ( '<STR_LIT>' , recipients , subject ) </s>
