Skip to content

Instantly share code, notes, and snippets.

@suriya
Created May 27, 2018 09:24
Show Gist options
  • Save suriya/9c8d6f6f53b7c7393ff880b5de3de7c5 to your computer and use it in GitHub Desktop.
Save suriya/9c8d6f6f53b7c7393ff880b5de3de7c5 to your computer and use it in GitHub Desktop.
A minimal Django program demonstrating errors in postmarker application
from django.conf import settings
settings.configure(
DEBUG=True,
SECRET_KEY='A-random-secret-key!',
EMAIL_BACKEND = 'postmarker.django.EmailBackend',
POSTMARK = {
'TOKEN': 'POSTMARK_API_TEST',
'TEST_MODE': True,
'VERBOSITY': 3,
}
)
def text_and_html_alternative_success():
"""
Thus function sends a text body and html alternative.
This function succeeds.
"""
from django.core.mail import EmailMultiAlternatives
msg = EmailMultiAlternatives(
subject='Subject', body='Text body', from_email='[email protected]',
to=['[email protected]']
)
msg.attach_alternative('<html></html>', "text/html")
msg.send(fail_silently=False)
def text_and_pdf_attachment_success():
"""
This functions sends a text body and PDF attachment
This function succeeds.
"""
from django.core.mail import EmailMultiAlternatives
msg = EmailMultiAlternatives(
subject='Subject', body='Body', from_email='[email protected]',
to=['[email protected]']
)
msg.attach('hello.pdf', 'PDF-File-Contents', 'application/pdf')
msg.send(fail_silently=False)
def text_html_alternative_and_pdf_attachment_failure():
"""
This functions sends a text body, HTML alternative, and PDF attachment.
This function fails.
"""
from django.core.mail import EmailMultiAlternatives
msg = EmailMultiAlternatives(
subject='Subject', body='Body', from_email='[email protected]',
to=['[email protected]']
)
msg.attach_alternative('<html></html>', "text/html")
msg.attach('hello.pdf', 'PDF-File-Contents', 'application/pdf')
msg.send(fail_silently=False)
def text_and_text_attachment_failure():
"""
This function sends a text body and text attachment.
This function fails.
"""
from django.core.mail import EmailMultiAlternatives
msg = EmailMultiAlternatives(
subject='Subject', body='Body', from_email='[email protected]',
to=['[email protected]']
)
msg.attach('hello.txt', 'Hello World', 'text/plain')
msg.send(fail_silently=False)
if __name__ == '__main__':
import django
django.setup()
# text_and_html_alternative_success()
# text_and_pdf_attachment_success()
# text_html_alternative_and_pdf_attachment_failure()
# text_and_text_attachment_failure()
Ouput open running text_and_text_attachment_failure()
2018-05-27 09:23:24,588 - Postmarker - DEBUG - Request: POST https://api.postmarkapp.com/email/batch, Data: ({'TrackOpens': False, 'Attachments': [{'Name': 'hello.txt', 'Content': 'Hello World', 'ContentType': 'text/plain'}], 'TextBody': 'Body', 'From': '[email protected]', 'Cc': None, 'ReplyTo': None, 'Bcc': None, 'To': '[email protected]', 'Tag': None, 'HtmlBody': None, 'Headers': [], 'Subject': 'Subject'},)
2018-05-27 09:23:26,211 - Postmarker - DEBUG - Response: [{"ErrorCode":300,"Message":"Invalid attachment content - illegal base64 string."}]
Traceback (most recent call last):
File "minimal.py", line 78, in <module>
text_and_text_attachment_failure()
File "minimal.py", line 70, in text_and_text_attachment_failure
msg.send(fail_silently=False)
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/django/core/mail/message.py", line 348, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/postmarker/django/backend.py", line 71, in send_messages
self.raise_for_response(not_sent)
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/postmarker/django/backend.py", line 89, in raise_for_response
raise PostmarkerException(message)
postmarker.exceptions.PostmarkerException: [300] Invalid attachment content - illegal base64 string.
Output upon running function text_html_alternative_and_pdf_attachment_failure().
2018-05-27 09:22:22,010 - Postmarker - DEBUG - Request: POST https://api.postmarkapp.com/email/batch, Data: ({'TextBody': 'Body', 'HtmlBody': '<html></html>', 'Headers': [], 'ReplyTo': None, 'Subject': 'Subject', 'Bcc': None, 'From': '[email protected]', 'Cc': None, 'Attachments': [{'Name': None, 'Content': [<django.core.mail.message.SafeMIMEText object at 0x7f8f349f4390>, <django.core.mail.message.SafeMIMEText object at 0x7f8f349f4470>], 'ContentType': 'multipart/alternative'}, {'Name': 'hello.pdf', 'Content': 'UERGLUZpbGUtQ29udGVudHM=\n', 'ContentType': 'application/pdf'}], 'To': '[email protected]', 'TrackOpens': False, 'Tag': None},)
Traceback (most recent call last):
File "minimal.py", line 77, in <module>
text_html_alternative_and_pdf_attachment_failure()
File "minimal.py", line 56, in text_html_alternative_and_pdf_attachment_failure
msg.send(fail_silently=False)
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/django/core/mail/message.py", line 348, in send
return self.get_connection(fail_silently).send_messages([self])
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/postmarker/django/backend.py", line 66, in send_messages
responses = self.client.emails.send_batch(*prepared_messages, TrackOpens=self.get_option('TRACK_OPENS'))
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/postmarker/models/emails.py", line 332, in send_batch
return self.EmailBatch(*emails).send(**extra)
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/postmarker/models/emails.py", line 247, in send
responses = [self._manager._send_batch(*batch) for batch in chunks(emails, self.MAX_SIZE)]
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/postmarker/models/emails.py", line 247, in <listcomp>
responses = [self._manager._send_batch(*batch) for batch in chunks(emails, self.MAX_SIZE)]
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/postmarker/models/emails.py", line 276, in _send_batch
return self.call('POST', '/email/batch', data=emails)
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/postmarker/models/base.py", line 72, in call
return self.client.call(*args, **kwargs)
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/postmarker/core.py", line 106, in call
**kwargs
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/postmarker/core.py", line 129, in _call
method, url, json=data, params=kwargs, headers=default_headers, timeout=self.timeout
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/requests/sessions.py", line 494, in request
prep = self.prepare_request(req)
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/requests/sessions.py", line 437, in prepare_request
hooks=merge_hooks(request.hooks, self.hooks),
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/requests/models.py", line 308, in prepare
self.prepare_body(data, files, json)
File "/home/vagrant/postmarker-test/venv-django-1.11/lib/python3.5/site-packages/requests/models.py", line 458, in prepare_body
body = complexjson.dumps(json)
File "/usr/lib/python3.5/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.5/json/encoder.py", line 198, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.5/json/encoder.py", line 256, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.5/json/encoder.py", line 179, in default
raise TypeError(repr(o) + " is not JSON serializable")
TypeError: <django.core.mail.message.SafeMIMEText object at 0x7f8f349f4390> is not JSON serializable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment