def alt_to_text_message(original_message): ''' Change a MIME message into a plain text Message. >>> # Test extreme cases >>> alt_to_text_message(None) == None True ''' text_message = None try: if original_message: content_type = original_message.get_content_type() charset, __ = get_charset(original_message) log_message("message content type is {}".format(content_type)) if content_type == mime_constants.MULTIPART_ALT_TYPE: plain_text = '' for part in original_message.walk(): if part.get_content_type( ) == mime_constants.TEXT_PLAIN_TYPE: if len(plain_text) > 0: plain_text += '\n\n' plain_text += part.get_payload() content_type = part.get_content_type() charset, __ = get_charset(part) log_message( "part content type is {}".format(content_type)) log_message("part charset is {}".format(charset)) if len(plain_text) > 0: try: # create a fresh message with the same headers # we'll change some headers and all of the body text_message = MIMEText(plain_text, mime_constants.PLAIN_SUB_TYPE, charset) for key in original_message.keys(): if key != mime_constants.CONTENT_TYPE_KEYWORD: text_message.__setitem__( key, original_message.get(key)) text_message.set_payload(plain_text, charset) except MessageException as message_exception: record_exception(message=message_exception) if type(text_message) == Message and DEBUGGING: log_message("New plain text message:\n" + text_message.as_string()) except IOError as io_exception: record_exception(message=io_exception) except MessageException as message_exception: record_exception(message=message_exception) return text_message
def alt_to_text_message(original_message): ''' Change a MIME message into a plain text Message. >>> # Test extreme cases >>> alt_to_text_message(None) == None True ''' text_message = None try: if original_message: content_type = original_message.get_content_type() charset, __ = get_charset(original_message) log_message("message content type is {}".format(content_type)) if content_type == mime_constants.MULTIPART_ALT_TYPE: plain_text = '' for part in original_message.walk(): if part.get_content_type() == mime_constants.TEXT_PLAIN_TYPE: if len(plain_text) > 0: plain_text += '\n\n' plain_text += part.get_payload() content_type = part.get_content_type() charset, __ = get_charset(part) log_message("part content type is {}".format(content_type)) log_message("part charset is {}".format(charset)) if len(plain_text) > 0: try: # create a fresh message with the same headers # we'll change some headers and all of the body text_message = MIMEText(plain_text, mime_constants.PLAIN_SUB_TYPE, charset) for key in original_message.keys(): if key != mime_constants.CONTENT_TYPE_KEYWORD: text_message.__setitem__(key, original_message.get(key)) text_message.set_payload(plain_text, charset) except MessageException as message_exception: record_exception(message=message_exception) if type(text_message) == Message and DEBUGGING: log_message("New plain text message:\n" + text_message.as_string()) except IOError as io_exception: record_exception(message=io_exception) except MessageException as message_exception: record_exception(message=message_exception) return text_message
def __setitem__(self, name, val): name, val = forbid_multi_line_headers(name, val) MIMEText.__setitem__(self, name, val)
def __setitem__(self, name, val): name, val = forbid_multi_line_headers(name, val, self.encoding) MIMEText.__setitem__(self, name, val)
def __setitem__(self, name, val): MIMEText.__setitem__(self, name, val)
def __setitem__(self, name, val): name, val = forbid_multi_line_headers(name, val, self.encoding) MIMEText.__setitem__(self, name, smart_unicode(val))
from __future__ import unicode_literals