google.cloud.forseti.common.util.email module

Email utility module.

class EmailUtil(api_key)[source]

Bases: object

Utility for sending emails.

static _add_recipients(email, email_recipients)[source]

Add multiple recipients to the sendgrid email object.

Parameters:
  • email (SendGrid) – SendGrid mail object
  • email_recipients (Str) – comma-separated text of the email recipients
Returns:

SendGrid mail object with mulitiple recipients.

Return type:

SendGrid

_execute_send(**kw)[source]

Executes the sending of the email.

This needs to be a standalone method so that we can wrap it with retry, and the final exception can be gracefully handled upstream.

Parameters:email (SendGrid) – SendGrid mail object
Returns:urllib2 response object
Return type:dict
classmethod create_attachment(file_location, content_type, filename, disposition='attachment', content_id=None)[source]

Create a SendGrid attachment.

SendGrid attachments file content must be base64 encoded.

Parameters:
  • file_location (str) – The path of the file.
  • content_type (str) – The content type of the attachment.
  • filename (str) – The filename of attachment.
  • disposition (str) – Content disposition, defaults to “attachment”.
  • content_id (str) – The content id.
Returns:

A SendGrid Attachment.

Return type:

Attachment

classmethod render_from_template(template_file, template_vars)[source]

Fill out an email template with template variables.

Parameters:
  • template_file (str) – The location of email template in filesystem.
  • template_vars (dict) – The template variables to fill into the template.
Returns:

The template content, rendered with the provided variables.

Return type:

str

send(email_sender=None, email_recipient=None, email_subject=None, email_content=None, content_type=None, attachment=None)[source]

Send an email.

This uses the SendGrid API. https://github.com/sendgrid/sendgrid-python

The minimum required info to send email are: sender, recipient, subject, and content (the body)

Parameters:
  • 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.