Skip to content

Commit

Permalink
Add support for template and sub data to Django backend
Browse files Browse the repository at this point in the history
Resolves #107
  • Loading branch information
pl-jankowskimichal authored and richleland committed Apr 13, 2016
1 parent 8c9bf1c commit 1fc96b0
Show file tree
Hide file tree
Showing 3 changed files with 88 additions and 7 deletions.
21 changes: 20 additions & 1 deletion docs/django/backend.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,26 @@ Django is now configured to use the SparkPost email backend. You can now send ma
html_message='<p>Hello Rock stars!</p>',
)
If you need to add cc, bcc, reply to, or attachments, use the `EmailMultiAlternatives` class directly:
You can also use `EmailMessage` or `EmailMultiAlternatives` class directly. That will give you access to more specific fileds like `template`:

.. code-block:: python
email = EmailMessage(
to=[
{
"address": "to@example.com",
"substitution_data": {
"key": "value"
}
}
],
from_email='test@from.com'
)
email.template = 'template-id'
email.send()
Or cc, bcc, reply to, or attachments fields:

.. code-block:: python
Expand Down
25 changes: 19 additions & 6 deletions sparkpost/django/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,20 @@ class SparkPostMessage(dict):
"""

def __init__(self, message):
formatted = {
'recipients': message.to,
'from_email': message.from_email,
'subject': message.subject,
'text': message.body
}

formatted = dict()

if message.to:
formatted['recipients'] = message.to

if message.from_email:
formatted['from_email'] = message.from_email

if message.subject:
formatted['subject'] = message.subject

if message.body:
formatted['text'] = message.body

if message.cc:
formatted['cc'] = message.cc
Expand Down Expand Up @@ -64,5 +72,10 @@ def __init__(self, message):
'data': base64_encoded_content.decode('ascii'),
'type': mimetype
})
if hasattr(message, 'substitution_data'):
formatted['substitution_data'] = message.substitution_data

if hasattr(message, 'template'):
formatted['template'] = message.template

super(SparkPostMessage, self).__init__(formatted)
49 changes: 49 additions & 0 deletions test/django/test_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,55 @@ def test_attachment_unicode():
expected.update(base_expected)
assert actual == expected


def test_template():
email_message = EmailMessage(
to=['to@example.com'],
from_email='test@from.com'
)
email_message.template = 'template-id'
actual = SparkPostMessage(email_message)
expected = dict(
recipients=['to@example.com'],
from_email='test@from.com',
template='template-id'
)
assert actual == expected


def test_substitution_data():
email_message = EmailMessage(
to=[
{
"address": "to@example.com",
"substitution_data": {
"key": "value"
}
}
],
from_email='test@from.com'
)
email_message.template = 'template-id'
email_message.substitution_data = {"key2": "value2"}
actual = SparkPostMessage(email_message)

expected = dict(
recipients=[
{
"address": "to@example.com",
"substitution_data": {
"key": "value"
}
}
],
from_email='test@from.com',
template='template-id',
substitution_data={"key2": "value2"}
)

assert actual == expected


if at_least_version('1.8'):
def test_reply_to():
expected = dict(
Expand Down

0 comments on commit 1fc96b0

Please sign in to comment.