Handle unicode attachment content in both python 2.X and python3.

This commit is contained in:
winhamwr
2014-04-23 16:59:48 -04:00
parent 4e0a0cca71
commit 70dc022f77
2 changed files with 15 additions and 13 deletions

View File

@@ -294,15 +294,18 @@ class DjrillBackend(BaseEmailBackend):
if mimetype is None:
mimetype = DEFAULT_ATTACHMENT_MIME_TYPE
# b64encode requires bytes, so let's convert our content.
try:
content_b64 = b64encode(content)
except TypeError:
# Python 3 b64encode requires bytes. Convert str attachment:
if isinstance(content, unicode):
# Python 2.X unicode string
content = content.encode(str_encoding)
except NameError:
# Python 3 doesn't differentiate between strings and unicode
# Convert python3 unicode str to bytes attachment:
if isinstance(content, str):
content_bytes = content.encode(str_encoding)
content_b64 = b64encode(content_bytes)
else:
raise
content = content.encode(str_encoding)
content_b64 = b64encode(content)
mandrill_attachment = {
'type': mimetype,