Skip to content

Commit 201eec1

Browse files
Add remaining Use Cases
1 parent 8d36f62 commit 201eec1

File tree

1 file changed

+259
-8
lines changed

1 file changed

+259
-8
lines changed

proposals/mail-helper-refactor.md

Lines changed: 259 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ msg = Mail(from_email=From('[email protected]', 'From Name'),
1313
to_email=To('[email protected]', 'To Name'),
1414
subject=Subject('Sending with SendGrid is Fun'),
1515
plain_text_content=PlainTextContent('and easy to do anywhere, even with Python'),
16-
html_content=HtmlContent('<strong>and easy to do anywhere, even with Ruby</strong>'))
16+
html_content=HtmlContent('<strong>and easy to do anywhere, even with Python</strong>'))
1717

1818
try:
19-
response = sendgrid.send(msg, api_key=os.environ.get('SENDGRID_API_KEY'))
19+
response = sendgrid.send(msg, apikey=os.environ.get('SENDGRID_apikey'))
2020
print(response.status_code)
2121
print(response.body)
2222
print(response.headers)
@@ -41,10 +41,10 @@ msg = Mail(from_email=From('[email protected]', 'From Name'),
4141
to_emails=tos,
4242
subject=Subject('Sending with SendGrid is Fun'),
4343
plain_text_content=PlainTextContent('and easy to do anywhere, even with Python'),
44-
html_content=HtmlContent('<strong>and easy to do anywhere, even with Ruby</strong>'))
44+
html_content=HtmlContent('<strong>and easy to do anywhere, even with Python</strong>'))
4545

4646
try:
47-
response = sendgrid.send(msg, api_key=os.environ.get('SENDGRID_API_KEY'))
47+
response = sendgrid.send(msg, apikey=os.environ.get('SENDGRID_apikey'))
4848
print(response.status_code)
4949
print(response.body)
5050
print(response.headers)
@@ -88,7 +88,7 @@ msg = Mail(from_email=From('[email protected]', 'From Name'),
8888
global_substitutions=global_substitutions)
8989

9090
try:
91-
response = sendgrid.send(msg, api_key=os.environ.get('SENDGRID_API_KEY'))
91+
response = sendgrid.send(msg, apikey=os.environ.get('SENDGRID_apikey'))
9292
print(response.status_code)
9393
print(response.body)
9494
print(response.headers)
@@ -100,16 +100,267 @@ except Exception as e:
100100

101101
The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment). If you don't have your key stored in an environment variable, you can assign it directly to `apikey` for testing purposes.
102102

