Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
)
reset_email = reset_email.replace(
"{{action_url}}", f"{appConfig.FROM_WEBSITE_URL}/reset/{reset.UUID}"
)
name = user.firstName
if not name:
name = user.emailAddress
reset_email = reset_email.replace("{{name}}", name)
from_email = Email(appConfig.FROM_EMAIL)
to_email = Email(user.emailAddress)
subject = f"Password reset requested at {appConfig.FROM_ORG_NAME}"
content = Content("text/html", reset_email)
return Mail(from_email, subject, to_email, content)
def send_single_email(email, subject, template, template_arguments):
""" Send an email using the SendGrid API
Args:
- email :string => the user's work email (ie username@company.com)
- subject :string => the subject line for the email
- template :string => the template file, corresponding to the email sent.
- template_arguments :dictionary => keyword arguments to specify to render_template
Returns:
- SendGrid response
"""
load_secrets()
env = Environment(loader=PackageLoader('yelp_beans', 'templates'))
template = env.get_template(template)
rendered_template = template.render(template_arguments)
message = mail.Mail(
from_email=mail.Email(SENDGRID_SENDER),
subject=subject,
to_email=mail.Email(email),
content=Content("text/html", rendered_template)
)
return send_grid_client.client.mail.send.post(request_body=message.get())
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))
"""
Send an email.
:param tuple to_email: email recipient address and name
:param basestring subject: email subject
:param basestring content: email content
"""
if not get_secret('sendgrid_key'):
# This is a test machine
return
try:
sg = sendgrid.SendGridAPIClient(apikey=get_secret('sendgrid_key'))
content = Content('text/plain', content)
mail = Mail(Email(*from_email), subject, Email(to_email[0]), content)
sg.client.mail.send.post(request_body=mail.get())
except Exception:
traceback.print_exc()
def send_simple_message(recipient):
# [START sendgrid-send]
sg = sendgrid.SendGridAPIClient(apikey=SENDGRID_API_KEY)
to_email = mail.Email(recipient)
from_email = mail.Email(SENDGRID_SENDER)
subject = 'This is a test email'
content = mail.Content('text/plain', 'Example message.')
message = mail.Mail(from_email, subject, to_email, content)
response = sg.client.mail.send.post(request_body=message.get())
return response
# [END sendgrid-send]
def send_email(subject, to, template_name, context):
sendgrid_client = sendgrid.SendGridAPIClient(settings.SENDGRID_API_KEY)
from_email = settings.DEFAULT_FROM_EMAIL
to_emails = to
content = render_to_string(template_name, context)
mail = Mail(from_email, to_emails, subject, content)
return sendgrid_client.send(mail)
email_sender (str): The email sender.
email_recipient (str): The email recipient.
email_subject (str): The email subject.
email_content (str): The email content (aka, body).
content_type (str): The email content type.
attachment (Attachment): A SendGrid Attachment.
Raises:
EmailSendError: An error with sending email has occurred.
"""
if not email_sender or not email_recipient:
LOGGER.warn('Unable to send email: sender=%s, recipient=%s',
email_sender, email_recipient)
raise util_errors.EmailSendError
email = mail.Mail()
email.from_email = mail.Email(email_sender)
email.subject = email_subject
email.add_content(mail.Content(content_type, email_content))
email = self._add_recipients(email, email_recipient)
if attachment:
email.add_attachment(attachment)
try:
response = self._execute_send(email)
except urllib2.HTTPError as e:
LOGGER.error('Unable to send email: %s %s',
e.code, e.reason)
raise util_errors.EmailSendError