Skip to content

Commit 28bd2a5

Browse files
Merge #593
1 parent 68b9eb0 commit 28bd2a5

File tree

5 files changed

+95
-41
lines changed

5 files changed

+95
-41
lines changed

docker-test/entrypoint.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ fi
1313

1414
cd sendgrid-python
1515
python3.6 setup.py install
16-
pip install pyyaml six werkzeug flask python-http-client
16+
pip install pyyaml six werkzeug flask python-http-client pytest
1717
exec $SHELL

examples/helpers/mail_example.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,10 @@ def send_kitchen_sink():
218218
send_kitchen_sink()
219219

220220

221-
def dynamic_template_usage():
221+
def transactional_template_usage():
222+
# Assumes you set your environment variable:
223+
# https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key
224+
222225
"""
223226
Sample usage of dynamic (handlebars) transactional templates.
224227
To make this work, you should have dynamic template created within your
@@ -228,7 +231,7 @@ def dynamic_template_usage():
228231
229232
"""
230233
mail = Mail()
231-
mail.from_email = '[email protected]'
234+
mail.from_email = Email('[email protected]')
232235
mail.template_id = 'd-your-dynamic-template-uid'
233236
p = Personalization()
234237
p.add_to(Email('[email protected]'))
@@ -238,5 +241,8 @@ def dynamic_template_usage():
238241
}
239242
mail.add_personalization(p)
240243

241-
sg = SendGridAPIClient(apikey='SG.your-api-key')
242-
sg.client.mail.send.post(request_body=mail.get())
244+
sg = SendGridAPIClient()
245+
response = sg.client.mail.send.post(request_body=mail.get())
246+
print(response.status_code)
247+
print(response.headers)
248+
print(response.body)

sendgrid/helpers/mail/mail.py

+3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""v3/mail/send response body builder"""
22
from .personalization import Personalization
33
from .header import Header
4+
from .email import Email
45

56

67
class Mail(object):
@@ -147,6 +148,8 @@ def from_email(self):
147148

148149
@from_email.setter
149150
def from_email(self, value):
151+
if isinstance(value, str):
152+
value = Email(value)
150153
self._from_email = value
151154

152155
@property

sendgrid/helpers/mail/personalization.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def __init__(self):
1717
self._substitutions = []
1818
self._custom_args = []
1919
self._send_at = None
20-
self.dynamic_template_data = None
20+
self._dynamic_template_data = None
2121

2222
@property
2323
def tos(self):
@@ -163,6 +163,18 @@ def send_at(self):
163163
def send_at(self, value):
164164
self._send_at = value
165165

166+
@property
167+
def dynamic_template_data(self):
168+
"""Data for dynamic transactional template.
169+
170+
:rtype: JSON-serializeable structure
171+
"""
172+
return self._dynamic_template_data
173+
174+
@dynamic_template_data.setter
175+
def dynamic_template_data(self, json):
176+
self._dynamic_template_data = json
177+
166178
def get(self):
167179
"""
168180
Get a JSON-ready representation of this Personalization.

use_cases/transational_templates.md

+68-35
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,71 @@
1-
# Transactional Templates
1+
### Transactional Templates
22

3-
For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/index.html). Following is the template content we used for testing.
3+
Sendgrid transactional templates let you leverage power of [handlebars](https://handlebarsjs.com/)
4+
syntax to easily manage complex dynamic content in transactional emails.
5+
6+
For this example, we assume you have created a [transactional template](https://sendgrid.com/docs/User_Guide/Transactional_Templates/create_and_edit_transactional_templates.html). Following is the template content we used for testing.
7+
8+
This example also assumes you [set your environment variable](https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key) with your SendGrid API Key.
9+
10+
Template ID (replace with your own):
11+
12+
```text
13+
d-13b8f94fbcae4ec6b75270d6cb59f932
14+
```
15+
16+
Email Subject:
17+
18+
```text
19+
{{ subject }}
20+
```
21+
22+
Template Body:
23+
24+
```html
25+
<html>
26+
<head>
27+
<title></title>
28+
</head>
29+
<body>
30+
Hello {{ name }},
31+
<br /><br/>
32+
I'm glad you are trying out the template feature!
33+
<br /><br/>
34+
I hope you are having a great day in {{ city }} :)
35+
<br /><br/>
36+
</body>
37+
</html>
38+
```
39+
40+
```python
41+
from sendgrid import SendGridAPIClient
42+
from sendgrid.helpers.mail import Mail, Email, Personalization
43+
44+
45+
sg = SendGridAPIClient()
46+
mail = Mail()
47+
mail.from_email = Email('[email protected]')
48+
mail.template_id = 'd-your-dynamic-template-uid'
49+
p = Personalization()
50+
p.add_to(Email('[email protected]'))
51+
p.dynamic_template_data = {
52+
'subject': 'Dynamic Templates in Python',
53+
'name': 'Example User',
54+
'city': 'Denver'
55+
}
56+
mail.add_personalization(p)
57+
58+
response = sg.client.mail.send.post(request_body=mail.get())
59+
print(response.status_code)
60+
print(response.headers)
61+
print(response.body)
62+
```
63+
64+
Read more about dynamic templates [here](https://sendgrid.com/docs/User_Guide/Transactional_Templates/how_to_send_an_email_with_transactional_templates.html).
65+
66+
# Legacy Templates
67+
68+
For this example, we assume you have created a [Legacy Template](https://sendgrid.com/templates). Following is the template content we used for testing.
469

570
Template ID (replace with your own):
671

@@ -66,38 +131,6 @@ print(response.body)
66131
print(response.headers)
67132
```
68133

69-
### With dynamic templates
70-
71-
Sendgrid dynamic templates let you leverage power of [handlebars](https://handlebarsjs.com/)
72-
syntax to easily manage complex dynamic content in transactional emails.
73-
74-
To check this example snippet, create
75-
[transactional email template](https://sendgrid.com/dynamic_templates) with code like
76-
```html
77-
<p>Hello, {{name}}! Your current balance is {{balance}}<p>
78-
```
79-
80-
Than send email based on it, providing context for substitutions:
81-
```python
82-
from sendgrid import SendGridAPIClient
83-
from sendgrid.helpers.mail import Email, Personalization
84-
85-
86-
sg = SendGridAPIClient(apikey='SG.your-api-key')
87-
88-
mail = Mail()
89-
mail.from_email='[email protected]'
90-
mail.template_id = 'd-your-dynamic-template-uid'
91-
p = Personalization()
92-
p.add_to(Email('[email protected]'))
93-
p.dynamic_template_data = {'name': 'Bob', 'balance': 42}
94-
mail.add_personalization(p)
95-
96-
sg.client.mail.send.post(request_body=mail.get())
97-
```
98-
99-
Read more about dynamic templates in [docs](https://sendgrid.com/docs/User_Guide/Transactional_Templates/how_to_send_an_email_with_transactional_templates.html)
100-
101134
## Without Mail Helper Class
102135

103136
```python
@@ -145,4 +178,4 @@ except urllib.HTTPError as e:
145178
print(response.status_code)
146179
print(response.body)
147180
print(response.headers)
148-
```
181+
```

0 commit comments

Comments
 (0)