From: Oleg Broytman Date: Wed, 3 Aug 2016 15:22:45 +0000 (+0300) Subject: Convert body to the current charset for mailcap filter X-Git-Tag: 2.7.0~23 X-Git-Url: https://git.phdru.name/?p=mimedecode.git;a=commitdiff_plain;h=ea094df666c1e2aa626d817d29dfd77acc52c5ea Convert body to the current charset for mailcap filter Convert text body from mail charset to the current locale's charset before passing it to the mailcap filter. --- diff --git a/mimedecode.py b/mimedecode.py index 25d3db0..e7381f7 100755 --- a/mimedecode.py +++ b/mimedecode.py @@ -218,6 +218,10 @@ def decode_body(msg, s): caps = mailcap.getcaps() content_type = msg.get_content_type() + if content_type.startswith('text/'): + charset = msg.get_content_charset() + else: + charset = None filename = tempfile.mktemp() command = None @@ -235,6 +239,8 @@ def decode_body(msg, s): return s outfile = open(filename, 'wb') + if charset and isinstance(s, bytes): + s = s.decode(charset, "replace") if not isinstance(s, bytes): s = s.encode(g.default_encoding, "replace") outfile.write(s)