mirror of
https://github.com/coddingtonbear/django-mailbox.git
synced 2026-07-09 22:38:19 +02:00
Fixing header decoding such that it works properly under both Python 3.x and 2.x.
This commit is contained in:
parent
ef42c319e9
commit
a2cf80ef99
4 changed files with 18 additions and 6 deletions
|
|
@ -257,7 +257,7 @@ class Mailbox(models.Model):
|
|||
msg = Message()
|
||||
msg.mailbox = self
|
||||
if 'subject' in message:
|
||||
msg.subject = convert_header_to_unicode(message['subject'][0:255])
|
||||
msg.subject = convert_header_to_unicode(message['subject'])[0:255]
|
||||
if 'message-id' in message:
|
||||
msg.message_id = message['message-id'][0:255]
|
||||
if 'from' in message:
|
||||
|
|
|
|||
|
|
@ -191,7 +191,12 @@ class TestProcessEmail(EmailMessageTestCase):
|
|||
actual_subject = msg.subject
|
||||
self.assertEqual(actual_subject, expected_subject)
|
||||
|
||||
expected_from = six.u('test test<mr.test32@mail.ru>')
|
||||
if six.PY3:
|
||||
# There were various bugfixes in Py3k's email module,
|
||||
# this is apparently one of them.
|
||||
expected_from = six.u('test test <mr.test32@mail.ru>')
|
||||
else:
|
||||
expected_from = six.u('test test<mr.test32@mail.ru>')
|
||||
actual_from = msg.from_header
|
||||
|
||||
self.assertEqual(expected_from, actual_from)
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
import email.header
|
||||
import logging
|
||||
|
||||
import six
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
|
||||
|
|
@ -15,13 +17,18 @@ DEFAULT_CHARSET = getattr(
|
|||
|
||||
|
||||
def convert_header_to_unicode(header):
|
||||
def _decode(value, encoding):
|
||||
if isinstance(value, six.text_type):
|
||||
return value
|
||||
if not encoding or encoding == 'unknown-8bit':
|
||||
encoding = DEFAULT_CHARSET
|
||||
return value.decode(encoding, 'REPLACE')
|
||||
|
||||
try:
|
||||
return ''.join(
|
||||
[
|
||||
(
|
||||
bytestr.decode(encoding, 'REPLACE')
|
||||
if encoding
|
||||
else bytestr.decode(DEFAULT_CHARSET, 'REPLACE')
|
||||
_decode(bytestr, encoding)
|
||||
) for bytestr, encoding in email.header.decode_header(header)
|
||||
]
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue