themartorana / python-postmark

Postmark library for Python 2.6 and greater
http://davemartorana.com
MIT License
199 stars 83 forks source link

text/* attachments fail in Django 1.11 #71

Closed jarcoal closed 3 years ago

jarcoal commented 7 years ago

Django 1.11 made a change to the way text/* files are attached to a message; now it will try and decode the content into utf-8, which will bomb when this library attempts to b64encode it.

There should be a check to see if the content is unicode and then encode it to a string before b64'ing.

nicholasserra commented 7 years ago

Thanks for the report and detailed info! I'll try to get a fix in asap. PRs are also welcome :)

jarcoal commented 7 years ago

Yeah I'll see if I can push something up over the weekend.

In case others see this thread and are looking for a quick fix, you can skip Django's email.attach(...) method and add the attachment directly like this: email.attachments.append((filename, content, mimetype)). That will skip the Django code that decodes the attachment into unicode.

nicholasserra commented 7 years ago

Trying to write a test to show this, having a hard time triggering it. Adding some logging is showing that i'm hitting the right lines, but getting no error. Here's my test:

class DjangoBackendTests(unittest.TestCase):
    def test_text_attachment_issue(self):
        email = PMEmailMessage('Subject', 'Content', 'from@example.com', ['to@example.com'])
        email.attach("attachment.txt", b"test", mimetype="text/plain")
        conn = mail.get_connection('postmark.django_backend.EmailBackend')
        conn.test_mode = True
        conn.send_messages([email])
        print 'Test ran'

Still working on it.

dfranc3373 commented 3 years ago

I believe the recently merged PR fixes this now: https://github.com/themartorana/python-postmark/pull/91

nicholasserra commented 3 years ago

Agreed Fixed via #91