-
Notifications
You must be signed in to change notification settings - Fork 714
/
live_test.py
357 lines (307 loc) · 13.2 KB
/
live_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
## Send a Single Email to a Single Recipient
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, From, To, Subject, PlainTextContent, HtmlContent, SendGridException
message = Mail(from_email=From('[email protected]', 'Twilio SendGrid'),
to_emails=To('[email protected]', 'Elmer Thomas'),
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent('and easy to do anywhere, even with Python'),
html_content=HtmlContent('<strong>and easy to do anywhere, even with Python</strong>'))
try:
print(json.dumps(message.get(), sort_keys=True, indent=4))
sendgrid_client = SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
response = sendgrid_client.send(message=message)
print(response.status_code)
print(response.body)
print(response.headers)
except SendGridException as e:
print(e.message)
# Send a Single Email to Multiple Recipients
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, From, To, Subject, PlainTextContent, HtmlContent, SendGridException
to_emails = [
To('[email protected]', 'Elmer SendGrid'),
To('[email protected]', 'Elmer Thomas')
]
message = Mail(from_email=From('[email protected]', 'Twilio SendGrid'),
to_emails=to_emails,
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent('and easy to do anywhere, even with Python'),
html_content=HtmlContent('<strong>and easy to do anywhere, even with Python</strong>'))
try:
print(json.dumps(message.get(), sort_keys=True, indent=4))
sendgrid_client = SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
response = sendgrid_client.send(message=message)
print(response.status_code)
print(response.body)
print(response.headers)
except SendGridException as e:
print(e.message)
# Send Multiple Emails to Multiple Recipients
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, From, To, Subject, PlainTextContent, HtmlContent, SendGridException, Substitution
import time
import datetime
to_emails = [
To(email='[email protected]',
name='Elmer Twilio',
substitutions={
Substitution('-name-', 'Elmer Twilio'),
Substitution('-github-', 'https://github.com/ethomas'),
},
subject=Subject('Override Global Subject')),
To(email='[email protected]',
name='Elmer Thomas',
substitutions={
Substitution('-name-', 'Elmer Thomas'),
Substitution('-github-', 'https://github.com/thinkingserious'),
})
]
ts = time.time()
global_substitutions = Substitution('-time-', datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))
message = Mail(from_email=From('[email protected]', 'Twilio SendGrid'),
to_emails=to_emails,
subject=Subject('Hi -name-'),
plain_text_content=PlainTextContent('Hello -name-, your github is -github-, email sent at -time-'),
html_content=HtmlContent('<strong>Hello -name-, your github is <a href=\"-github-\">here</a></strong> email sent at -time-'),
global_substitutions=global_substitutions,
is_multiple=True)
try:
print(json.dumps(message.get(), sort_keys=True, indent=4))
sendgrid_client = SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
response = sendgrid_client.send(message=message)
print(response.status_code)
print(response.body)
print(response.headers)
except SendGridException as e:
print(e.message)
# Kitchen Sink - an example with all settings used
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import (
Mail, From, To, Cc, Bcc, Subject, PlainTextContent,
HtmlContent, SendGridException, Substitution,
Header, CustomArg, SendAt, Content, MimeType, Attachment,
FileName, FileContent, FileType, Disposition, ContentId,
TemplateId, Section, ReplyTo, Category, BatchId, Asm,
GroupId, GroupsToDisplay, IpPoolName, MailSettings,
BccSettings, BccSettingsEmail, BypassListManagement,
FooterSettings, FooterText, FooterHtml, SandBoxMode,
SpamCheck, SpamThreshold, SpamUrl, TrackingSettings,
ClickTracking, SubscriptionTracking, SubscriptionText,
SubscriptionHtml, SubscriptionSubstitutionTag,
OpenTracking, OpenTrackingSubstitutionTag, Ganalytics,
UtmSource, UtmMedium, UtmTerm, UtmContent, UtmCampaign)
import time
import datetime
message = Mail()
# Define Personalizations
message.to = To('[email protected]', 'Example User1', p=0)
message.to = [
To('[email protected]', 'Example User2', p=0),
To('[email protected]', 'Example User3', p=0)
]
message.cc = Cc('[email protected]', 'Example User4', p=0)
message.cc = [
Cc('[email protected]', 'Example User5', p=0),
Cc('[email protected]', 'Example User6', p=0)
]
message.bcc = Bcc('[email protected]', 'Example User7', p=0)
message.bcc = [
Bcc('[email protected]', 'Example User8', p=0),
Bcc('[email protected]', 'Example User9', p=0)
]
message.subject = Subject('Sending with SendGrid is Fun 0', p=0)
message.header = Header('X-Test1', 'Test1', p=0)
message.header = Header('X-Test2', 'Test2', p=0)
message.header = [
Header('X-Test3', 'Test3', p=0),
Header('X-Test4', 'Test4', p=0)
]
message.substitution = Substitution('%name1%', 'Example Name 1', p=0)
message.substitution = Substitution('%city1%', 'Example City 1', p=0)
message.substitution = [
Substitution('%name2%', 'Example Name 2', p=0),
Substitution('%city2%', 'Example City 2', p=0)
]
message.custom_arg = CustomArg('marketing1', 'true', p=0)
message.custom_arg = CustomArg('transactional1', 'false', p=0)
message.custom_arg = [
CustomArg('marketing2', 'false', p=0),
CustomArg('transactional2', 'true', p=0)
]
message.send_at = SendAt(1461775051, p=0)
message.to = To('[email protected]', 'Example User10', p=1)
message.to = [
To('[email protected]', 'Example User11', p=1),
To('[email protected]', 'Example User12', p=1)
]
message.cc = Cc('[email protected]', 'Example User13', p=1)
message.cc = [
Cc('[email protected]', 'Example User14', p=1),
Cc('[email protected]', 'Example User15', p=1)
]
message.bcc = Bcc('[email protected]', 'Example User16', p=1)
message.bcc = [
Bcc('[email protected]', 'Example User17', p=1),
Bcc('[email protected]', 'Example User18', p=1)
]
message.header = Header('X-Test5', 'Test5', p=1)
message.header = Header('X-Test6', 'Test6', p=1)
message.header = [
Header('X-Test7', 'Test7', p=1),
Header('X-Test8', 'Test8', p=1)
]
message.substitution = Substitution('%name3%', 'Example Name 3', p=1)
message.substitution = Substitution('%city3%', 'Example City 3', p=1)
message.substitution = [
Substitution('%name4%', 'Example Name 4', p=1),
Substitution('%city4%', 'Example City 4', p=1)
]
message.custom_arg = CustomArg('marketing3', 'true', p=1)
message.custom_arg = CustomArg('transactional3', 'false', p=1)
message.custom_arg = [
CustomArg('marketing4', 'false', p=1),
CustomArg('transactional4', 'true', p=1)
]
message.send_at = SendAt(1461775052, p=1)
message.subject = Subject('Sending with SendGrid is Fun 1', p=1)
# The values below this comment are global to entire message
message.from_email = From('[email protected]', 'Twilio SendGrid')
message.reply_to = ReplyTo('[email protected]', 'Twilio SendGrid Reply')
message.subject = Subject('Sending with SendGrid is Fun 2')
message.content = Content(MimeType.text, 'and easy to do anywhere, even with Python')
message.content = Content(MimeType.html, '<strong>and easy to do anywhere, even with Python</strong>')
message.content = [
Content('text/calendar', 'Party Time!!'),
Content('text/custom', 'Party Time 2!!')
]
message.attachment = Attachment(FileContent('base64 encoded content 1'),
FileType('application/pdf'),
FileName('balance_001.pdf'),
Disposition('attachment'),
ContentId('Content ID 1'))
message.attachment = [
Attachment(FileContent('base64 encoded content 2'),
FileType('image/png'),
FileName('banner.png'),
Disposition('inline'),
ContentId('Content ID 2')),
Attachment(FileContent('base64 encoded content 3'),
FileType('image/png'),
FileName('banner2.png'),
Disposition('inline'),
ContentId('Content ID 3'))
]
message.template_id = TemplateId('13b8f94f-bcae-4ec6-b752-70d6cb59f932')
message.section = Section('%section1%', 'Substitution for Section 1 Tag')
message.section = [
Section('%section2%', 'Substitution for Section 2 Tag'),
Section('%section3%', 'Substitution for Section 3 Tag')
]
message.header = Header('X-Test9', 'Test9')
message.header = Header('X-Test10', 'Test10')
message.header = [
Header('X-Test11', 'Test11'),
Header('X-Test12', 'Test12')
]
message.category = Category('Category 1')
message.category = Category('Category 2')
message.category = [
Category('Category 1'),
Category('Category 2')
]
message.custom_arg = CustomArg('marketing5', 'false')
message.custom_arg = CustomArg('transactional5', 'true')
message.custom_arg = [
CustomArg('marketing6', 'true'),
CustomArg('transactional6', 'false')
]
message.send_at = SendAt(1461775053)
message.batch_id = BatchId("HkJ5yLYULb7Rj8GKSx7u025ouWVlMgAi")
message.asm = Asm(GroupId(1), GroupsToDisplay([1,2,3,4]))
message.ip_pool_name = IpPoolName("IP Pool Name")
mail_settings = MailSettings()
mail_settings.bcc_settings = BccSettings(False, BccSettingsEmail("[email protected]"))
mail_settings.bypass_list_management = BypassListManagement(False)
mail_settings.footer_settings = FooterSettings(True, FooterText("w00t"), FooterHtml("<string>w00t!<strong>"))
mail_settings.sandbox_mode = SandBoxMode(True)
mail_settings.spam_check = SpamCheck(True, SpamThreshold(5), SpamUrl("https://example.com"))
message.mail_settings = mail_settings
tracking_settings = TrackingSettings()
tracking_settings.click_tracking = ClickTracking(True, False)
tracking_settings.open_tracking = OpenTracking(True, OpenTrackingSubstitutionTag("open_tracking"))
tracking_settings.subscription_tracking = SubscriptionTracking(
True,
SubscriptionText("Goodbye"),
SubscriptionHtml("<strong>Goodbye!</strong>"),
SubscriptionSubstitutionTag("unsubscribe"))
tracking_settings.ganalytics = Ganalytics(
True,
UtmSource("utm_source"),
UtmMedium("utm_medium"),
UtmTerm("utm_term"),
UtmContent("utm_content"),
UtmCampaign("utm_campaign"))
message.tracking_settings = tracking_settings
try:
sendgrid_client = SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
print(json.dumps(message.get(), sort_keys=True, indent=4))
response = sendgrid_client.send(message=message)
print(response.status_code)
print(response.body)
print(response.headers)
except SendGridException as e:
print(e.message)
## Send a Single Email to a Single Recipient with a Dynamic Template
import os
import json
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail, From, To, Subject, PlainTextContent, HtmlContent, SendGridException, DynamicTemplateData
message = Mail(from_email=From('[email protected]', 'Twilio SendGrid'),
to_emails=To('[email protected]', 'Elmer Thomas'),
subject=Subject('Sending with SendGrid is Fun'),
plain_text_content=PlainTextContent('and easy to do anywhere, even with Python'),
html_content=HtmlContent('<strong>and easy to do anywhere, even with Python</strong>'))
message.dynamic_template_data = DynamicTemplateData({
"total":"$ 239.85",
"items":[
{
"text":"New Line Sneakers",
"image":"https://marketing-image-production.s3.amazonaws.com/uploads/8dda1131320a6d978b515cc04ed479df259a458d5d45d58b6b381cae0bf9588113e80ef912f69e8c4cc1ef1a0297e8eefdb7b270064cc046b79a44e21b811802.png",
"price":"$ 79.95"
},
{
"text":"Old Line Sneakers",
"image":"https://marketing-image-production.s3.amazonaws.com/uploads/3629f54390ead663d4eb7c53702e492de63299d7c5f7239efdc693b09b9b28c82c924225dcd8dcb65732d5ca7b7b753c5f17e056405bbd4596e4e63a96ae5018.png",
"price":"$ 79.95"
},
{
"text":"Blue Line Sneakers",
"image":"https://marketing-image-production.s3.amazonaws.com/uploads/00731ed18eff0ad5da890d876c456c3124a4e44cb48196533e9b95fb2b959b7194c2dc7637b788341d1ff4f88d1dc88e23f7e3704726d313c57f350911dd2bd0.png",
"price":"$ 79.95"
}
],
"receipt":True,
"name":"Sample Name",
"address01":"1234 Fake St.",
"address02":"Apt. 123",
"city":"Place",
"state":"CO",
"zip":"80202"
})
try:
print(json.dumps(message.get(), sort_keys=True, indent=4))
sendgrid_client = SendGridAPIClient(api_key=os.environ.get('SENDGRID_API_KEY'))
response = sendgrid_client.send(message=message)
print(response.status_code)
print(response.body)
print(response.headers)
except SendGridException as e:
print(e.message)