Skip to content

Commit 1fc96b0

Browse files
pl-jankowskimichalrichleland
authored andcommitted
Add support for template and sub data to Django backend
Resolves #107
1 parent 8c9bf1c commit 1fc96b0

File tree

3 files changed

+88
-7
lines changed

3 files changed

+88
-7
lines changed

docs/django/backend.rst

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,26 @@ Django is now configured to use the SparkPost email backend. You can now send ma
4444
html_message='<p>Hello Rock stars!</p>',
4545
)
4646
47-
If you need to add cc, bcc, reply to, or attachments, use the `EmailMultiAlternatives` class directly:
47+
48+
You can also use `EmailMessage` or `EmailMultiAlternatives` class directly. That will give you access to more specific fileds like `template`:
49+
50+
.. code-block:: python
51+
52+
email = EmailMessage(
53+
to=[
54+
{
55+
"address": "[email protected]",
56+
"substitution_data": {
57+
"key": "value"
58+
}
59+
}
60+
],
61+
from_email='[email protected]'
62+
)
63+
email.template = 'template-id'
64+
email.send()
65+
66+
Or cc, bcc, reply to, or attachments fields:
4867

4968
.. code-block:: python
5069

sparkpost/django/message.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@ class SparkPostMessage(dict):
2222
"""
2323

2424
def __init__(self, message):
25-
formatted = {
26-
'recipients': message.to,
27-
'from_email': message.from_email,
28-
'subject': message.subject,
29-
'text': message.body
30-
}
25+
26+
formatted = dict()
27+
28+
if message.to:
29+
formatted['recipients'] = message.to
30+
31+
if message.from_email:
32+
formatted['from_email'] = message.from_email
33+
34+
if message.subject:
35+
formatted['subject'] = message.subject
36+
37+
if message.body:
38+
formatted['text'] = message.body
3139

3240
if message.cc:
3341
formatted['cc'] = message.cc
@@ -64,5 +72,10 @@ def __init__(self, message):
6472
'data': base64_encoded_content.decode('ascii'),
6573
'type': mimetype
6674
})
75+
if hasattr(message, 'substitution_data'):
76+
formatted['substitution_data'] = message.substitution_data
77+
78+
if hasattr(message, 'template'):
79+
formatted['template'] = message.template
6780

6881
super(SparkPostMessage, self).__init__(formatted)

test/django/test_message.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,55 @@ def test_attachment_unicode():
107107
expected.update(base_expected)
108108
assert actual == expected
109109

110+
111+
def test_template():
112+
email_message = EmailMessage(
113+
114+
from_email='[email protected]'
115+
)
116+
email_message.template = 'template-id'
117+
actual = SparkPostMessage(email_message)
118+
expected = dict(
119+
recipients=['[email protected]'],
120+
from_email='[email protected]',
121+
template='template-id'
122+
)
123+
assert actual == expected
124+
125+
126+
def test_substitution_data():
127+
email_message = EmailMessage(
128+
to=[
129+
{
130+
"address": "[email protected]",
131+
"substitution_data": {
132+
"key": "value"
133+
}
134+
}
135+
],
136+
from_email='[email protected]'
137+
)
138+
email_message.template = 'template-id'
139+
email_message.substitution_data = {"key2": "value2"}
140+
actual = SparkPostMessage(email_message)
141+
142+
expected = dict(
143+
recipients=[
144+
{
145+
"address": "[email protected]",
146+
"substitution_data": {
147+
"key": "value"
148+
}
149+
}
150+
],
151+
from_email='[email protected]',
152+
template='template-id',
153+
substitution_data={"key2": "value2"}
154+
)
155+
156+
assert actual == expected
157+
158+
110159
if at_least_version('1.8'):
111160
def test_reply_to():
112161
expected = dict(

0 commit comments

Comments
 (0)