Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
return False
if not link:
link = url_for('student.index', _external=True)
html = render_template(template, subject=subject, body=body,
link=link, link_text=link_text, **kwargs)
mail = sg_helpers.Mail()
mail.set_from(sg_helpers.Email('no-reply@okpy.org', from_name))
mail.set_subject(subject)
mail.add_content(sg_helpers.Content("text/html", emailFormat(html)))
if reply_to:
mail.set_reply_to(sg_helpers.Email(reply_to))
personalization = sg_helpers.Personalization()
personalization.add_to(sg_helpers.Email(to))
for recipient in cc:
personalization.add_cc(sg_helpers.Email(recipient))
mail.add_personalization(personalization)
try:
response = sg.client.mail.send.post(request_body=mail.get())
except HTTPError:
logger.error("Could not send the email", exc_info=True)
return False
if response.status_code != 202:
logger.error("Could not send email: {} - {}"
.format(response.status_code, response.body))
return False
def _prepare_email_message(self):
"""Prepare email message for Sendgrid
:return: Sendgrid Email message
"""
mail = Mail()
mail.from_email = Email(self.source)
mail.subject = self.subject
# personalization of email
personalization = Personalization()
# add multiple recipients
for to_email in self._convert_var_type_to_list(self.destination):
personalization.add_to(Email(to_email))
mail.add_personalization(personalization)
# add cc (if any)
if self.cc:
for cc_email in self._convert_var_type_to_list(self.cc):
personalization.add_cc(Email(cc_email))
mail.add_personalization(personalization)
# add bcc (if any)
if self.bcc:
for bcc_email in self._convert_var_type_to_list(self.bcc):
personalization.add_bcc(Email(bcc_email))
def send_templated_notification_simple(email, template_id, group_id, category):
"""
Send an email based on a template.
:param str email: The email recipient
:param str template_id: The template ID of the email.
:param str template_id: The group ID of the email.
pass to the email template.
"""
mail = sendgrid.helpers.mail.Mail()
mail.from_email = sendgrid.Email("halite@halite.io", "Halite Challenge")
personalization = sendgrid.helpers.mail.Personalization()
personalization.add_to(sendgrid.helpers.mail.Email(email, email))
mail.add_personalization(personalization)
mail.template_id = template_id
mail.asm = sendgrid.helpers.mail.ASM(group_id, [config.GOODNEWS_ACCOMPLISHMENTS, config.GAME_ERROR_MESSAGES, config.RESEARCH_EMAILS, config.NEWSLETTERS_ARTICLES])
mail.add_category(sendgrid.helpers.mail.Category(category))
settings = sendgrid.helpers.mail.MailSettings()
settings.sandbox_mode = sendgrid.helpers.mail.SandBoxMode(config.SENDGRID_SANDBOX_MODE)
mail.mail_settings = settings
try:
response = sg.client.mail.send.post(request_body=mail.get())
print(response.status_code)
except UnauthorizedError as e:
app.logger.error("Could not send email", exc_info=e)
def _create_email(self, email: dict, email_id: str) -> Mail:
self.log_debug('converting email %s to sendgrid format', email_id)
mail = Mail()
personalization = Personalization()
for i, to in enumerate(email.get('to', [])):
personalization.add_to(Email(to))
self.log_debug('added to %d to email %s', i, email_id)
for i, cc in enumerate(email.get('cc', [])):
personalization.add_cc(Email(cc))
self.log_debug('added cc %d to email %s', i, email_id)
for i, bcc in enumerate(email.get('bcc', [])):
personalization.add_bcc(Email(bcc))
self.log_debug('added bcc %d to email %s', i, email_id)
mail.add_personalization(personalization)
self.log_debug('added recipients to email %s', email_id)
def send_email(self, to_email, subject, from_email=None, html=None, text=None, *args, **kwargs): # noqa
if not any([from_email, self.default_from]):
raise ValueError("Missing from email and no default.")
if not any([html, text]):
raise ValueError("Missing html or text.")
self.from_email = Email(from_email or self.default_from)
self.subject = subject
personalization = Personalization()
if type(to_email) is list:
for email in self._extract_emails(to_email):
personalization.add_to(email)
elif type(to_email) is Email:
personalization.add_to(to_email)
elif type(to_email) is str:
personalization.add_to(Email(to_email))
self.add_personalization(personalization)
content = Content("text/html", html) if html else Content("text/plain", text)
self.add_content(content)
return self.client.mail.send.post(request_body=self.get())
def send_templated_notification(recipient, template_id, substitutions, group_id, category):
"""
Send an email based on a template.
:param Recipient recipient: The recipient of the email
:param str template_id: The template ID of the email.
:param Dict[str, Any] substitutions: Any other substitution variables to
:param str group_id: The group ID of the email.
pass to the email template.
"""
mail = sendgrid.helpers.mail.Mail()
if not recipient.organization:
recipient = recipient._replace(organization="(no affiliation)")
mail.from_email = sendgrid.Email("halite@halite.io", "Halite Challenge")
personalization = sendgrid.helpers.mail.Personalization()
personalization.add_to(sendgrid.helpers.mail.Email(recipient.email, recipient.username))
all_substitutions = itertools.chain(
recipient._asdict().items(), substitutions.items())
for substitution_key, substitution_value in all_substitutions:
personalization.add_substitution(sendgrid.helpers.mail.Substitution(
"-{}-".format(substitution_key), substitution_value))
mail.add_personalization(personalization)
mail.template_id = template_id
mail.asm = sendgrid.helpers.mail.ASM(group_id, [config.GOODNEWS_ACCOMPLISHMENTS, config.GAME_ERROR_MESSAGES, config.RESEARCH_EMAILS, config.NEWSLETTERS_ARTICLES])
mail.add_category(sendgrid.helpers.mail.Category(category))
settings = sendgrid.helpers.mail.MailSettings()
settings.sandbox_mode = sendgrid.helpers.mail.SandBoxMode(config.SENDGRID_SANDBOX_MODE)
mail.mail_settings = settings