def decodeMessageAsString(msg): """ This helper method takes Message object or string and returns string which does not contain base64 encoded parts Returns message without any encoding in parts """ if isinstance(msg, str): msg = Parser().parsestr(msg) new = deepcopy(msg) # From is utf8 encoded: '=?utf-8?q?Site_Administrator_=3C=3E?=' new.replace_header('From', decode_header(new['From'])[0][0]) new.replace_header('Subject', decode_header(new['Subject'])[0][0]) charset = Charset('utf-8') charset.header_encoding = SHORTEST charset.body_encoding = QP charset.output_charset = 'utf-8' for part in new.walk(): if part.get_content_maintype() == "multipart": continue decoded = part.get_payload(decode=1) del part['Content-Transfer-Encoding'] part.set_payload(decoded, charset) return new.as_string()
def _set_charset(self, mime): from email.Charset import Charset, QP, BASE64, SHORTEST mime_encoding = self.config.get('notification', 'mime_encoding').lower() charset = Charset() charset.input_charset = 'utf-8' charset.output_charset = 'utf-8' charset.input_codec = 'utf-8' charset.output_codec = 'utf-8' if mime_encoding == 'base64': charset.header_encoding = BASE64 charset.body_encoding = BASE64 elif mime_encoding in ('qp', 'quoted-printable'): charset.header_encoding = QP charset.body_encoding = QP elif mime_encoding == 'none': charset.header_encoding = SHORTEST charset.body_encoding = None del mime['Content-Transfer-Encoding'] mime.set_charset(charset)