Skip to content

Commit 5b34ab4

Browse files
Merge pull request #209 from sendgrid/toc-template
Added TOC and USE_CASES.md section
2 parents d032f46 + 47e400c commit 5b34ab4

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@ Please browse the rest of this README for further detail.
1010

1111
We appreciate your continued support, thank you!
1212

13+
## Table of Contents
14+
15+
* [Installation](#installation)
16+
* [Quick Start](#quick_start)
17+
* [Processing Inbound Email](#inbound)
18+
* [Usage](#usage)
19+
* [Use Cases](#use_cases)
20+
* [Announcements](#announcements)
21+
* [Roadmap](#roadmap)
22+
* [How to Contribute](#contribute)
23+
* [Troubleshooting](#troubleshooting)
24+
* [About](#about)
25+
26+
<a name="installation"></a>
1327
# Installation
1428

1529
## Prerequisites
@@ -37,6 +51,7 @@ pip install sendgrid
3751

3852
- [Python-HTTP-Client](https://github.com/sendgrid/python-http-client)
3953

54+
<a name="quick_start"></a>
4055
# Quick Start
4156

4257
## Hello Email
@@ -126,10 +141,12 @@ print(response.body)
126141
print(response.headers)
127142
```
128143

144+
<a name="inbound"></a>
129145
# Processing Inbound Email
130146

131147
Please see [our helper](https://github.com/sendgrid/sendgrid-python/tree/master/sendgrid/helpers/inbound) for utilizing our Inbound Parse webhook.
132148

149+
<a name="usage"></a>
133150
# Usage
134151

135152
- [SendGrid Documentation](https://sendgrid.com/docs/API_Reference/index.html)
@@ -139,14 +156,22 @@ Please see [our helper](https://github.com/sendgrid/sendgrid-python/tree/master/
139156
- [v3 Web API Mail Send Helper](https://github.com/sendgrid/sendgrid-python/tree/master/sendgrid/helpers/mail) - build a request object payload for a v3 /mail/send API call.
140157
- [Processing Inbound Email](https://github.com/sendgrid/sendgrid-python/tree/master/sendgrid/helpers/inbound)
141158

159+
<a name="use_cases">
160+
# Use Cases
161+
162+
[Examples of common API use cases](https://github.com/sendgrid/sendgrid-python/blob/master/USE_CASES.md), such as how to send an email with a transactional template.
163+
164+
<a name="announcements"></a>
142165
# Announcements
143166

144167
All updates to this library is documented in our [CHANGELOG](https://github.com/sendgrid/sendgrid-python/blob/master/CHANGELOG.md) and [releases](https://github.com/sendgrid/sendgrid-python/releases).
145168

169+
<a name="roadmap"></a>
146170
# Roadmap
147171

148172
If you are interested in the future direction of this project, please take a look at our open [issues](https://github.com/sendgrid/sendgrid-python/issues) and [pull requests](https://github.com/sendgrid/sendgrid-python/pulls). We would love to hear your feedback.
149173

174+
<a name="contribute"></a>
150175
# How to Contribute
151176

152177
We encourage contribution to our libraries (you might even score some nifty swag), please see our [CONTRIBUTING](https://github.com/sendgrid/sendgrid-python/blob/master/CONTRIBUTING.md) guide for details.
@@ -158,10 +183,12 @@ Quick links:
158183
- [Sign the CLA to Create a Pull Request](https://github.com/sendgrid/sendgrid-open-source-templates/tree/master/CONTRIBUTING.md#cla)
159184
- [Improvements to the Codebase](https://github.com/sendgrid/sendgrid-python/blob/master/CONTRIBUTING.md#improvements_to_the_codebase)
160185

186+
<a name="troubleshooting"></a>
161187
# Troubleshooting
162188

163189
Please see our [troubleshooting guide](https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md) for common library issues.
164190

191+
<a name="about"></a>
165192
# About
166193

167194
sendgrid-python is guided and supported by the SendGrid [Developer Experience Team](mailto:dx@sendgrid.com).

USE_CASES.md

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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

Comments
 (0)