﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
26802	Sending mails with attachment results in 'bytes' object has no attribute 'encode'	Brandl	nobody	"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."	Bug	closed	Core (Mail)	1.9	Normal	fixed			Ready for checkin	1	0	0	0	0	0