103-
Coming soon
103+
```python
104+
import os
105+
import sendgrid
106+
from sendgrid.helpers.mail import *
107+
108+
msg = Mail(from_email=From('[email protected]', 'From Name'),
109+
to_email=To('[email protected]', 'To Name'),
110+
subject=Subject('Sending with SendGrid is Fun'),
111+
plain_text_content=PlainTextContent('and easy to do anywhere, even with Python'),
112+
html_content=HtmlContent('<strong>and easy to do anywhere, even with Python</strong>'))
113+
114+
# For a detailed description of each of these settings, please see the [documentation](https://sendgrid.com/docs/API_Reference/api_v3.html).
115+
116+
msg.add_to(To('[email protected]', 'Example User1'))
117+
to_emails = [
118+
To('[email protected]', 'Example User2'),
119+
To('[email protected]', 'Example User3')
120+
]
121+
msg.add_tos(to_emails)
122+
123+
msg.add_cc(Cc('[email protected]', 'Example User4'))
124+
cc_emails = [
125+
Cc('[email protected]', 'Example User5'),
126+
Cc('[email protected]', 'Example User6')
127+
]
128+
msg.add_ccs(cc_emails)
129+
130+
msg.add_bcc(Bcc('[email protected]', 'Example User7'))
131+
bcc_emails = [
132+
Bcc('[email protected]', 'Example User8'),
133+
Bcc('[email protected]', 'Example User9')
134+
]
135+
msg.add_bccs(bcc_emails)
136+
137+
msg.add_header(Header('X-Test1', 'Test1'))
138+
msg.add_header(Header('X-Test2', 'Test2'))
139+
headers = [
140+
Header('X-Test3', 'Test3'),
141+
Header('X-Test4', 'Test4')
142+
]
143+
msg.add_headers(headers)
144+
145+
msg.add_substitution(Substitution('%name1%', 'Example Name 1'))
146+
msg.add_substitution(Substitution('%city1%', 'Denver'))
147+
substitutions = [
148+
Substitution('%name2%', 'Example Name 2'),
149+
Substitution('%city2%', 'Orange')
150+
]
151+
msg.add_substitutions(substitutions)
152+
153+
msg.add_custom_arg(CustomArg('marketing1', 'false'))
154+
msg.add_custom_arg(CustomArg('transactional1', 'true'))
155+
custom_args = [
156+
CustomArg('marketing2', 'true'),
157+
CustomArg('transactional2', 'false')
158+
]
159+
msg.add_custom_args(custom_args)
160+
161+
msg.set_send_at(1461775051)
162+
163+
msg.set_subject(Subject('this subject overrides the Global Subject on the default Personalization'))
164+
165+
# If you need to add more [Personalizations](https://sendgrid.com/docs/Classroom/Send/v3_Mail_Send/personalizations.html), here is an example of adding another Personalization by passing in a personalization index.
166+
167+
msg.add_to(To('[email protected]', 'Example User10'), 1)
168+
to_emails = [
169+
To('[email protected]', 'Example User11'),
170+
To('[email protected]', 'Example User12')
171+
]
172+
msg.add_tos(to_emails, 1)
173+
174+
msg.add_cc(Cc('[email protected]', 'Example User13'), 1)
175+
cc_emails = [
176+
Cc('[email protected]', 'Example User14'),
177+
Cc('[email protected]', 'Example User15')
178+
]
179+
msg.add_ccs(cc_emails, 1)
180+
181+
msg.add_bcc(Bcc('[email protected]', 'Example User16'), 1)
182+
bcc_emails = [
183+
Bcc('[email protected]', 'Example User17'),
184+
Bcc('[email protected]', 'Example User18')
185+
]
186+
msg.add_bccs(bcc_emails, 1)
187+
188+
msg.add_header(Header('X-Test5', 'Test5'), 1)
189+
msg.add_header(Header('X-Test6', 'Test6'), 1)
190+
headers = [
191+
Header('X-Test7', 'Test7'),
192+
Header('X-Test8', 'Test8')
193+
]
194+
msg.add_headers(headers, 1)
195+
196+
msg.add_substitution(Substitution('%name3%', 'Example Name 3'), 1);
197+
msg.add_substitution(Substitution('%city3%', 'Redwood City'), 1);
198+
substitutions = [
199+
Substitution('%name4%', 'Example Name 4'),
200+
Substitution('%city4%', 'London')
201+
]
202+
msg.add_substitutions(substitutions, 1)
203+
204+
msg.add_custom_arg(CustomArg('marketing3', 'true'), 1)
205+
msg.add_custom_arg(CustomArg('transactional3', 'false'), 1)
206+
custom_args = [
207+
CustomArg('marketing4', 'false'),
208+
CustomArg('transactional4': 'true')
209+
]
210+
msg.add_custom_args(custom_args, 1)
211+
212+
msg.set_send_at(1461775052, 1)
213+
214+
msg.set_subject(Subject('this subject overrides the Global Subject on the second Personalization'), 1)
215+
216+
# The values below this comment are global to entire message
217+
218+
msg.set_from(From('[email protected]', 'Example User0'))
219+
220+
msg.set_global_subject(Subject('Sending with SendGrid is Fun'));
221+
222+
msg.add_content(Content(MimeType.Text, 'and easy to do anywhere, even with Python'))
223+
msg.add_content(Content(MimeType.Html, '<strong>and easy to do anywhere, even with Python</strong>'))
224+
contents = [
225+
Content('text/calendar', 'Party Time!!'),
226+
Content('text/custom', 'Party Time 2!!')
227+
]
228+
msg.add_contents(contents)
229+
230+
msg.add_attachment(Attachment('balance_001.pdf',
231+
'base64 encoded content',
232+
'application/pdf',
233+
'attachment',
234+
'Balance Sheet'))
235+
236+
attachments = [
237+
Attachment('banner.png',
238+
'base64 encoded content',
239+
'image/png',
240+
'inline',
241+
'Banner'),
242+
Attachment('banner2.png',
243+
'base64 encoded content',
244+
'image/png',
245+
'inline',
246+
'Banner 2'),
247+
]
248+
msg.add_attachments(attachments)
249+
250+
msg.set_template_id(TemplateId('13b8f94f-bcae-4ec6-b752-70d6cb59f932'))
251+
252+
msg.add_global_header(Header('X-Day', 'Monday'))
253+
global_headers = [
254+
Header('X-Month', 'January'),
255+
Header('X-Year': '2017')
256+
]
257+
msg.set_global_headers(global_headers)
258+
259+
msg.add_section(Section('%section1%', 'Substitution for Section 1 Tag'))
260+
sections = [
261+
Section('%section2%', 'Substitution for Section 2 Tag'),
262+
Section('%section3%': 'Substitution for Section 3 Tag')
263+
]
264+
msg.add_sections(sections)
265+
266+
try:
267+
response = sendgrid.send(msg, apikey=os.environ.get('SENDGRID_apikey'))
268+
print(response.status_code)
269+
print(response.body)
270+
print(response.headers)
271+
except Exception as e:
272+
print(e.read())
273+
```
104274

