-
Notifications
You must be signed in to change notification settings - Fork 714
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
148 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,9 @@ | |
from sendgrid.helpers.mail import * | ||
from sendgrid import * | ||
|
||
# NOTE: you will need move this file to the root directory of this project to execute properly. | ||
# NOTE: you will need move this file to the root | ||
# directory of this project to execute properly. | ||
|
||
|
||
def build_hello_email(): | ||
"""Minimum required to send an email""" | ||
|
@@ -17,66 +19,150 @@ def build_hello_email(): | |
|
||
return mail.get() | ||
|
||
def build_kitchen_sink(): | ||
"""All settings set""" | ||
mail = Mail() | ||
|
||
mail.from_email = Email("[email protected]", "Example User") | ||
def build_personalization(personalization): | ||
"""Build personalization mock instance from a mock dict""" | ||
mock_personalization = Personalization() | ||
|
||
mail.subject = "Hello World from the SendGrid Python Library" | ||
for to_addr in personalization['to_list']: | ||
personalization.add_to(to_addr) | ||
|
||
personalization = Personalization() | ||
personalization.add_to(Email("[email protected]", "Example User")) | ||
personalization.add_to(Email("[email protected]", "Example User")) | ||
personalization.add_cc(Email("[email protected]", "Example User")) | ||
personalization.add_cc(Email("[email protected]", "Example User")) | ||
personalization.add_bcc(Email("[email protected]")) | ||
personalization.add_bcc(Email("[email protected]")) | ||
personalization.subject = "Hello World from the Personalized SendGrid Python Library" | ||
personalization.add_header(Header("X-Test", "test")) | ||
personalization.add_header(Header("X-Mock", "true")) | ||
personalization.add_substitution(Substitution("%name%", "Example User")) | ||
personalization.add_substitution(Substitution("%city%", "Denver")) | ||
personalization.add_custom_arg(CustomArg("user_id", "343")) | ||
personalization.add_custom_arg(CustomArg("type", "marketing")) | ||
personalization.send_at = 1443636843 | ||
mail.add_personalization(personalization) | ||
|
||
personalization2 = Personalization() | ||
personalization2.add_to(Email("[email protected]", "Example User")) | ||
personalization2.add_to(Email("[email protected]", "Example User")) | ||
personalization2.add_cc(Email("[email protected]", "Example User")) | ||
personalization2.add_cc(Email("[email protected]", "Eric Shallock")) | ||
personalization2.add_bcc(Email("[email protected]")) | ||
personalization2.add_bcc(Email("[email protected]")) | ||
personalization2.subject = "Hello World from the Personalized SendGrid Python Library" | ||
personalization2.add_header(Header("X-Test", "test")) | ||
personalization2.add_header(Header("X-Mock", "true")) | ||
personalization2.add_substitution(Substitution("%name%", "Example User")) | ||
personalization2.add_substitution(Substitution("%city%", "Denver")) | ||
personalization2.add_custom_arg(CustomArg("user_id", "343")) | ||
personalization2.add_custom_arg(CustomArg("type", "marketing")) | ||
personalization2.send_at = 1443636843 | ||
mail.add_personalization(personalization2) | ||
for cc_addr in personalization['cc_list']: | ||
personalization.add_to(cc_addr) | ||
|
||
mail.add_content(Content("text/plain", "some text here")) | ||
mail.add_content(Content("text/html", "<html><body>some text here</body></html>")) | ||
for bcc_addr in personalization['bcc_list']: | ||
personalization.add_bc(bcc_addr) | ||
|
||
personalization.subject = personalization['subject'] | ||
|
||
for header in personalization['headers']: | ||
personalization.add_header(header) | ||
|
||
for substitution in personalization['substitutions']: | ||
personalization.add_substitution(substitution) | ||
|
||
for arg in personalization['custom_args']: | ||
personalization.add_custom_arg(arg) | ||
|
||
personalization.send_at = personalization['send_at'] | ||
return mock_personalization | ||
|
||
|
||
def get_mock_personalization_dict(): | ||
"""Get a dict of personalization mock.""" | ||
mock_pers = dict() | ||
|
||
mock_pers['to_list'] = [Email("[email protected]", | ||
"Example User"), | ||
Email("[email protected]", | ||
"Example User")] | ||
|
||
mock_pers['cc_list'] = [Email("[email protected]", | ||
"Example User"), | ||
Email("[email protected]", | ||
"Example User")] | ||
|
||
mock_pers['bcc_list'] = [Email("[email protected]"), | ||
Email("[email protected]")] | ||
|
||
mock_pers['subject'] = ("Hello World from the Personalized " | ||
"SendGrid Python Library") | ||
|
||
mock_pers['headers'] = [Header("X-Test", "test"), | ||
Header("X-Mock", "true")] | ||
|
||
mock_pers['substitutions'] = [Substitution("%name%", "Example User"), | ||
Substitution("%city%", "Denver")] | ||
|
||
mock_pers['custom_args'] = [CustomArg("user_id", "343"), | ||
CustomArg("type", "marketing")] | ||
|
||
mock_pers['send_at'] = 1443636843 | ||
return mock_pers | ||
|
||
|
||
def build_attachment1(): | ||
"""Build attachment mock.""" | ||
attachment = Attachment() | ||
attachment.content = "TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12" | ||
attachment.content = ("TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNl" | ||
"Y3RldHVyIGFkaXBpc2NpbmcgZWxpdC4gQ3JhcyBwdW12") | ||
attachment.type = "application/pdf" | ||
attachment.filename = "balance_001.pdf" | ||
attachment.disposition = "attachment" | ||
attachment.content_id = "Balance Sheet" | ||
mail.add_attachment(attachment) | ||
return attachment | ||
|
||
|
||
def build_attachment2(): | ||
"""Build attachment mock.""" | ||
attachment = Attachment() | ||
attachment.content = "BwdW" | ||
attachment.type = "image/png" | ||
attachment.filename = "banner.png" | ||
attachment.disposition = "inline" | ||
attachment.content_id = "Banner" | ||
return attachment | ||
|
||
|
||
def build_mail_settings(): | ||
"""Build mail settings mock.""" | ||
mail_settings = MailSettings() | ||
mail_settings.bcc_settings = BCCSettings(True, Email("[email protected]")) | ||
mail_settings.bypass_list_management = BypassListManagement(True) | ||
mail_settings.footer_settings = FooterSettings(True, "Footer Text", | ||
("<html><body>Footer " | ||
"Text</body></html>")) | ||
mail_settings.sandbox_mode = SandBoxMode(True) | ||
mail_settings.spam_check = SpamCheck(True, 1, | ||
"https://spamcatcher.sendgrid.com") | ||
return mail_settings | ||
|
||
|
||
def build_tracking_settings(): | ||
"""Build tracking settings mock.""" | ||
tracking_settings = TrackingSettings() | ||
tracking_settings.click_tracking = ClickTracking(True, True) | ||
tracking_settings.open_tracking = OpenTracking(True, | ||
("Optional tag to " | ||
"replace with the" | ||
"open image in the " | ||
"body of the message")) | ||
|
||
subs_track = SubscriptionTracking(True, | ||
("text to insert into the " | ||
"text/plain portion of the" | ||
" message"), | ||
("<html><body>html to insert " | ||
"into the text/html portion of " | ||
"the message</body></html>"), | ||
("Optional tag to replace with " | ||
"the open image in the body of " | ||
"the message")) | ||
|
||
tracking_settings.subscription_tracking = subs_track | ||
tracking_settings.ganalytics = Ganalytics(True, "some source", | ||
"some medium", "some term", | ||
"some_content", "some_campaign") | ||
return tracking_settings | ||
|
||
|
||
def build_kitchen_sink(): | ||
"""All settings set""" | ||
mail = Mail() | ||
|
||
mail.from_email = Email("[email protected]", "Example User") | ||
mail.subject = "Hello World from the SendGrid Python Library" | ||
|
||
personalization = get_mock_personalization_dict() | ||
mail.add_personalization(build_personalization(personalization)) | ||
mail.add_personalization(build_personalization(personalization)) | ||
|
||
mail.add_content(Content("text/plain", "some text here")) | ||
mail.add_content(Content("text/html", ("<html><body>some text " | ||
"here</body></html>"))) | ||
|
||
attachment2 = Attachment() | ||
attachment2.content = "BwdW" | ||
attachment2.type = "image/png" | ||
attachment2.filename = "banner.png" | ||
attachment2.disposition = "inline" | ||
attachment2.content_id = "Banner" | ||
mail.add_attachment(attachment2) | ||
mail.add_attachment(build_attachment1()) | ||
mail.add_attachment(build_attachment2()) | ||
|
||
mail.template_id = "13b8f94f-bcae-4ec6-b752-70d6cb59f932" | ||
|
||
|
@@ -94,32 +180,18 @@ def build_kitchen_sink(): | |
|
||
mail.send_at = 1443636842 | ||
|
||
# This must be a valid [batch ID](https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) to work | ||
# This must be a valid [batch ID] | ||
# (https://sendgrid.com/docs/API_Reference/SMTP_API/scheduling_parameters.html) to work | ||
# mail.set_batch_id("N2VkYjBjYWItMGU4OC0xMWU2LWJhMzYtZjQ1Yzg5OTBkNzkxLWM5ZTUyZjNhOA") | ||
|
||
mail.asm = ASM(99, [4, 5, 6, 7, 8]) | ||
|
||
mail.ip_pool_name = "24" | ||
|
||
mail_settings = MailSettings() | ||
mail_settings.bcc_settings = BCCSettings(True, Email("[email protected]")) | ||
mail_settings.bypass_list_management = BypassListManagement(True) | ||
mail_settings.footer_settings = FooterSettings(True, "Footer Text", "<html><body>Footer Text</body></html>") | ||
mail_settings.sandbox_mode = SandBoxMode(True) | ||
mail_settings.spam_check = SpamCheck(True, 1, "https://spamcatcher.sendgrid.com") | ||
mail.mail_settings = mail_settings | ||
|
||
tracking_settings = TrackingSettings() | ||
tracking_settings.click_tracking = ClickTracking(True, True) | ||
tracking_settings.open_tracking = OpenTracking(True, "Optional tag to replace with the open image in the body of the message") | ||
tracking_settings.subscription_tracking = SubscriptionTracking(True, "text to insert into the text/plain portion of the message", "<html><body>html to insert into the text/html portion of the message</body></html>", "Optional tag to replace with the open image in the body of the message") | ||
tracking_settings.ganalytics = Ganalytics(True, "some source", "some medium", "some term", "some_content", "some_campaign") | ||
mail.tracking_settings = tracking_settings | ||
|
||
mail.mail_settings = build_mail_settings() | ||
mail.tracking_settings = build_tracking_settings() | ||
mail.reply_to = Email("[email protected]") | ||
|
||
return mail.get() | ||
|
||
|
||
def send_hello_email(): | ||
# Assumes you set your environment variable: | ||
# https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key | ||
|
@@ -130,6 +202,7 @@ def send_hello_email(): | |
print(response.headers) | ||
print(response.body) | ||
|
||
|
||
def send_kitchen_sink(): | ||
# Assumes you set your environment variable: | ||
# https://github.com/sendgrid/sendgrid-python/blob/master/TROUBLESHOOTING.md#environment-variables-and-your-sendgrid-api-key | ||
|
@@ -140,5 +213,9 @@ def send_kitchen_sink(): | |
print(response.headers) | ||
print(response.body) | ||
|
||
send_hello_email() # this will actually send an email | ||
send_kitchen_sink() # this will only send an email if you set SandBox Mode to False | ||
|
||
# this will actually send an email | ||
send_hello_email() | ||
|
||
# this will only send an email if you set SandBox Mode to False | ||
send_kitchen_sink() |