|
| 1 | +This documentation provides examples for specific use cases. Please [open an issue](https://github.com/sendgrid/sendgrid-python/issues) or make a pull request for any use cases you would like us to document here. Thank you! |
| 2 | + |
| 3 | +# Table of Contents |
| 4 | + |
| 5 | +* [Transactional Templates](#transactional_templates) |
| 6 | + |
| 7 | +<a name="transactional_templates"></a> |
| 8 | +# Transactional Templates |
| 9 | + |
| 10 | +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. |
| 11 | + |
| 12 | +Template ID (replace with your own): |
| 13 | + |
| 14 | +```text |
| 15 | +13b8f94f-bcae-4ec6-b752-70d6cb59f932 |
| 16 | +``` |
| 17 | + |
| 18 | +Email Subject: |
| 19 | + |
| 20 | +```text |
| 21 | +<%subject%> |
| 22 | +``` |
| 23 | + |
| 24 | +Template Body: |
| 25 | + |
| 26 | +```html |
| 27 | +<html> |
| 28 | +<head> |
| 29 | + <title></title> |
| 30 | +</head> |
| 31 | +<body> |
| 32 | +Hello -name-, |
| 33 | +<br /><br/> |
| 34 | +I'm glad you are trying out the template feature! |
| 35 | +<br /><br/> |
| 36 | +<%body%> |
| 37 | +<br /><br/> |
| 38 | +I hope you are having a great day in -city- :) |
| 39 | +<br /><br/> |
| 40 | +</body> |
| 41 | +</html> |
| 42 | +``` |
| 43 | + |
| 44 | +## With Mail Helper Class |
| 45 | + |
| 46 | +```python |
| 47 | +import sendgrid |
| 48 | +import os |
| 49 | +from sendgrid.helpers.mail import Email, Content, Substitution, Mail |
| 50 | +try: |
| 51 | + # Python 3 |
| 52 | + import urllib.request as urllib |
| 53 | +except ImportError: |
| 54 | + # Python 2 |
| 55 | + import urllib2 as urllib |
| 56 | + |
| 57 | +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) |
| 58 | +from_email = Email("test@example.com") |
| 59 | +subject = "I'm replacing the subject tag" |
| 60 | +to_email = Email("test@example.com") |
| 61 | +content = Content("text/html", "I'm replacing the <strong>body tag</strong>") |
| 62 | +mail = Mail(from_email, subject, to_email, content) |
| 63 | +mail.personalizations[0].add_substitution(Substitution("-name-", "Example User")) |
| 64 | +mail.personalizations[0].add_substitution(Substitution("-city-", "Denver")) |
| 65 | +mail.set_template_id("13b8f94f-bcae-4ec6-b752-70d6cb59f932") |
| 66 | +try: |
| 67 | + response = sg.client.mail.send.post(request_body=mail.get()) |
| 68 | +except urllib.HTTPError as e: |
| 69 | + print e.read() |
| 70 | + exit() |
| 71 | +print(response.status_code) |
| 72 | +print(response.body) |
| 73 | +print(response.headers) |
| 74 | +``` |
| 75 | + |
| 76 | +## Without Mail Helper Class |
| 77 | + |
| 78 | +```python |
| 79 | +import sendgrid |
| 80 | +import os |
| 81 | +try: |
| 82 | + # Python 3 |
| 83 | + import urllib.request as urllib |
| 84 | +except ImportError: |
| 85 | + # Python 2 |
| 86 | + import urllib2 as urllib |
| 87 | + |
| 88 | +sg = sendgrid.SendGridAPIClient(apikey=os.environ.get('SENDGRID_API_KEY')) |
| 89 | +data = { |
| 90 | + "personalizations": [ |
| 91 | + { |
| 92 | + "to": [ |
| 93 | + { |
| 94 | + "email": "elmer@sendgrid.com" |
| 95 | + } |
| 96 | + ], |
| 97 | + "substitutions": { |
| 98 | + "-name-": "Example User", |
| 99 | + "-city-": "Denver" |
| 100 | + }, |
| 101 | + "subject": "I'm replacing the subject tag" |
| 102 | + }, |
| 103 | + ], |
| 104 | + "from": { |
| 105 | + "email": "dx@sendgrid.com" |
| 106 | + }, |
| 107 | + "content": [ |
| 108 | + { |
| 109 | + "type": "text/html", |
| 110 | + "value": "I'm replacing the <strong>body tag</strong>" |
| 111 | + } |
| 112 | + ], |
| 113 | + "template_id": "13b8f94f-bcae-4ec6-b752-70d6cb59f932" |
| 114 | +} |
| 115 | +try: |
| 116 | + response = sg.client.mail.send.post(request_body=data) |
| 117 | +except urllib.HTTPError as e: |
| 118 | + print e.read() |
| 119 | + exit() |
| 120 | +print(response.status_code) |
| 121 | +print(response.body) |
| 122 | +print(response.headers) |
| 123 | +``` |
0 commit comments