<s> """<STR_LIT>""" <EOL> import email <EOL> from email import MIMEBase <EOL> from email import MIMEMultipart <EOL> from email import MIMEText <EOL> from email import Parser <EOL> import email . header <EOL> import logging <EOL> from google . appengine . api import api_base_pb <EOL> from google . appengine . api import apiproxy_stub_map <EOL> from google . appengine . api import mail_service_pb <EOL> from google . appengine . api import users <EOL> from google . appengine . api . mail_errors import * <EOL> from google . appengine . runtime import apiproxy_errors <EOL> ERROR_MAP = { <EOL> mail_service_pb . MailServiceError . BAD_REQUEST : <EOL> BadRequestError , <EOL> mail_service_pb . MailServiceError . UNAUTHORIZED_SENDER : <EOL> InvalidSenderError , <EOL> mail_service_pb . MailServiceError . INVALID_ATTACHMENT_TYPE : <EOL> InvalidAttachmentTypeError , <EOL> mail_service_pb . MailServiceError . INVALID_HEADER_NAME : <EOL> InvalidHeaderNameError , <EOL> } <EOL> EXTENSION_MIME_MAP = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:html>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT:text>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> EXTENSION_BLACKLIST = [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] <EOL> HEADER_WHITELIST = frozenset ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> def invalid_email_reason ( email_address , field ) : <EOL> """<STR_LIT>""" <EOL> if email_address is None : <EOL> return '<STR_LIT>' % field <EOL> if isinstance ( email_address , users . User ) : <EOL> email_address = email_address . email ( ) <EOL> if not isinstance ( email_address , basestring ) : <EOL> return '<STR_LIT>' % field <EOL> stripped_address = email_address . strip ( ) <EOL> if not stripped_address : <EOL> return '<STR_LIT>' % field <EOL> return None <EOL> InvalidEmailReason = invalid_email_reason <EOL> def is_email_valid ( email_address ) : <EOL> """<STR_LIT>""" <EOL> return invalid_email_reason ( email_address , '<STR_LIT>' ) is None <EOL> IsEmailValid = is_email_valid <EOL> def check_email_valid ( email_address , field ) : <EOL> """<STR_LIT>""" <EOL> reason = invalid_email_reason ( email_address , field ) <EOL> if reason is not None : <EOL> raise InvalidEmailError ( reason ) <EOL> CheckEmailValid = check_email_valid <EOL> def is_ascii ( string ) : <EOL> """<STR_LIT>""" <EOL> return all ( ord ( c ) < <NUM_LIT> for c in string ) <EOL> def invalid_headers_reason ( headers ) : <EOL> """<STR_LIT>""" <EOL> if headers is None : <EOL> return '<STR_LIT>' <EOL> if not isinstance ( headers , dict ) : <EOL> return '<STR_LIT>' <EOL> for k , v in headers . iteritems ( ) : <EOL> if not isinstance ( k , basestring ) : <EOL> return '<STR_LIT>' <EOL> if not isinstance ( v , basestring ) : <EOL> return '<STR_LIT>' <EOL> if not is_ascii ( k ) : <EOL> return '<STR_LIT>' <EOL> if k . strip ( ) not in HEADER_WHITELIST : <EOL> return '<STR_LIT>' % k . strip ( ) <EOL> def check_headers_valid ( headers ) : <EOL> """<STR_LIT>""" <EOL> reason = invalid_headers_reason ( headers ) <EOL> if reason is not None : <EOL> raise InvalidEmailError ( reason ) <EOL> def _email_sequence ( emails ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( emails , basestring ) : <EOL> return emails , <EOL> return emails <EOL> def _attachment_sequence ( attachments ) : <EOL> """<STR_LIT>""" <EOL> if len ( attachments ) == <NUM_LIT:2> and isinstance ( attachments [ <NUM_LIT:0> ] , basestring ) : <EOL> attachments = attachments , <EOL> for attachment in attachments : <EOL> if isinstance ( attachment , Attachment ) : <EOL> yield attachment <EOL> else : <EOL> yield Attachment ( * attachment ) <EOL> def _parse_mime_message ( mime_message ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( mime_message , email . Message . Message ) : <EOL> return mime_message <EOL> elif isinstance ( mime_message , basestring ) : <EOL> return email . message_from_string ( mime_message ) <EOL> else : <EOL> return email . message_from_file ( mime_message ) <EOL> def send_mail ( sender , <EOL> to , <EOL> subject , <EOL> body , <EOL> make_sync_call = apiproxy_stub_map . MakeSyncCall , <EOL> ** kw ) : <EOL> """<STR_LIT>""" <EOL> kw [ '<STR_LIT>' ] = sender <EOL> kw [ '<STR_LIT:to>' ] = to <EOL> kw [ '<STR_LIT>' ] = subject <EOL> kw [ '<STR_LIT:body>' ] = body <EOL> message = EmailMessage ( ** kw ) <EOL> message . send ( make_sync_call = make_sync_call ) <EOL> SendMail = send_mail <EOL> def send_mail_to_admins ( sender , <EOL> subject , <EOL> body , <EOL> make_sync_call = apiproxy_stub_map . MakeSyncCall , <EOL> ** kw ) : <EOL> """<STR_LIT>""" <EOL> kw [ '<STR_LIT>' ] = sender <EOL> kw [ '<STR_LIT>' ] = subject <EOL> kw [ '<STR_LIT:body>' ] = body <EOL> message = AdminEmailMessage ( ** kw ) <EOL> message . send ( make_sync_call = make_sync_call ) <EOL> SendMailToAdmins = send_mail_to_admins <EOL> def _GetMimeType ( file_name ) : <EOL> """<STR_LIT>""" <EOL> extension_index = file_name . rfind ( '<STR_LIT:.>' ) <EOL> if extension_index == - <NUM_LIT:1> : <EOL> extension = '<STR_LIT>' <EOL> else : <EOL> extension = file_name [ extension_index + <NUM_LIT:1> : ] . lower ( ) <EOL> if extension in EXTENSION_BLACKLIST : <EOL> raise InvalidAttachmentTypeError ( <EOL> '<STR_LIT>' % extension ) <EOL> mime_type = EXTENSION_MIME_MAP . get ( extension , None ) <EOL> if mime_type is None : <EOL> mime_type = '<STR_LIT>' <EOL> return mime_type <EOL> def _GuessCharset ( text ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> text . decode ( '<STR_LIT>' ) <EOL> return '<STR_LIT>' <EOL> except UnicodeDecodeError : <EOL> return '<STR_LIT:utf-8>' <EOL> def _I18nHeader ( text ) : <EOL> """<STR_LIT>""" <EOL> charset = _GuessCharset ( text ) <EOL> return email . header . Header ( text , charset , maxlinelen = <NUM_LIT> ) <EOL> def mail_message_to_mime_message ( protocol_message ) : <EOL> """<STR_LIT>""" <EOL> parts = [ ] <EOL> if protocol_message . has_textbody ( ) : <EOL> parts . append ( MIMEText . MIMEText ( <EOL> protocol_message . textbody ( ) , <EOL> _charset = _GuessCharset ( protocol_message . textbody ( ) ) ) ) <EOL> if protocol_message . has_htmlbody ( ) : <EOL> parts . append ( MIMEText . MIMEText ( <EOL> protocol_message . htmlbody ( ) , _subtype = '<STR_LIT:html>' , <EOL> _charset = _GuessCharset ( protocol_message . htmlbody ( ) ) ) ) <EOL> if len ( parts ) == <NUM_LIT:1> : <EOL> payload = parts <EOL> else : <EOL> payload = [ MIMEMultipart . MIMEMultipart ( '<STR_LIT>' , _subparts = parts ) ] <EOL> result = MIMEMultipart . MIMEMultipart ( _subparts = payload ) <EOL> for attachment in protocol_message . attachment_list ( ) : <EOL> file_name = attachment . filename ( ) <EOL> mime_type = _GetMimeType ( file_name ) <EOL> maintype , subtype = mime_type . split ( '<STR_LIT:/>' ) <EOL> mime_attachment = MIMEBase . MIMEBase ( maintype , subtype ) <EOL> mime_attachment . add_header ( '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> filename = attachment . filename ( ) ) <EOL> mime_attachment . set_payload ( attachment . data ( ) ) <EOL> if attachment . has_contentid ( ) : <EOL> mime_attachment [ '<STR_LIT>' ] = attachment . contentid ( ) <EOL> result . attach ( mime_attachment ) <EOL> if protocol_message . to_size ( ) : <EOL> result [ '<STR_LIT>' ] = _I18nHeader ( '<STR_LIT:U+002CU+0020>' . join ( protocol_message . to_list ( ) ) ) <EOL> if protocol_message . cc_size ( ) : <EOL> result [ '<STR_LIT>' ] = _I18nHeader ( '<STR_LIT:U+002CU+0020>' . join ( protocol_message . cc_list ( ) ) ) <EOL> if protocol_message . bcc_size ( ) : <EOL> result [ '<STR_LIT>' ] = _I18nHeader ( '<STR_LIT:U+002CU+0020>' . join ( protocol_message . bcc_list ( ) ) ) <EOL> result [ '<STR_LIT>' ] = _I18nHeader ( protocol_message . sender ( ) ) <EOL> result [ '<STR_LIT>' ] = _I18nHeader ( protocol_message . replyto ( ) ) <EOL> result [ '<STR_LIT>' ] = _I18nHeader ( protocol_message . subject ( ) ) <EOL> for header in protocol_message . header_list ( ) : <EOL> result [ header . name ( ) ] = _I18nHeader ( header . value ( ) ) <EOL> return result <EOL> MailMessageToMIMEMessage = mail_message_to_mime_message <EOL> def _to_str ( value ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( value , unicode ) : <EOL> return value . encode ( '<STR_LIT:utf-8>' ) <EOL> return value <EOL> def _decode_and_join_header ( header , separator = u'<STR_LIT:U+0020>' ) : <EOL> """<STR_LIT>""" <EOL> if not header : <EOL> return header <EOL> return separator . join ( s . decode ( charset or '<STR_LIT>' , '<STR_LIT:replace>' ) <EOL> for s , charset in email . header . decode_header ( header ) ) <EOL> def _decode_address_list_field ( address_list ) : <EOL> """<STR_LIT>""" <EOL> if not address_list : <EOL> return None <EOL> if len ( address_list ) == <NUM_LIT:1> : <EOL> return _decode_and_join_header ( address_list [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return map ( _decode_and_join_header , address_list ) <EOL> def wrapping ( wrapped ) : <EOL> def wrapping_wrapper ( wrapper ) : <EOL> try : <EOL> wrapper . __wrapped__ = wrapped <EOL> wrapper . __name__ = wrapped . __name__ <EOL> wrapper . __doc__ = wrapped . __doc__ <EOL> wrapper . __dict__ . update ( wrapped . __dict__ ) <EOL> except Exception : <EOL> pass <EOL> return wrapper <EOL> return wrapping_wrapper <EOL> def _positional ( max_pos_args ) : <EOL> """<STR_LIT>""" <EOL> def positional_decorator ( wrapped ) : <EOL> @ wrapping ( wrapped ) <EOL> def positional_wrapper ( * args , ** kwds ) : <EOL> if len ( args ) > max_pos_args : <EOL> plural_s = '<STR_LIT>' <EOL> if max_pos_args != <NUM_LIT:1> : <EOL> plural_s = '<STR_LIT:s>' <EOL> raise TypeError ( <EOL> '<STR_LIT>' % <EOL> ( wrapped . __name__ , max_pos_args , plural_s , len ( args ) ) ) <EOL> return wrapped ( * args , ** kwds ) <EOL> return positional_wrapper <EOL> return positional_decorator <EOL> class Attachment ( object ) : <EOL> """<STR_LIT>""" <EOL> @ _positional ( <NUM_LIT:3> ) <EOL> def __init__ ( self , filename , payload , content_id = None ) : <EOL> """<STR_LIT>""" <EOL> self . filename = filename <EOL> self . payload = payload <EOL> self . content_id = content_id <EOL> def __eq__ ( self , other ) : <EOL> self_tuple = ( self . filename , self . payload , self . content_id ) <EOL> if isinstance ( other , Attachment ) : <EOL> other_tuple = ( other . filename , other . payload , other . content_id ) <EOL> elif not hasattr ( other , '<STR_LIT>' ) : <EOL> return NotImplemented <EOL> elif len ( other ) == <NUM_LIT:2> : <EOL> other_tuple = other + ( None , ) <EOL> elif len ( other ) == <NUM_LIT:3> : <EOL> other_tuple = other <EOL> else : <EOL> return NotImplemented <EOL> return self_tuple == other_tuple <EOL> def __hash__ ( self ) : <EOL> if self . content_id : <EOL> return hash ( ( self . filename , self . payload , self . content_id ) ) <EOL> else : <EOL> return hash ( ( self . filename , self . payload ) ) <EOL> def __ne__ ( self , other ) : <EOL> return not self == other <EOL> def __iter__ ( self ) : <EOL> return iter ( ( self . filename , self . payload ) ) <EOL> def __getitem__ ( self , i ) : <EOL> return tuple ( iter ( self ) ) [ i ] <EOL> def __contains__ ( self , val ) : <EOL> return val in ( self . filename , self . payload ) <EOL> def __len__ ( self ) : <EOL> return <NUM_LIT:2> <EOL> class EncodedPayload ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , payload , charset = None , encoding = None ) : <EOL> """<STR_LIT>""" <EOL> self . payload = payload <EOL> self . charset = charset <EOL> self . encoding = encoding <EOL> def decode ( self ) : <EOL> """<STR_LIT>""" <EOL> payload = self . payload <EOL> if self . encoding and self . encoding . lower ( ) not in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> try : <EOL> payload = payload . decode ( self . encoding ) <EOL> except LookupError : <EOL> raise UnknownEncodingError ( '<STR_LIT>' % self . encoding ) <EOL> except ( Exception , Error ) , e : <EOL> raise PayloadEncodingError ( '<STR_LIT>' % e ) <EOL> if self . charset and str ( self . charset ) . lower ( ) != '<STR_LIT>' : <EOL> try : <EOL> payload = payload . decode ( str ( self . charset ) ) <EOL> except LookupError : <EOL> raise UnknownCharsetError ( '<STR_LIT>' % self . charset ) <EOL> except ( Exception , Error ) , e : <EOL> raise PayloadEncodingError ( '<STR_LIT>' % e ) <EOL> return payload <EOL> def __eq__ ( self , other ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( other , EncodedPayload ) : <EOL> return ( self . payload == other . payload and <EOL> self . charset == other . charset and <EOL> self . encoding == other . encoding ) <EOL> else : <EOL> return NotImplemented <EOL> def __hash__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return hash ( ( self . payload , self . charset , self . encoding ) ) <EOL> def copy_to ( self , mime_message ) : <EOL> """<STR_LIT>""" <EOL> if self . encoding : <EOL> mime_message [ '<STR_LIT>' ] = self . encoding <EOL> mime_message . set_payload ( self . payload , self . charset ) <EOL> def to_mime_message ( self ) : <EOL> """<STR_LIT>""" <EOL> mime_message = email . Message . Message ( ) <EOL> self . copy_to ( mime_message ) <EOL> return mime_message <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return str ( self . to_mime_message ( ) ) <EOL> def __repr__ ( self ) : <EOL> """<STR_LIT>""" <EOL> result = '<STR_LIT>' % hash ( self . payload ) <EOL> if self . charset : <EOL> result += '<STR_LIT>' % self . charset <EOL> if self . encoding : <EOL> result += '<STR_LIT>' % self . encoding <EOL> return result + '<STR_LIT:>>' <EOL> class _EmailMessageBase ( object ) : <EOL> """<STR_LIT>""" <EOL> PROPERTIES = set ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:body>' , <EOL> '<STR_LIT:html>' , <EOL> '<STR_LIT>' , <EOL> ] ) <EOL> ALLOWED_EMPTY_PROPERTIES = set ( [ <EOL> '<STR_LIT>' , <EOL> '<STR_LIT:body>' <EOL> ] ) <EOL> PROPERTIES . update ( ( '<STR_LIT:to>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def __init__ ( self , mime_message = None , ** kw ) : <EOL> """<STR_LIT>""" <EOL> if mime_message : <EOL> mime_message = _parse_mime_message ( mime_message ) <EOL> self . update_from_mime_message ( mime_message ) <EOL> self . __original = mime_message <EOL> self . initialize ( ** kw ) <EOL> @ property <EOL> def original ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __original <EOL> def initialize ( self , ** kw ) : <EOL> """<STR_LIT>""" <EOL> for name , value in kw . iteritems ( ) : <EOL> setattr ( self , name , value ) <EOL> def Initialize ( self , ** kw ) : <EOL> self . initialize ( ** kw ) <EOL> def check_initialized ( self ) : <EOL> """<STR_LIT>""" <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> raise MissingSenderError ( ) <EOL> found_body = False <EOL> try : <EOL> body = self . body <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> if isinstance ( body , EncodedPayload ) : <EOL> body . decode ( ) <EOL> found_body = True <EOL> try : <EOL> html = self . html <EOL> except AttributeError : <EOL> pass <EOL> else : <EOL> if isinstance ( html , EncodedPayload ) : <EOL> html . decode ( ) <EOL> found_body = True <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> for attachment in _attachment_sequence ( self . attachments ) : <EOL> _GetMimeType ( attachment . filename ) <EOL> if isinstance ( attachment . payload , EncodedPayload ) : <EOL> attachment . payload . decode ( ) <EOL> def CheckInitialized ( self ) : <EOL> self . check_initialized ( ) <EOL> def is_initialized ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . check_initialized ( ) <EOL> return True <EOL> except Error : <EOL> return False <EOL> def IsInitialized ( self ) : <EOL> return self . is_initialized ( ) <EOL> def ToProto ( self ) : <EOL> """<STR_LIT>""" <EOL> self . check_initialized ( ) <EOL> message = mail_service_pb . MailMessage ( ) <EOL> message . set_sender ( _to_str ( self . sender ) ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> message . set_replyto ( _to_str ( self . reply_to ) ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> message . set_subject ( _to_str ( self . subject ) ) <EOL> else : <EOL> message . set_subject ( '<STR_LIT>' ) <EOL> if hasattr ( self , '<STR_LIT:body>' ) : <EOL> body = self . body <EOL> if isinstance ( body , EncodedPayload ) : <EOL> body = body . decode ( ) <EOL> message . set_textbody ( _to_str ( body ) ) <EOL> if hasattr ( self , '<STR_LIT:html>' ) : <EOL> html = self . html <EOL> if isinstance ( html , EncodedPayload ) : <EOL> html = html . decode ( ) <EOL> message . set_htmlbody ( _to_str ( html ) ) <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> for attachment in _attachment_sequence ( self . attachments ) : <EOL> if isinstance ( attachment . payload , EncodedPayload ) : <EOL> attachment . payload = attachment . payload . decode ( ) <EOL> protoattachment = message . add_attachment ( ) <EOL> protoattachment . set_filename ( _to_str ( attachment . filename ) ) <EOL> protoattachment . set_data ( _to_str ( attachment . payload ) ) <EOL> if attachment . content_id : <EOL> protoattachment . set_contentid ( attachment . content_id ) <EOL> return message <EOL> def to_mime_message ( self ) : <EOL> """<STR_LIT>""" <EOL> return mail_message_to_mime_message ( self . ToProto ( ) ) <EOL> def ToMIMEMessage ( self ) : <EOL> return self . to_mime_message ( ) <EOL> def send ( self , make_sync_call = apiproxy_stub_map . MakeSyncCall ) : <EOL> """<STR_LIT>""" <EOL> message = self . ToProto ( ) <EOL> response = api_base_pb . VoidProto ( ) <EOL> try : <EOL> make_sync_call ( '<STR_LIT>' , self . _API_CALL , message , response ) <EOL> except apiproxy_errors . ApplicationError , e : <EOL> if e . application_error in ERROR_MAP : <EOL> raise ERROR_MAP [ e . application_error ] ( e . error_detail ) <EOL> raise e <EOL> def Send ( self , * args , ** kwds ) : <EOL> self . send ( * args , ** kwds ) <EOL> def _check_attachment ( self , attachment ) : <EOL> if not ( isinstance ( attachment . filename , basestring ) or <EOL> isinstance ( attachment . payload , basestring ) ) : <EOL> raise TypeError ( ) <EOL> def _check_attachments ( self , attachments ) : <EOL> """<STR_LIT>""" <EOL> attachments = _attachment_sequence ( attachments ) <EOL> for attachment in attachments : <EOL> self . _check_attachment ( attachment ) <EOL> def __setattr__ ( self , attr , value ) : <EOL> """<STR_LIT>""" <EOL> if not attr . startswith ( '<STR_LIT>' ) : <EOL> if attr in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> check_email_valid ( value , attr ) <EOL> if not value and not attr in self . ALLOWED_EMPTY_PROPERTIES : <EOL> raise ValueError ( '<STR_LIT>' % attr ) <EOL> if attr not in self . PROPERTIES : <EOL> raise AttributeError ( '<STR_LIT>' % attr ) <EOL> if attr == '<STR_LIT>' : <EOL> self . _check_attachments ( value ) <EOL> super ( _EmailMessageBase , self ) . __setattr__ ( attr , value ) <EOL> def _add_body ( self , content_type , payload ) : <EOL> """<STR_LIT>""" <EOL> if content_type == '<STR_LIT>' : <EOL> self . body = payload <EOL> elif content_type == '<STR_LIT>' : <EOL> self . html = payload <EOL> def _update_payload ( self , mime_message ) : <EOL> """<STR_LIT>""" <EOL> payload = mime_message . get_payload ( ) <EOL> if payload : <EOL> if mime_message . get_content_maintype ( ) == '<STR_LIT>' : <EOL> for alternative in payload : <EOL> self . _update_payload ( alternative ) <EOL> else : <EOL> filename = mime_message . get_param ( '<STR_LIT:filename>' , <EOL> header = '<STR_LIT>' ) <EOL> if filename : <EOL> filename = email . utils . collapse_rfc2231_value ( filename ) <EOL> if not filename : <EOL> filename = mime_message . get_param ( '<STR_LIT:name>' ) <EOL> payload = EncodedPayload ( payload , <EOL> ( mime_message . get_content_charset ( ) or <EOL> mime_message . get_charset ( ) ) , <EOL> mime_message [ '<STR_LIT>' ] ) <EOL> if '<STR_LIT>' in mime_message : <EOL> attachment = Attachment ( filename , <EOL> payload , <EOL> content_id = mime_message [ '<STR_LIT>' ] ) <EOL> else : <EOL> attachment = Attachment ( filename , payload ) <EOL> if filename : <EOL> try : <EOL> attachments = self . attachments <EOL> except AttributeError : <EOL> self . attachments = [ attachment ] <EOL> else : <EOL> if isinstance ( attachments [ <NUM_LIT:0> ] , basestring ) : <EOL> self . attachments = [ attachments ] <EOL> attachments = self . attachments <EOL> attachments . append ( attachment ) <EOL> else : <EOL> self . _add_body ( mime_message . get_content_type ( ) , payload ) <EOL> def update_from_mime_message ( self , mime_message ) : <EOL> """<STR_LIT>""" <EOL> mime_message = _parse_mime_message ( mime_message ) <EOL> sender = _decode_and_join_header ( mime_message [ '<STR_LIT>' ] ) <EOL> if sender : <EOL> self . sender = sender <EOL> reply_to = _decode_and_join_header ( mime_message [ '<STR_LIT>' ] ) <EOL> if reply_to : <EOL> self . reply_to = reply_to <EOL> subject = _decode_and_join_header ( mime_message [ '<STR_LIT>' ] , separator = u'<STR_LIT>' ) <EOL> if subject : <EOL> self . subject = subject <EOL> self . _update_payload ( mime_message ) <EOL> def bodies ( self , content_type = None ) : <EOL> """<STR_LIT>""" <EOL> if ( not content_type or <EOL> content_type == '<STR_LIT:text>' or <EOL> content_type == '<STR_LIT>' ) : <EOL> try : <EOL> yield '<STR_LIT>' , self . html <EOL> except AttributeError : <EOL> pass <EOL> if ( not content_type or <EOL> content_type == '<STR_LIT:text>' or <EOL> content_type == '<STR_LIT>' ) : <EOL> try : <EOL> yield '<STR_LIT>' , self . body <EOL> except AttributeError : <EOL> pass <EOL> class EmailMessage ( _EmailMessageBase ) : <EOL> """<STR_LIT>""" <EOL> _API_CALL = '<STR_LIT>' <EOL> PROPERTIES = set ( _EmailMessageBase . PROPERTIES | set ( ( '<STR_LIT>' , ) ) ) <EOL> def check_initialized ( self ) : <EOL> """<STR_LIT>""" <EOL> if ( not hasattr ( self , '<STR_LIT:to>' ) and <EOL> not hasattr ( self , '<STR_LIT>' ) and <EOL> not hasattr ( self , '<STR_LIT>' ) ) : <EOL> raise MissingRecipientsError ( ) <EOL> super ( EmailMessage , self ) . check_initialized ( ) <EOL> def CheckInitialized ( self ) : <EOL> self . check_initialized ( ) <EOL> def ToProto ( self ) : <EOL> """<STR_LIT>""" <EOL> message = super ( EmailMessage , self ) . ToProto ( ) <EOL> for attribute , adder in ( ( '<STR_LIT:to>' , message . add_to ) , <EOL> ( '<STR_LIT>' , message . add_cc ) , <EOL> ( '<STR_LIT>' , message . add_bcc ) ) : <EOL> if hasattr ( self , attribute ) : <EOL> for address in _email_sequence ( getattr ( self , attribute ) ) : <EOL> adder ( _to_str ( address ) ) <EOL> for name , value in getattr ( self , '<STR_LIT>' , { } ) . iteritems ( ) : <EOL> header = message . add_header ( ) <EOL> header . set_name ( name ) <EOL> header . set_value ( _to_str ( value ) ) <EOL> return message <EOL> def __setattr__ ( self , attr , value ) : <EOL> """<STR_LIT>""" <EOL> if attr in [ '<STR_LIT:to>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if isinstance ( value , basestring ) : <EOL> if value == '<STR_LIT>' and getattr ( self , '<STR_LIT>' , False ) : <EOL> return <EOL> check_email_valid ( value , attr ) <EOL> else : <EOL> for address in value : <EOL> check_email_valid ( address , attr ) <EOL> elif attr == '<STR_LIT>' : <EOL> check_headers_valid ( value ) <EOL> super ( EmailMessage , self ) . __setattr__ ( attr , value ) <EOL> def update_from_mime_message ( self , mime_message ) : <EOL> """<STR_LIT>""" <EOL> mime_message = _parse_mime_message ( mime_message ) <EOL> super ( EmailMessage , self ) . update_from_mime_message ( mime_message ) <EOL> to = _decode_address_list_field ( mime_message . get_all ( '<STR_LIT:to>' ) ) <EOL> if to : <EOL> self . to = to <EOL> cc = _decode_address_list_field ( mime_message . get_all ( '<STR_LIT>' ) ) <EOL> if cc : <EOL> self . cc = cc <EOL> bcc = _decode_address_list_field ( mime_message . get_all ( '<STR_LIT>' ) ) <EOL> if bcc : <EOL> self . bcc = bcc <EOL> class AdminEmailMessage ( _EmailMessageBase ) : <EOL> """<STR_LIT>""" <EOL> _API_CALL = '<STR_LIT>' <EOL> __UNUSED_PROPERTIES = set ( ( '<STR_LIT:to>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> def __setattr__ ( self , attr , value ) : <EOL> if attr in self . __UNUSED_PROPERTIES : <EOL> logging . warning ( '<STR_LIT>' <EOL> '<STR_LIT>' , attr ) <EOL> super ( AdminEmailMessage , self ) . __setattr__ ( attr , value ) <EOL> class InboundEmailMessage ( EmailMessage ) : <EOL> """<STR_LIT>""" <EOL> __HEADER_PROPERTIES = { '<STR_LIT:date>' : '<STR_LIT:date>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> PROPERTIES = frozenset ( _EmailMessageBase . PROPERTIES | <EOL> set ( ( '<STR_LIT>' , ) ) | <EOL> set ( __HEADER_PROPERTIES . iterkeys ( ) ) ) <EOL> ALLOW_BLANK_EMAIL = True <EOL> def update_from_mime_message ( self , mime_message ) : <EOL> """<STR_LIT>""" <EOL> mime_message = _parse_mime_message ( mime_message ) <EOL> super ( InboundEmailMessage , self ) . update_from_mime_message ( mime_message ) <EOL> for property , header in InboundEmailMessage . __HEADER_PROPERTIES . iteritems ( ) : <EOL> value = mime_message [ header ] <EOL> if value : <EOL> setattr ( self , property , value ) <EOL> def _add_body ( self , content_type , payload ) : <EOL> """<STR_LIT>""" <EOL> if ( content_type == '<STR_LIT>' and not hasattr ( self , '<STR_LIT:body>' ) or <EOL> content_type == '<STR_LIT>' and not hasattr ( self , '<STR_LIT:html>' ) ) : <EOL> super ( InboundEmailMessage , self ) . _add_body ( content_type , payload ) <EOL> else : <EOL> try : <EOL> alternate_bodies = self . alternate_bodies <EOL> except AttributeError : <EOL> alternate_bodies = self . alternate_bodies = [ ( content_type , payload ) ] <EOL> else : <EOL> alternate_bodies . append ( ( content_type , payload ) ) <EOL> def bodies ( self , content_type = None ) : <EOL> """<STR_LIT>""" <EOL> main_bodies = super ( InboundEmailMessage , self ) . bodies ( content_type ) <EOL> for payload_type , payload in main_bodies : <EOL> yield payload_type , payload <EOL> partial_type = bool ( content_type and content_type . find ( '<STR_LIT:/>' ) < <NUM_LIT:0> ) <EOL> try : <EOL> for payload_type , payload in self . alternate_bodies : <EOL> if content_type : <EOL> if partial_type : <EOL> match_type = payload_type . split ( '<STR_LIT:/>' ) [ <NUM_LIT:0> ] <EOL> else : <EOL> match_type = payload_type <EOL> match = match_type == content_type <EOL> else : <EOL> match = True <EOL> if match : <EOL> yield payload_type , payload <EOL> except AttributeError : <EOL> pass <EOL> def to_mime_message ( self ) : <EOL> """<STR_LIT>""" <EOL> mime_message = super ( InboundEmailMessage , self ) . to_mime_message ( ) <EOL> for property , header in InboundEmailMessage . __HEADER_PROPERTIES . iteritems ( ) : <EOL> try : <EOL> mime_message [ header ] = getattr ( self , property ) <EOL> except AttributeError : <EOL> pass <EOL> return mime_message <EOL> Parser . Parser </s>
