Skip to content

Commit

Permalink
Fix utf8 encoding with a text/plain mailcap entry.
Browse files Browse the repository at this point in the history
Without this change, temporary file in db.utils.render_part reads
"Liebe Grüße!".

This seems to essentially revert 777823f,
the reasoning of which I don't yet follow.

This may resolve the issue in pazz#1522.
  • Loading branch information
ryneeverett committed Jun 14, 2020
1 parent 8343bff commit 7dfa415
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
6 changes: 1 addition & 5 deletions alot/db/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,11 +429,7 @@ def remove_cte(part, as_string=False):
# decoding into a str is done at the end if requested
elif '8bit' in cte:
logging.debug('assuming Content-Transfer-Encoding: 8bit')
# Python's mail library may decode 8bit as raw-unicode-escape, so
# we need to encode that back to bytes so we can decode it using
# the correct encoding, or it might not, in which case assume that
# the str representation we got is correct.
bp = payload.encode('raw-unicode-escape')
bp = payload.encode('utf8')

elif 'quoted-printable' in cte:
logging.debug('assuming Content-Transfer-Encoding: quoted-printable')
Expand Down
18 changes: 18 additions & 0 deletions tests/db/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,24 @@ def test_simple_utf8_file(self):

self.assertEqual(actual, expected)

@mock.patch('alot.db.utils.settings.mailcap_find_match',
mock.Mock(return_value=(
None, {'view': 'sed "s/!/?/"'})))
def test_utf8_plaintext_mailcap(self):
"""
Handle unicode correctly in the presence of a text/plain mailcap entry.
https://github.com/pazz/alot/issues/1522
"""
mail = email.message_from_binary_file(
open('tests/static/mail/utf8.eml', 'rb'),
_class=email.message.EmailMessage)
body_part = utils.get_body_part(mail)
actual = utils.extract_body_part(body_part)
expected = "Liebe Grüße?\n"

self.assertEqual(actual, expected)

@mock.patch('alot.db.utils.settings.get', mock.Mock(return_value=True))
@mock.patch('alot.db.utils.settings.mailcap_find_match',
mock.Mock(return_value=(
Expand Down

0 comments on commit 7dfa415

Please sign in to comment.