themartorana/python-postmark

text/* attachments fail in Django 1.11

Closed this issue · 5 comments

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.

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

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.

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.

I believe the recently merged PR fixes this now: #91

Agreed Fixed via #91