forked from sendgrid/sendgrid-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_v2_test.py
executable file
·64 lines (57 loc) · 2.08 KB
/
example_v2_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
import sendgrid
import os
if os.path.exists('.env'):
for line in open('.env'):
var = line.strip().split('=')
if len(var) == 2:
os.environ[var[0]] = var[1]
sg = sendgrid.SendGridClient(os.environ.get('SENDGRID_API_KEY'))
"""
# Basic Send Example
message = sendgrid.Mail()
message.add_to('John Doe <example@example.com>')
message.set_subject('Testing from the Python library')
message.set_html('<b>This was a successful test!</b>')
message.set_text('This was a successful test!')
message.set_from('Jane Doe <example@example.com')
status, msg = sg.send(message)
print status
print msg
# SMTPAPI Basic Send Example
message = sendgrid.Mail()
message.add_substitution(':first_name', 'John')
message.smtpapi.add_to('John Doe <example@example.com>')
message.set_subject('Testing from the Python library using the SMTPAPI')
message.set_html('<b>:first_name, this was a successful test of using the SMTPAPI library!</b>')
message.set_text(':name, this was a successful test of using the SMTPAPI library!')
message.set_from('Jane Doe <example@example.com>')
status, msg = sg.send(message)
print status
print msg
# Template Engine Example
# In the template editor, the subject is <%subject%> and the body is:
#
# Hello :name,
#
# <%body%>
#
# With Best Regards,
#
# Your Library Tester
#
# <%subject%> is replaced with the value in message.set_subject
# <%body%> is replaced with the value in message.set_html and message.set_text
# :name is replaced with the value in message.add_substitution
message = sendgrid.Mail()
message.add_filter('templates', 'enable', '1')
message.add_filter('templates', 'template_id', 'TEMPLATE-ALPHA-NUMERIC-ID')
message.add_substitution(':name', 'John')
message.add_to('John Doe <example@example.com')
message.set_subject('Testing from the Python library using the SMTPAPI')
message.set_html('<b>This was a successful test of using the SMTPAPI library!</b>')
message.set_text('This was a successful test of using the SMTPAPI library!')
message.set_from('Jane Doe <example@example.com>')
status, msg = sg.send(message)
print status
print msg
"""