brack3t / Djrill

[INACTIVE/UNMAINTAINED] Djrill is an email backend and new message class for Django users that want to take advantage of the Mandrill transactional email service from MailChimp.
BSD 3-Clause "New" or "Revised" License
319 stars 64 forks source link

`UnicodeEncodeError` with unicode attachments #65

Closed winhamwr closed 10 years ago

winhamwr commented 10 years ago

If an attachment is added that contains unicode, sending the message fails with a UnicodeEncodeError. I have a branch with a failing test:

ERROR: test_unicode_attachment_correctly_decoded (djrill.tests.test_mandrill_send.DjrillBackendTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/wes/policystat/workspace/Djrill/djrill/tests/test_mandrill_send.py", line 194, in test_unicode_attachment_correctly_decoded
    email.send()
  File "/home/wes/policystat/workspace/Djrill/Django-1.6.3-py2.7.egg/django/core/mail/message.py", line 274, in send
    return self.get_connection(fail_silently).send_messages([self])
  File "/home/wes/policystat/workspace/Djrill/djrill/mail/backends/djrill.py", line 68, in send_messages
    sent = self._send(message)
  File "/home/wes/policystat/workspace/Djrill/djrill/mail/backends/djrill.py", line 89, in _send
    self._add_attachments(message, msg_dict)
  File "/home/wes/policystat/workspace/Djrill/djrill/mail/backends/djrill.py", line 257, in _add_attachments
    att_dict, is_embedded = self._make_mandrill_attachment(attachment, str_encoding)
  File "/home/wes/policystat/workspace/Djrill/djrill/mail/backends/djrill.py", line 298, in _make_mandrill_attachment
    content_b64 = b64encode(content)
  File "/usr/lib/python2.7/base64.py", line 53, in b64encode
    encoded = binascii.b2a_base64(s)[:-1]
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 3: ordinal not in range(128)

My plan is to treat python2.X like we're currently treating python3 by first converting to bytes.

winhamwr commented 10 years ago

Hello,

I tested this fix out with both python 2.7 and python 3.3 and the test suite passes on both. I think this is the best way to handle the possible need for unicode=>bytes conversion in both versions of python, but I'm certainly open to alternatives. Please let me know if there's anything else I can help with to make this PR easier to merge.

Thanks -Wes

jlward commented 10 years ago

@winhamwr it looks like your tests are failing in python 3.2.

winhamwr commented 10 years ago

Both python 3.2 and python 3.3 are working, now.

kbabris commented 10 years ago

Hello, I see the last comment was made on Apr. 24 and no activity since. The problem is still there. Will this fix be merged?

kylegibson commented 10 years ago

I'm :+1: on getting this merged in

medmunds commented 10 years ago

Sorry, I completely lost track of this. Merging now -- thanks for the fix.