@@ -13,7 +13,7 @@ def build_hello_email():
13
13
from sendgrid import SendGridAPIClient
14
14
from sendgrid .helpers .mail import Mail , From , To , Subject , PlainTextContent , HtmlContent , SendGridException
15
15
16
- message = Mail (
from_email = From (
'[email protected] .com ' ,
'Example From Name' ),
16
+ message = Mail (
from_email = From (
'[email protected] ' ,
'Example From Name' ),
17
17
to_emails = To (
'[email protected] ' ,
'Example To Name' ),
18
18
subject = Subject ('Sending with SendGrid is Fun' ),
19
19
plain_text_content = PlainTextContent ('and easy to do anywhere, even with Python' ),
@@ -26,25 +26,30 @@ def build_hello_email():
26
26
except SendGridException as e :
27
27
print (e .message )
28
28
29
- for cc_addr in personalization ['cc_list' ]:
29
+ mock_personalization = Personalization ()
30
+ personalization_dict = get_mock_personalization_dict ()
31
+
32
+ for cc_addr in personalization_dict ['cc_list' ]:
30
33
mock_personalization .add_to (cc_addr )
31
34
32
- for bcc_addr in personalization ['bcc_list' ]:
35
+ for bcc_addr in personalization_dict ['bcc_list' ]:
33
36
mock_personalization .add_bcc (bcc_addr )
34
37
35
- for header in personalization ['headers' ]:
38
+ for header in personalization_dict ['headers' ]:
36
39
mock_personalization .add_header (header )
37
40
38
- for substitution in personalization ['substitutions' ]:
41
+ for substitution in personalization_dict ['substitutions' ]:
39
42
mock_personalization .add_substitution (substitution )
40
43
41
- for arg in personalization ['custom_args' ]:
44
+ for arg in personalization_dict ['custom_args' ]:
42
45
mock_personalization .add_custom_arg (arg )
43
46
44
- mock_personalization .subject = personalization ['subject' ]
45
- mock_personalization .send_at = personalization ['send_at' ]
46
- return mock_personalization
47
+ mock_personalization .subject = personalization_dict ['subject' ]
48
+ mock_personalization .send_at = personalization_dict ['send_at' ]
49
+
50
+ message .add_personalization (mock_personalization )
47
51
52
+ return message
48
53
49
54
def get_mock_personalization_dict ():
50
55
"""Get a dict of personalization mock."""
@@ -78,6 +83,36 @@ def get_mock_personalization_dict():
78
83
mock_pers ['send_at' ] = 1443636843
79
84
return mock_pers
80
85
86
+ def build_multiple_emails_personalized ():
87
+ import json
88
+ from sendgrid .helpers .mail import Mail , From , To , Cc , Bcc , Subject , PlainTextContent , \
89
+ HtmlContent , SendGridException , Personalization
90
+
91
+ # Note that the domain for all From email addresses must match
92
+ message = Mail (
from_email = From (
'[email protected] ' ,
'Example From Name' ),
93
+ subject = Subject ('Sending with SendGrid is Fun' ),
94
+ plain_text_content = PlainTextContent ('and easy to do anywhere, even with Python' ),
95
+ html_content = HtmlContent ('<strong>and easy to do anywhere, even with Python</strong>' ))
96
+
97
+ mock_personalization = Personalization ()
98
+ mock_personalization .
add_to (
To (
'[email protected] ' ,
'Example User 1' ))
99
+ mock_personalization .
add_cc (
Cc (
'[email protected] ' ,
'Example User 2' ))
100
+ message .add_personalization (mock_personalization )
101
+
102
+ mock_personalization_2 = Personalization ()
103
+ mock_personalization_2 .
add_to (
To (
'[email protected] ' ,
'Example User 3' ))
104
+ mock_personalization_2 .
set_from (
From (
'[email protected] ' ,
'Example From Name 2' ))
105
+ mock_personalization_2 .
add_bcc (
Bcc (
'[email protected] ' ,
'Example User 4' ))
106
+ message .add_personalization (mock_personalization_2 )
107
+
108
+ try :
109
+ print (json .dumps (message .get (), sort_keys = True , indent = 4 ))
110
+ return message .get ()
111
+
112
+ except SendGridException as e :
113
+ print (e .message )
114
+
115
+ return message
81
116
82
117
def build_attachment1 ():
83
118
"""Build attachment mock. Make sure your content is base64 encoded before passing into attachment.content.
@@ -308,6 +343,15 @@ def build_kitchen_sink():
308
343
309
344
return message
310
345
346
+ def send_multiple_emails_personalized ():
347
+ # Assumes you set your environment variable:
348
+ # https://github.com/sendgrid/sendgrid-python/blob/HEAD/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key
349
+ message = build_multiple_emails_personalized ()
350
+ sendgrid_client = SendGridAPIClient (os .environ .get ('SENDGRID_API_KEY' ))
351
+ response = sendgrid_client .send (message = message )
352
+ print (response .status_code )
353
+ print (response .body )
354
+ print (response .headers )
311
355
312
356
def send_hello_email ():
313
357
# Assumes you set your environment variable:
@@ -334,5 +378,8 @@ def send_kitchen_sink():
334
378
## this will actually send an email
335
379
# send_hello_email()
336
380
381
+ ## this will send multiple emails
382
+ # send_multiple_emails_personalized()
383
+
337
384
## this will only send an email if you set SandBox Mode to False
338
385
# send_kitchen_sink()
0 commit comments