feebb19f70b4 — Oliver Cope 5 years ago
Add code for string decoding when reading attachment from a file
1 files changed, 7 insertions(+), 2 deletions(-)

M mailtools/utils.py
M mailtools/utils.py +7 -2
@@ 100,13 100,18 @@ def attachment_from_string(data, filenam
         if content_type is None or encoding is not None:
             content_type = "application/octet-stream"
 
+    if isinstance(data, bytes):
+        data_str = lambda: data.decode(encoding or "UTF-8")
+    else:
+        data_str = lambda: data
+
     maintype, subtype = content_type.split("/", 1)
 
     if maintype == "text":
-        attach = MIMEText(data, _subtype=subtype)
+        attach = MIMEText(data_str(), _subtype=subtype)
 
     elif maintype == "message":
-        attach = message_from_string(data)
+        attach = message_from_string(data_str())
 
     elif maintype == "image":
         attach = MIMEImage(data, _subtype=subtype)