Skip to content

Commit

Permalink
Merge pull request #82 from clickonchris/django-post-office3
Browse files Browse the repository at this point in the history
Fixes a HTML content problem uncovered from django-post-office 3
  • Loading branch information
nicholasserra authored Jan 18, 2020
2 parents 19dc975 + 90c60d6 commit a706bd0
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 14 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
language: python
python:
- '2.7'
- '3.5'
- '3.6'
- '3.7'
Expand Down
2 changes: 1 addition & 1 deletion postmark/django_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def _build_message(self, message):
html_body = alt[0]
break

elif getattr(message, 'content_subtype', None) == 'html':
if getattr(message, 'content_subtype', None) == 'html':
# Don't send html content as plain text
text_body = None
html_body = message.body
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Django==1.11.23
Django==2.2.9
mock
8 changes: 5 additions & 3 deletions test/demo/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ class EmailForm(forms.Form):
sender = forms.EmailField(max_length=100, initial=settings.POSTMARK_SENDER)
to = forms.CharField(initial='[email protected]')
subject = forms.CharField(initial='test email')
body = forms.CharField(widget=forms.Textarea, initial='this is a test')
body = forms.CharField(widget=forms.Textarea, initial='this is a test')
html_body = forms.CharField(widget=forms.Textarea, initial='<h1>Test</h1>This is a test')

def save(self, fail_silently=False):
"""
Build and send the email message.
"""
send_mail(subject=unicode(ugettext_lazy(self.cleaned_data['subject'])),
message=self.cleaned_data['body'],
send_mail(subject=str(ugettext_lazy(self.cleaned_data['subject'])),
message=self.cleaned_data['body'],
html_message=self.cleaned_data['html_body'],
from_email=self.cleaned_data['sender'],
recipient_list=[addr.strip() for addr in self.cleaned_data['to'].split(',')],
fail_silently=fail_silently)
Expand Down
11 changes: 7 additions & 4 deletions test/demo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,20 @@
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
# 'django.contrib.admin',
)

MIDDLEWARE_CLASSES = (
)

ROOT_URLCONF = 'urls'

TEMPLATE_DIRS = (
os.path.join(settings_path, 'templates'),
)
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(settings_path, 'templates')],
}
]

#EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
#EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
Expand Down
7 changes: 4 additions & 3 deletions test/demo/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# Uncomment the next two lines to enable the admin:
# from django.contrib import admin
# admin.autodiscover()
import views

urlpatterns = patterns('',
(r'', 'views.test_email'),
)
urlpatterns = [
url(r'', views.test_email),
]
62 changes: 61 additions & 1 deletion tests.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import json
import sys
import unittest
from email.mime.image import MIMEImage

from io import BytesIO

from django.core.mail import EmailMultiAlternatives, EmailMessage
from django.test import override_settings, TestCase

from postmark.django_backend import EmailBackend

if sys.version_info[0] < 3:
from StringIO import StringIO
from urllib2 import HTTPError
Expand Down Expand Up @@ -206,7 +212,60 @@ def test_activate(self):

with mock.patch('postmark.core.HTTPConnection.getresponse') as mock_response:
mock_response.return_value = StringIO('{"test": "test"}')
self.assertEquals(bounce.activate(1), {'test': 'test'})
self.assertEqual(bounce.activate(1), {'test': 'test'})


class EmailBackendTests(TestCase):

def setUp(self):
self.backend = EmailBackend(api_key='dummy')

def test_send_multi_alternative_html_email(self):
# build a message and send it
message = EmailMultiAlternatives(
connection=self.backend,
from_email='[email protected]', to=['[email protected]'], subject='html test', body='hello there'
)
message.attach_alternative('<b>hello</b> there', 'text/html')

with mock.patch('postmark.core.urlopen', side_effect=HTTPError('', 200, '', {}, None)) as transport:
message.send()
data = json.loads(transport.call_args[0][0].data.decode('utf-8'))
self.assertEqual('hello there', data['TextBody'])
self.assertEqual('<b>hello</b> there', data['HtmlBody'])

def test_send_content_subtype_email(self):
# build a message and send it
message = EmailMessage(
connection=self.backend,
from_email='[email protected]', to=['[email protected]'], subject='html test', body='<b>hello</b> there'
)
message.content_subtype = 'html'

with mock.patch('postmark.core.urlopen', side_effect=HTTPError('', 200, '', {}, None)) as transport:
message.send()
data = json.loads(transport.call_args[0][0].data.decode('utf-8'))
self.assertEqual('<b>hello</b> there', data['HtmlBody'])
self.assertFalse('TextBody' in data)

def test_send_multi_alternative_with_subtype_html_email(self):
"""
Client uses EmailMultiAlternative but instead of specifying a html alternative they insert html content
into the main message and specify message_subtype
:return:
"""
message = EmailMultiAlternatives(
connection=self.backend,
from_email='[email protected]', to=['[email protected]'], subject='html test', body='<b>hello</b> there'
)
# NO alternatives attached. subtype specified instead
message.content_subtype = 'html'

with mock.patch('postmark.core.urlopen', side_effect=HTTPError('', 200, '', {}, None)) as transport:
message.send()
data = json.loads(transport.call_args[0][0].data.decode('utf-8'))
self.assertFalse('TextBody' in data)
self.assertEqual('<b>hello</b> there', data['HtmlBody'])


if __name__ == '__main__':
Expand All @@ -215,6 +274,7 @@ def test_activate(self):
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': ':memory:'
}
},
INSTALLED_APPS=[
Expand Down

0 comments on commit a706bd0

Please sign in to comment.