#26802: Sending mails with attachment results in 'bytes' object has no attribute
'encode'
-----------------------------+--------------------
Reporter: Brandl | Owner: nobody
Type: Bug | Status: new
Component: Core (Mail) | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------+--------------------
When trying to send a EMail with attachment, it always failed with the
following Exception:
{{{
'bytes' object has no attribute 'encode'
}}}
At first I thought this is a bug in django-post-office or a duplicate of
this bug:
https://code.djangoproject.com/ticket/24623
But I'm running the newest Version of Django(1.9.7) and django-
post_office(2.0.7) on Python Python 3.5.1
Here is the trace:
{{{
File "/home/me/Projects/MyProject/.env/lib/python3.5/site-
packages/post_office/models.py", line 119, in dispatch
self.email_message(connection=connection).send()
File "/home/me/Projects/MyProject/.env/lib/python3.5/site-
packages/django/core/mail/message.py", line 292, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/me/Projects/MyProject/.env/lib/python3.5/site-
packages/django/core/mail/backends/smtp.py", line 107, in send_messages
sent = self._send(message)
File "/home/me/Projects/MyProject/.env/lib/python3.5/site-
packages/django/core/mail/backends/smtp.py", line 121, in _send
message = email_message.message()
File "/home/me/Projects/MyProject/.env/lib/python3.5/site-
packages/django/core/mail/message.py", line 256, in message
msg = self._create_message(msg)
File "/home/me/Projects/MyProject/.env/lib/python3.5/site-
packages/django/core/mail/message.py", line 344, in _create_message
return self._create_attachments(msg)
File "/home/me/Projects/MyProject/.env/lib/python3.5/site-
packages/django/core/mail/message.py", line 357, in _create_attachments
msg.attach(self._create_attachment(*attachment))
File "/home/me/Projects/MyProject/.env/lib/python3.5/site-
packages/django/core/mail/message.py", line 399, in _create_attachment
attachment = self._create_mime_attachment(content, mimetype)
File "/home/me/Projects/MyProject/.env/lib/python3.5/site-
packages/django/core/mail/message.py", line 370, in
_create_mime_attachment
attachment = SafeMIMEText(content, subtype, encoding)
File "/home/me/Projects/MyProject/.env/lib/python3.5/site-
packages/django/core/mail/message.py", line 171, in __init__
MIMEText.__init__(self, _text, _subtype, None)
File "/usr/lib64/python3.5/email/mime/text.py", line 34, in __init__
_text.encode('us-ascii')
AttributeError: 'bytes' object has no attribute 'encode'
}}}
As it turns out:
{{{
if _charset == 'utf-8':
# Unfortunately, Python < 3.5 doesn't support setting a
Charset instance
# as MIMEText init parameter
(http://bugs.python.org/issue16324).
# We do it manually and trigger re-encoding of the payload.
MIMEText.__init__(self, _text, _subtype, 'utf-8')
}}}
instead of
{{{
MIMEText.__init__(self, _text, _subtype, None)
}}}
fixes the bug, but I'm not sure if that's a clean solution.
--
Ticket URL: <https://code.djangoproject.com/ticket/26802>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/049.ba0c221775e186071dfcf514d18d2750%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.