105275
# Attachments
106276

107277
The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment). If you don't have your key stored in an environment variable, you can assign it directly to `apikey` for testing purposes.
108278

109-
Coming soon
279+
```python
280+
import os
281+
import sendgrid
282+
from sendgrid.helpers.mail import Mail, From, To, Subject, PlainTextContent, HtmlContent, Attachment
283+
284+
msg = Mail(from_email=From('[email protected]', 'From Name'),
285+
to_email=To('[email protected]', 'To Name'),
286+
subject=Subject('Sending with SendGrid is Fun'),
287+
plain_text_content=PlainTextContent('and easy to do anywhere, even with Python'),
288+
html_content=HtmlContent('<strong>and easy to do anywhere, even with Python</strong>'))
289+
msg.add_attachment(Attachment('balance_001.pdf',
290+
'base64 encoded content',
291+
'application/pdf',
292+
'attachment',
293+
'Balance Sheet'))
294+
295+
try:
296+
response = sendgrid.send(msg, apikey=os.environ.get('SENDGRID_apikey'))
297+
print(response.status_code)
298+
print(response.body)
299+
print(response.headers)
300+
except Exception as e:
301+
print(e.read())
302+
```
110303

111304
# Transactional Templates
112305

113306
The following code assumes you are storing the API key in an [environment variable (recommended)](https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment). If you don't have your key stored in an environment variable, you can assign it directly to `apikey` for testing purposes.
114307

115-
Coming soon
308+
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.
309+
310+
Template ID (replace with your own):
311+
312+
```text
313+
13b8f94f-bcae-4ec6-b752-70d6cb59f932
314+
```
315+
316+
Email Subject:
317+
318+
```text
319+
<%subject%>
320+
```
321+
322+
Template Body:
323+
324+
```html
325+
<html>
326+
<head>
327+
<title></title>
328+
</head>
329+
<body>
330+
Hello -name-,
331+
<br /><br/>
332+
I'm glad you are trying out the template feature!
333+
<br /><br/>
334+
<%body%>
335+
<br /><br/>
336+
I hope you are having a great day in -city- :)
337+
<br /><br/>
338+
</body>
339+
</html>
340+
```
341+
342+
```python
343+
import os
344+
import sendgrid
345+
from sendgrid.helpers.mail import Mail, From, To, Subject, PlainTextContent, HtmlContent, Attachment
346+
347+
msg = Mail(from_email=From('[email protected]', 'From Name'),
348+
to_email=To('[email protected]', 'To Name'),
349+
subject=Subject('Sending with SendGrid is Fun'),
350+
plain_text_content=PlainTextContent('and easy to do anywhere, even with Python'),
351+
html_content=HtmlContent('<strong>and easy to do anywhere, even with Python</strong>'))
352+
substitutions = [
353+
Substitution('-name-', 'Example User'),
354+
Substitution('-city-', 'Denver')
355+
]
356+
msg.add_substitutions(substitutions)
357+
msg.set_template_id(TemplateId('13b8f94f-bcae-4ec6-b752-70d6cb59f932'))
358+
359+
try:
360+
response = sendgrid.send(msg, apikey=os.environ.get('SENDGRID_apikey'))
361+
print(response.status_code)
362+
print(response.body)
363+
print(response.headers)
364+
except Exception as e:
365+
print(e.read())
366+
```

0 commit comments

Comments
 (0)