summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2021-05-15 22:30:18 +0200
committerAnton Khirnov <anton@khirnov.net>2021-05-15 22:30:18 +0200
commit1954560bde385757f541cb0310530c95d6a7fe3d (patch)
tree49878e60f67ad28d7164c72f34b1433c3282bd17
parentedaf6f0603718e92a19dee9711e2c3ea854ccf21 (diff)
db/message: make sure attachment data is always a byte sequence
Encode text strings to UTF-8 and set the charset parameter accordingly.
-rw-r--r--alot/db/message.py12
1 files changed, 10 insertions, 2 deletions
diff --git a/alot/db/message.py b/alot/db/message.py
index 62169356..a6d67dfd 100644
--- a/alot/db/message.py
+++ b/alot/db/message.py
@@ -140,9 +140,17 @@ class _MimeTree:
cd = part.get_content_disposition()
fn = part.get_filename()
if cd == 'attachment' or fn is not None:
- data = part.get_content()
+ data = part.get_content()
+ params = part.get_params()
+
+ # make sure data is always a byte-sequence
+ if isinstance(data, str):
+ data = data.encode('utf-8', errors = 'replace')
+ params = [p for p in params if p[0] != 'charset'] + \
+ [('charset', 'utf-8')]
+
self.attachment = Attachment(data, self.content_type,
- fn, part.get_params())
+ fn, params)
def __str__(self):
return 'MimePart(%s)' % self.content_type