|
1 |
| -# Transactional Templates |
| 1 | +### Transactional Templates |
2 | 2 |
|
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. |
4 | 69 |
|
5 | 70 | Template ID (replace with your own):
|
6 | 71 |
|
@@ -66,38 +131,6 @@ print(response.body)
|
66 | 131 | print(response.headers)
|
67 | 132 | ```
|
68 | 133 |
|
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 |
| - |
101 | 134 | ## Without Mail Helper Class
|
102 | 135 |
|
103 | 136 | ```python
|
@@ -145,4 +178,4 @@ except urllib.HTTPError as e:
|
145 | 178 | print(response.status_code)
|
146 | 179 | print(response.body)
|
147 | 180 | print(response.headers)
|
148 |
| -``` |
| 181 | +``` |
0 commit comments