<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import <EOL> from __future__ import print_function <EOL> from __future__ import division <EOL> import sys , os <EOL> from email import * <EOL> from pycopia import socket <EOL> from pycopia import timelib <EOL> class MailError ( Exception ) : <EOL> pass <EOL> _HOSTNAME = None <EOL> def _get_hostname ( ) : <EOL> global _HOSTNAME <EOL> if _HOSTNAME is None : <EOL> _HOSTNAME = socket . gethostname ( ) <EOL> return _HOSTNAME <EOL> def formatdate ( timeval = None ) : <EOL> if timeval is None : <EOL> timeval = timelib . now ( ) <EOL> timeval = timelib . gmtime ( timeval ) <EOL> return "<STR_LIT>" % ( <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] [ timeval [ <NUM_LIT:6> ] ] , <EOL> timeval [ <NUM_LIT:2> ] , <EOL> [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] [ timeval [ <NUM_LIT:1> ] - <NUM_LIT:1> ] , <EOL> timeval [ <NUM_LIT:0> ] , timeval [ <NUM_LIT:3> ] , timeval [ <NUM_LIT:4> ] , timeval [ <NUM_LIT:5> ] ) <EOL> class SimpleMessage ( Message . Message ) : <EOL> def __init__ ( self , _text , mimetype = "<STR_LIT>" , charset = "<STR_LIT:utf-8>" ) : <EOL> Message . Message . __init__ ( self ) <EOL> self [ '<STR_LIT>' ] = '<STR_LIT:1.0>' <EOL> self . add_header ( '<STR_LIT:Content-Type>' , mimetype ) <EOL> self . set_payload ( _text , charset ) <EOL> def __eq__ ( self , other ) : <EOL> if self [ "<STR_LIT>" ] != other [ "<STR_LIT>" ] : <EOL> return <NUM_LIT:0> <EOL> elif str ( self [ "<STR_LIT>" ] ) != str ( other [ "<STR_LIT>" ] ) : <EOL> return <NUM_LIT:0> <EOL> elif self . get_payload ( ) != other . get_payload ( ) : <EOL> return <NUM_LIT:0> <EOL> else : <EOL> return <NUM_LIT:1> <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> class AutoMessageMixin ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , From = None , To = None ) : <EOL> self . mail_from = None <EOL> self . rcpt_to = [ ] <EOL> if From : <EOL> self . From ( From ) <EOL> if To : <EOL> self . To ( To ) <EOL> def send ( self , smtp , mail_options = None , rcpt_options = None ) : <EOL> """<STR_LIT>""" <EOL> mopts = mail_options or [ ] <EOL> rcptopts = rcpt_options or [ ] <EOL> if not self . mail_from or not self . rcpt_to : <EOL> raise MailError ( "<STR_LIT>" ) <EOL> if self . has_key ( "<STR_LIT>" ) : <EOL> del self [ "<STR_LIT>" ] <EOL> now = timelib . now ( ) <EOL> self [ "<STR_LIT>" ] = formatdate ( now ) <EOL> self [ "<STR_LIT>" ] = "<STR_LIT>" % ( <EOL> timelib . localtimestamp ( now , fmt = "<STR_LIT>" ) , <EOL> id ( self ) % <NUM_LIT> , <EOL> _get_hostname ( ) ) <EOL> return smtp . sendmail ( self . mail_from , self . rcpt_to , self . as_string ( <NUM_LIT:0> ) , mopts , rcptopts ) <EOL> def Subject ( self , subj ) : <EOL> self [ "<STR_LIT>" ] = str ( subj ) <EOL> def From ( self , addr , name = None ) : <EOL> if addr is None : <EOL> addr , name = self_address ( ) <EOL> self . mail_from = addr <EOL> self [ "<STR_LIT>" ] = get_address ( addr , name ) <EOL> self . set_unixfrom ( "<STR_LIT>" % ( addr , formatdate ( ) ) ) <EOL> def To ( self , addr , name = None ) : <EOL> self . _add_recipient ( "<STR_LIT>" , addr , name ) <EOL> def Cc ( self , addr , name = None ) : <EOL> self . _add_recipient ( "<STR_LIT>" , addr , name ) <EOL> def Bcc ( self , addr , name = None ) : <EOL> self . _add_recipient ( "<STR_LIT>" , addr , name ) <EOL> def _add_recipient ( self , header , addr , name ) : <EOL> if addr is None : <EOL> addr , name = self_address ( ) <EOL> elif type ( addr ) is tuple : <EOL> addr , name = addr <EOL> elif type ( addr ) is list : <EOL> for name in addr : <EOL> self . _add_recipient ( header , name , None ) <EOL> return <EOL> elif not isinstance ( addr , basestring ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> self . rcpt_to . append ( addr ) <EOL> long_addr = get_address ( addr , name ) <EOL> if self . has_key ( header ) : <EOL> new = self [ header ] + "<STR_LIT:U+002CU+0020>" + long_addr <EOL> del self [ header ] <EOL> self [ header ] = new <EOL> else : <EOL> self [ header ] = long_addr <EOL> class AutoMessage ( SimpleMessage , AutoMessageMixin ) : <EOL> def __init__ ( self , text , mimetype = "<STR_LIT>" , charset = "<STR_LIT:utf-8>" , From = None , To = None ) : <EOL> AutoMessageMixin . __init__ ( self , From , To ) <EOL> SimpleMessage . __init__ ( self , text , mimetype , charset ) <EOL> class MultipartMessage ( MIMEMultipart . MIMEMultipart , AutoMessageMixin ) : <EOL> def __init__ ( self , From = None , To = None ) : <EOL> MIMEMultipart . MIMEMultipart . __init__ ( self ) <EOL> AutoMessageMixin . __init__ ( self , From , To ) <EOL> def get_parser ( klass = SimpleMessage , strict = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> parser = Parser . Parser ( klass , strict ) <EOL> return parser <EOL> def parse_file ( filename , klass = SimpleMessage , strict = <NUM_LIT:0> ) : <EOL> p = get_parser ( klass , strict ) <EOL> fo = open ( filename ) <EOL> try : <EOL> msg = p . parse ( fo ) <EOL> finally : <EOL> fo . close ( ) <EOL> return msg <EOL> def parse_fileobject ( fo , klass = SimpleMessage , strict = <NUM_LIT:0> ) : <EOL> p = get_parser ( klass , strict ) <EOL> msg = p . parse ( fo ) <EOL> return msg <EOL> def new_message ( body = "<STR_LIT>" , headdict = None , msgclass = SimpleMessage ) : <EOL> """<STR_LIT>""" <EOL> msg = msgclass ( body ) <EOL> myemail , longname = self_address ( ) <EOL> msg . set_unixfrom ( "<STR_LIT>" % ( myemail , formatdate ( ) ) ) <EOL> if headdict : <EOL> for name , val in headdict . items ( ) : <EOL> msg . add_header ( name , val ) <EOL> return msg <EOL> def message_from_string ( s , klass = SimpleMessage , strict = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> parser = get_parser ( klass , strict ) <EOL> return parser . parsestr ( s ) <EOL> def self_address ( domain = None ) : <EOL> """<STR_LIT>""" <EOL> global CONFIG <EOL> name , longname = getuser ( ) <EOL> dom = domain or CONFIG . get ( "<STR_LIT>" ) <EOL> if dom : <EOL> return "<STR_LIT>" % ( name , dom ) , longname <EOL> else : <EOL> return "<STR_LIT>" % ( name , _get_hostname ( ) ) , longname <EOL> def getuser ( ) : <EOL> """<STR_LIT>""" <EOL> import pwd <EOL> uent = pwd . getpwuid ( os . getuid ( ) ) <EOL> return uent [ <NUM_LIT:0> ] , uent [ <NUM_LIT:4> ] <EOL> def parseaddr ( addr ) : <EOL> addrs = Utils . _AddressList ( addr ) . addresslist <EOL> if not addrs : <EOL> return '<STR_LIT>' , '<STR_LIT>' <EOL> return addrs [ <NUM_LIT:0> ] <EOL> def get_address ( addr , name = None ) : <EOL> if addr is None : <EOL> addr , name = self_address ( ) <EOL> if name : <EOL> return '<STR_LIT>' % ( name , addr ) <EOL> else : <EOL> return str ( addr ) <EOL> def _do_attach ( multipart , obj ) : <EOL> if isinstance ( obj , Message . Message ) : <EOL> multipart . attach ( obj ) <EOL> else : <EOL> from pycopia import textutils <EOL> body = textutils . text ( obj ) <EOL> msg = MIMEText . MIMEText ( body , _subtype = "<STR_LIT>" ) <EOL> multipart . attach ( msg ) <EOL> def ezmail ( obj , To = None , From = None , subject = None , cc = None , bcc = None , extra_headers = None , mailhost = None ) : <EOL> """<STR_LIT>""" <EOL> global CONFIG <EOL> if isinstance ( obj , ( AutoMessage , MultipartMessage ) ) : <EOL> outer = obj <EOL> else : <EOL> if type ( obj ) is list : <EOL> outer = MultipartMessage ( ) <EOL> for part in obj : <EOL> _do_attach ( outer , part ) <EOL> elif isinstance ( obj , unicode ) : <EOL> outer = AutoMessage ( obj , charset = "<STR_LIT:utf-8>" ) <EOL> else : <EOL> outer = AutoMessage ( unicode ( obj ) , charset = "<STR_LIT:utf-8>" ) <EOL> outer . From ( From ) <EOL> if To : <EOL> outer . To ( To ) <EOL> if subject : <EOL> outer . Subject ( subject ) <EOL> if cc : <EOL> outer . Cc ( cc ) <EOL> if bcc : <EOL> outer . Bcc ( bcc ) <EOL> if extra_headers : <EOL> for name , value in extra_headers . items ( ) : <EOL> outer [ name ] = value <EOL> mhost = mailhost or CONFIG . get ( "<STR_LIT>" , "<STR_LIT:localhost>" ) <EOL> if mhost == "<STR_LIT:localhost>" : <EOL> smtp = LocalSender ( ) <EOL> status = outer . send ( smtp ) <EOL> if not status : <EOL> raise MailError ( str ( status ) ) <EOL> else : <EOL> from pycopia . inet import SMTP <EOL> smtp = SMTP . SMTP ( mhost , bindto = CONFIG . get ( "<STR_LIT>" ) ) <EOL> errs = outer . send ( smtp ) <EOL> smtp . quit ( ) <EOL> if errs : <EOL> raise MailError ( str ( status ) ) <EOL> return outer [ "<STR_LIT>" ] <EOL> def mail ( obj , To = None , From = None , subject = None , cc = None , bcc = None , extra_headers = None , mailhost = None ) : <EOL> try : <EOL> return ezmail ( obj , To , From , subject , cc , bcc , extra_headers , mailhost ) <EOL> except MailError as err : <EOL> print ( "<STR_LIT>" , file = sys . stderr ) <EOL> print ( err , file = sys . stderr ) <EOL> return None <EOL> def get_config ( ) : <EOL> from pycopia import basicconfig <EOL> return basicconfig . get_config ( "<STR_LIT>" ) <EOL> class LocalSender ( object ) : <EOL> """<STR_LIT>""" <EOL> def sendmail ( self , From , rcpt_to , msg , mopts = None , rcptopts = None ) : <EOL> from pycopia import proctools <EOL> cmd = "<STR_LIT>" <EOL> for rcpt in rcpt_to : <EOL> cmd += '<STR_LIT>' % rcpt <EOL> proc = proctools . spawnpty ( cmd ) <EOL> proc . write ( msg ) <EOL> proc . write ( "<STR_LIT:\n>" ) <EOL> proc . send_eof ( ) <EOL> proc . wait ( ) <EOL> return proc . exitstatus <EOL> CONFIG = get_config ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> print ( mail ( [ "<STR_LIT>" , "<STR_LIT>" ] , To = "<STR_LIT>" , subject = "<STR_LIT>" ) ) </s>
