# HG changeset patch # User Oliver Cope # Date 1570786004 -7200 # Fri Oct 11 11:26:44 2019 +0200 # Node ID feebb19f70b4b3226a4e097a55142de979522870 # Parent d957442de7fbcadadb4c880f4cab08a6aa8cbd54 Add code for string decoding when reading attachment from a file diff --git a/mailtools/utils.py b/mailtools/utils.py --- a/mailtools/utils.py +++ b/mailtools/utils.py @@ -100,13 +100,18 @@ 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)