|
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 |
|
|
0 commit comments