Skip to content

Commit 3739580

Browse files
ryneeverettpazz
authored andcommitted
Fix utf8 encoding with a text/plain mailcap entry.
This seems to essentially revert 777823f, the reasoning of which I don't yet follow. This may resolve the issue in #1522.
1 parent 8343bff commit 3739580

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

alot/db/utils.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -429,11 +429,7 @@ def remove_cte(part, as_string=False):
429429
# decoding into a str is done at the end if requested
430430
elif '8bit' in cte:
431431
logging.debug('assuming Content-Transfer-Encoding: 8bit')
432-
# Python's mail library may decode 8bit as raw-unicode-escape, so
433-
# we need to encode that back to bytes so we can decode it using
434-
# the correct encoding, or it might not, in which case assume that
435-
# the str representation we got is correct.
436-
bp = payload.encode('raw-unicode-escape')
432+
bp = payload.encode('utf8')
437433

438434
elif 'quoted-printable' in cte:
439435
logging.debug('assuming Content-Transfer-Encoding: quoted-printable')

tests/db/test_utils.py

+18
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,24 @@ def test_simple_utf8_file(self):
748748

749749
self.assertEqual(actual, expected)
750750

751+
@mock.patch('alot.db.utils.settings.mailcap_find_match',
752+
mock.Mock(return_value=(
753+
None, {'view': 'sed "s/!/?/"'})))
754+
def test_utf8_plaintext_mailcap(self):
755+
"""
756+
Handle unicode correctly in the presence of a text/plain mailcap entry.
757+
758+
https://github.com/pazz/alot/issues/1522
759+
"""
760+
mail = email.message_from_binary_file(
761+
open('tests/static/mail/utf8.eml', 'rb'),
762+
_class=email.message.EmailMessage)
763+
body_part = utils.get_body_part(mail)
764+
actual = utils.extract_body_part(body_part)
765+
expected = "Liebe Grüße?\n"
766+
767+
self.assertEqual(actual, expected)
768+
751769
@mock.patch('alot.db.utils.settings.get', mock.Mock(return_value=True))
752770
@mock.patch('alot.db.utils.settings.mailcap_find_match',
753771
mock.Mock(return_value=(

0 commit comments

Comments
 (0)