Skip to content

Commit b002197

Browse files
committed
Adds support for dynamic template data in personalizations
1 parent 1ac58c6 commit b002197

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import json
2-
import os
3-
import urllib2
1+
from sendgrid import SendGridAPIClient
42
from sendgrid.helpers.mail import *
5-
from sendgrid import *
3+
64

75
# NOTE: you will need move this file to the root
86
# directory of this project to execute properly.
@@ -217,3 +215,23 @@ def send_kitchen_sink():
217215

218216
# this will only send an email if you set SandBox Mode to False
219217
send_kitchen_sink()
218+
219+
220+
def dynamic_template_usage():
221+
"""
222+
Sample usage of dynamic (handlebars) transactional templates.
223+
To make this work, you should have dynamic template created within your
224+
SendGrid account. For this particular example, template may be like::
225+
226+
<p>Hello, {{name}}! Your current balance is {{balance}}<p>
227+
228+
"""
229+
mail = Mail(from_email='templates@sendgrid.com')
230+
mail.template_id = 'd-your-dynamic-template-uid'
231+
p = Personalization()
232+
p.add_to(Email('user@example.com'))
233+
p.dynamic_template_data = {'name': 'Bob', 'balance': 42}
234+
mail.add_personalization(p)
235+
236+
sg = SendGridAPIClient(apikey='SG.your-api-key')
237+
sg.client.mail.send.post(request_body=mail.get())

sendgrid/helpers/mail/personalization.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
class Personalization(object):
22
"""A Personalization defines who should receive an individual message and
33
how that message should be handled.
4+
5+
:var dynamic_template_data: data for dynamic transactional template.
6+
Should be JSON-serializeable structure. No pre-processing sill be done
7+
prior to sending this via http client.
48
"""
59

610
def __init__(self):
@@ -13,6 +17,7 @@ def __init__(self):
1317
self._substitutions = []
1418
self._custom_args = []
1519
self._send_at = None
20+
self.dynamic_template_data = None
1621

1722
@property
1823
def tos(self):
@@ -198,4 +203,8 @@ def get(self):
198203

199204
if self.send_at is not None:
200205
personalization["send_at"] = self.send_at
206+
207+
if self.dynamic_template_data is not None:
208+
personalization['dynamic_template_data'] = self.dynamic_template_data
209+
201210
return personalization

test/test_mail.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ def test_sendgridAPIKey(self):
8080
else:
8181
self.fail("Should have failed as SendGrid API key included")
8282

83-
8483
def test_helloEmail(self):
8584
self.max_diff = None
8685

@@ -130,7 +129,7 @@ def test_helloEmailAdditionalContent(self):
130129
personalization = Personalization()
131130
personalization.add_to(Email("test@example.com"))
132131
mail.add_personalization(personalization)
133-
132+
134133
mail.add_content(Content("text/html", "<html><body>some text here</body></html>"))
135134
mail.add_content(Content("text/plain", "some text here"))
136135

@@ -562,3 +561,14 @@ def test_disable_tracking(self):
562561
def test_directly_setting_substitutions(self):
563562
personalization = Personalization()
564563
personalization.substitutions = [{'a': 0}]
564+
565+
def test_dynamic_template_data(self):
566+
p = Personalization()
567+
p.add_to(Email('test@sendgrid.com'))
568+
p.dynamic_template_data = {'customer': {'name': 'Bob', 'returning': True}, 'total': 42}
569+
570+
expected = {
571+
'to': [{'email': 'test@sendgrid.com'}],
572+
'dynamic_template_data': {'customer': {'name': 'Bob', 'returning': True}, 'total': 42}
573+
}
574+
self.assertDictEqual(p.get(), expected)

0 commit comments

Comments
 (0)