-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from SparkPost/ISSUE-75
Support for cc/bcc in Django backend
- Loading branch information
Showing
7 changed files
with
176 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
from django.core.mail import EmailMultiAlternatives | ||
|
||
from .exceptions import UnsupportedContent | ||
|
||
|
||
class SparkPostMessage(dict): | ||
""" | ||
Takes a Django EmailMessage and formats it for use with the SparkPost API. | ||
The dictionary returned would be formatted like this: | ||
{ | ||
'recipients': ['recipient@example.com'], | ||
'from_email': 'from@example.com', | ||
'text': 'Hello world', | ||
'html': '<p>Hello world</p>', | ||
'subject': 'Hello from the SparkPost Django email backend' | ||
} | ||
""" | ||
|
||
def __init__(self, message): | ||
formatted = { | ||
'recipients': message.to, | ||
'from_email': message.from_email, | ||
'subject': message.subject, | ||
'text': message.body | ||
} | ||
|
||
if message.cc: | ||
formatted['cc'] = message.cc | ||
|
||
if message.bcc: | ||
formatted['bcc'] = message.bcc | ||
|
||
if hasattr(message, 'reply_to') and message.reply_to: | ||
formatted['reply_to'] = ','.join(message.reply_to) | ||
|
||
if isinstance(message, EmailMultiAlternatives): | ||
for alternative in message.alternatives: | ||
if alternative[1] == 'text/html': | ||
formatted['html'] = alternative[0] | ||
else: | ||
raise UnsupportedContent( | ||
'Content type %s is not supported' % alternative[1] | ||
) | ||
|
||
if message.attachments: | ||
raise UnsupportedContent( | ||
'The SparkPost Django email backend does not ' | ||
'currently support attachment.' | ||
) | ||
|
||
return super(SparkPostMessage, self).__init__(formatted) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
try: | ||
from StringIO import StringIO | ||
except ImportError: | ||
from io import StringIO | ||
|
||
import pytest | ||
from django.core.mail import EmailMultiAlternatives | ||
from django.core.mail.message import EmailMessage | ||
|
||
from sparkpost.django.exceptions import UnsupportedContent | ||
from sparkpost.django.message import SparkPostMessage | ||
from .utils import at_least_version | ||
|
||
|
||
base_options = dict( | ||
subject='Test', | ||
body='Testing', | ||
from_email='test@from.com', | ||
to=['recipient@example.com'] | ||
) | ||
|
||
|
||
def message(**options): | ||
options.update(base_options) | ||
email_message = EmailMessage(**options) | ||
return SparkPostMessage(email_message) | ||
|
||
|
||
def multipart_message(**options): | ||
options.update(base_options) | ||
email_message = EmailMultiAlternatives(**options) | ||
email_message.attach_alternative('<p>Testing</p>', 'text/html') | ||
return SparkPostMessage(email_message) | ||
|
||
|
||
base_expected = dict( | ||
recipients=['recipient@example.com'], | ||
from_email='test@from.com', | ||
subject='Test', | ||
text='Testing' | ||
) | ||
|
||
|
||
def test_minimal(): | ||
assert message() == base_expected | ||
|
||
|
||
def test_multipart(): | ||
expected = dict( | ||
html='<p>Testing</p>' | ||
) | ||
expected.update(base_expected) | ||
assert multipart_message() == expected | ||
|
||
|
||
def test_cc_bcc(): | ||
expected = dict( | ||
cc=['ccone@example.com'], | ||
bcc=['bccone@example.com'] | ||
) | ||
expected.update(base_expected) | ||
|
||
options = dict(cc=['ccone@example.com'], | ||
bcc=['bccone@example.com']) | ||
assert message(**options) == expected | ||
|
||
|
||
def test_attachment(): | ||
attachment = StringIO() | ||
attachment.write('hello file') | ||
email_message = EmailMessage(**base_options) | ||
email_message.attach('file.txt', attachment, 'text/plain') | ||
|
||
with pytest.raises(UnsupportedContent): | ||
SparkPostMessage(email_message) | ||
|
||
if at_least_version('1.8'): | ||
def test_reply_to(): | ||
expected = dict( | ||
reply_to='replyone@example.com,replytwo@example.com' | ||
) | ||
expected.update(base_expected) | ||
|
||
assert message(reply_to=['replyone@example.com', | ||
'replytwo@example.com']) == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from distutils.version import StrictVersion | ||
|
||
from django import get_version | ||
|
||
|
||
def at_least_version(version): | ||
return StrictVersion(get_version()) > StrictVersion(version) |