def decode_mail_header(value, default_charset='us-ascii'): """ Decode a header value into a unicode string. """ try: headers = decode_header(value) except email.errors.HeaderParseError: return str_decode(str_encode(value, default_charset, 'replace'), default_charset) else: for index, (text, charset) in enumerate(headers): try: logger.debug( "Mail header no. {index}: {data} encoding {charset}". format(index=index, data=str_decode(text, charset or 'utf-8', 'replace'), charset=charset)) headers[index] = str_decode(text, charset or default_charset, 'replace') except LookupError: # if the charset is unknown, force default headers[index] = str_decode(text, default_charset, 'replace') return ''.join(headers)
def decode_mail_header(value, default_charset='us-ascii'): """ Decode a header value into a unicode string. """ try: headers = decode_header(value) except email.errors.HeaderParseError: return str_decode(str_encode(value, default_charset, 'replace'), default_charset) else: for index, (text, charset) in enumerate(headers): try: headers[index] = str_decode(text, charset or default_charset, 'replace') except LookupError: # if the charset is unknown, force default headers[index] = str_decode(text, default_charset, 'replace') return ''.join(headers)
def decode_mail_header(value, default_charset='us-ascii'): """ Decode a header value into a unicode string. """ try: headers = decode_header(value) except email.errors.HeaderParseError: return str_decode(str_encode(value, default_charset, 'replace'), default_charset) else: for index, (text, charset) in enumerate(headers): logger.debug("Mail header no. {index}: {data} encoding {charset}".format( index=index, data=str_decode(text, charset or 'utf-8', 'replace'), charset=charset)) try: headers[index] = str_decode(text, charset or default_charset, 'replace') except LookupError: # if the charset is unknown, force default headers[index] = str_decode(text, default_charset, 'replace') return ''.join(headers)