diff --git a/djrill/mail/backends/djrill.py b/djrill/mail/backends/djrill.py index 18ee6a8..6ddc7ce 100644 --- a/djrill/mail/backends/djrill.py +++ b/djrill/mail/backends/djrill.py @@ -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, diff --git a/djrill/tests/test_mandrill_send.py b/djrill/tests/test_mandrill_send.py index af88a0d..772844c 100644 --- a/djrill/tests/test_mandrill_send.py +++ b/djrill/tests/test_mandrill_send.py @@ -180,18 +180,17 @@ class DjrillBackendTests(DjrillBackendMockAPITestCase): self.assertFalse('images' in data['message']) def test_unicode_attachment_correctly_decoded(self): - unicode_attachment = [ - ('before_html.html', u'

\u2019

', 'text/html'), - ] - email = mail.EmailMessage( + msg = mail.EmailMessage( subject='Subject', body='Body goes here', from_email='from@example.com', to=['to1@example.com'], - attachments=unicode_attachment, ) + # Slight modification from the Django unicode docs: + # http://django.readthedocs.org/en/latest/ref/unicode.html#email + msg.attach("Une pièce jointe.html", u'

\u2019

', mimetype='text/html') - email.send() + msg.send() data = self.get_api_call_data() attachments = data['message']['attachments']