Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_single_email_to_a_single_recipient_content_reversed(self):
"""Tests bug found in Issue-451 with Content ordering causing a crash
"""
from sendgrid.helpers.mail import (Mail, From, To, Subject,
PlainTextContent, HtmlContent)
self.maxDiff = None
message = Mail()
message.from_email = From('test+from@example.com', 'Example From Name')
message.to = To('test+to@example.com', 'Example To Name')
message.subject = Subject('Sending with SendGrid is Fun')
message.content = HtmlContent(
'<strong>and easy to do anywhere, even with Python</strong>')
message.content = PlainTextContent(
'and easy to do anywhere, even with Python')
self.assertEqual(
message.get(),
json.loads(r'''{
"content": [
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('dx@example.com', 'DX')
message.reply_to = ReplyTo('dx_reply@example.com', 'DX 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 = [
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('dx@example.com', 'DX')
message.reply_to = ReplyTo('dx_reply@example.com', 'DX 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'),
FileName('balance_001.pdf'),
FileType('application/pdf'),
def test_unicode_values_in_substitutions_helper(self):
from sendgrid.helpers.mail import (Mail, From, To, Subject,
PlainTextContent, HtmlContent)
self.maxDiff = None
message = Mail(
from_email=From('test+from@example.com', 'Example From Name'),
to_emails=To('test+to@example.com', 'Example To Name'),
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.substitution = Substitution('%city%', u'Αθήνα', p=1)
self.assertEqual(
message.get(),
json.loads(r'''{
"content": [
def test_multiple_emails_to_multiple_recipients(self):
from sendgrid.helpers.mail import (Mail, From, To, Subject,
PlainTextContent, HtmlContent,
Substitution)
self.maxDiff = None
to_emails = [
To(email='test+to0@example.com',
name='Example Name 0',
substitutions=[
Substitution('-name-', 'Example Name Substitution 0'),
Substitution('-github-', 'https://example.com/test0'),
],
subject=Subject('Override Global Subject')),
To(email='test+to1@example.com',
name='Example Name 1',
substitutions=[
Substitution('-name-', 'Example Name Substitution 1'),
Substitution('-github-', 'https://example.com/test1'),
])
]
global_substitutions = Substitution('-time-', '2019-01-01 00:00:00')
message = Mail(
from_email=From('test+from@example.com', 'Example From Name'),
to_emails=to_emails,
subject=Subject('Hi -name-'),
plain_text_content=PlainTextContent(
'Hello -name-, your URL is -github-, email sent at -time-'),
html_content=HtmlContent(
'<strong>Hello -name-, your URL is <a href="\"-github-\"">here</a></strong> email sent at -time-'),
To('test3@example.com', 'Example User3', p=0)
]
message.cc = Cc('test4@example.com', 'Example User4', p=0)
message.cc = [
Cc('test5@example.com', 'Example User5', p=0),
Cc('test6@example.com', 'Example User6', p=0)
]
message.bcc = Bcc('test7@example.com', 'Example User7', p=0)
message.bcc = [
Bcc('test8@example.com', 'Example User8', p=0),
Bcc('test9@example.com', '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)
def send(to_email, subject, html_content):
"""Send email."""
sendgrid_api_key = db_config.get_value('sendgrid_api_key')
if not sendgrid_api_key:
logs.log_warn('Skipping email as SendGrid API key is not set in config.')
return
from_email = db_config.get_value('sendgrid_sender')
if not from_email:
logs.log_warn('Skipping email as SendGrid sender is not set in config.')
return
message = Mail(
from_email=From(str(from_email)),
to_emails=To(str(to_email)),
subject=Subject(subject),
html_content=HtmlContent(str(html_content)))
try:
sg = SendGridAPIClient(sendgrid_api_key)
response = sg.send(message)
logs.log(
'Sent email to %s.' % to_email,
status_code=response.status_code,
body=response.body,
headers=response.headers)
except Exception:
logs.log_error('Failed to send email to %s.' % to_email)
To('test3@sendgrid.com', 'Example User3', p=0)
]
message.cc = Cc('test4@example.com', 'Example User4', p=0)
message.cc = [
Cc('test5@example.com', 'Example User5', p=0),
Cc('test6@example.com', 'Example User6', p=0)
]
message.bcc = Bcc('test7@example.com', 'Example User7', p=0)
message.bcc = [
Bcc('test8@example.com', 'Example User8', p=0),
Bcc('test9@example.com', '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.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('dx@sendgrid.com', 'DX')
message.reply_to = ReplyTo('dx_reply@sendgrid.com', 'DX 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!!')
]