1
0
Fork 0

Always decode headers into unicode objects before assigning to model; fixes #12.

This commit is contained in:
Adam Coddington 2014-04-22 15:49:49 -07:00
parent d3a766ffd6
commit 79e189fb22
5 changed files with 36 additions and 15 deletions

View file

@ -10,16 +10,19 @@ logger = logging.getLogger(__name__)
DEFAULT_CHARSET = getattr(
settings,
'DJANGO_MAILBOX_DEFAULT_CHARSET',
'ascii',
'iso8859-1',
)
def decode_header(header):
def convert_header_to_unicode(header):
try:
return ''.join(
[
unicode(t[0], t[1] or DEFAULT_CHARSET)
for t in email.header.decode_header(header)
(
bytestr.decode(encoding, 'REPLACE')
if encoding
else bytestr.decode(DEFAULT_CHARSET, 'REPLACE')
) for bytestr, encoding in email.header.decode_header(header)
]
)
except UnicodeDecodeError: