This repository has been archived by the owner on Jun 5, 2023. It is now read-only.
/
sendgrid_connector.py
executable file
·187 lines (152 loc) · 6.7 KB
/
sendgrid_connector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# Copyright 2017 The Forseti Security Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Sendgrid email connector module."""
# The pre-commit linter will complain about useless disable of no-member, but
# this is needed because quiet the Sendgrid no-member error on Travis.
# pylint: disable=no-member,useless-suppression
import base64
import urllib.request
import urllib.error
import urllib.parse
from future import standard_library
import sendgrid
from sendgrid.helpers import mail
from retrying import retry
from google.cloud.forseti.common.util import errors as util_errors
from google.cloud.forseti.common.util import logger
from google.cloud.forseti.common.util import retryable_exceptions
from google.cloud.forseti.common.util.email import base_email_connector
standard_library.install_aliases()
LOGGER = logger.get_logger(__name__)
class SendgridConnector(base_email_connector.BaseEmailConnector):
"""Utility for sending emails using Sendgrid API Key."""
def __init__(self, sender, recipient, auth, custom=None):
"""Initialize the email util.
Args:
sender (str): The email sender.
recipient (str): The email recipient.
auth (dict): A set of authentication attributes
corresponding to the selected connector.
custom (dict): A set of custom attributes.
"""
self.sender = sender
self.recipient = recipient
if auth.get('api_key'):
api_key = auth.get('api_key')
# else block below is for backward compatibility
else:
api_key = auth.get('sendgrid_api_key')
self.sendgrid = sendgrid.SendGridAPIClient(apikey=api_key)
if custom:
LOGGER.warning('Unable to process custom attributes: %s', custom)
@retry(retry_on_exception=retryable_exceptions.is_retryable_exception,
wait_exponential_multiplier=1000, wait_exponential_max=10000,
stop_max_attempt_number=5)
def _execute_send(self, email):
"""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.
Args:
email (SendGrid): SendGrid mail object
Returns:
dict: urllib2 response object
"""
return self.sendgrid.client.mail.send.post(request_body=email.get())
@staticmethod
def _add_recipients(email, email_recipients):
"""Add multiple recipients to the sendgrid email object.
Args:
email (SendGrid): SendGrid mail object
email_recipients (Str): comma-separated text of the email recipients
Returns:
SendGrid: SendGrid mail object with multiple recipients.
"""
personalization = mail.Personalization()
recipients = email_recipients.split(',')
for recipient in recipients:
personalization.add_to(mail.Email(recipient))
email.add_personalization(personalization)
return email
def send(self, email_sender=None, email_recipient=None,
email_subject=None, email_content=None, content_type=None,
attachment=None):
"""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)
Args:
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.warning('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 urllib.error.HTTPError as e:
LOGGER.exception('Unable to send email: %s %s',
e.code, e.reason)
raise util_errors.EmailSendError
if response.status_code == 202:
LOGGER.info('Email accepted for delivery:\n%s',
email_subject)
else:
LOGGER.error('Unable to send email:\n%s\n%s\n%s\n%s',
email_subject, response.status_code,
response.body, response.headers)
raise util_errors.EmailSendError
@classmethod
def create_attachment(cls,
file_location=None,
content_type=None,
filename=None,
disposition='attachment',
content_id=None):
"""Create a SendGrid attachment.
SendGrid attachments file content must be base64 encoded.
Args:
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:
Attachment: A SendGrid Attachment.
"""
file_content = ''
with open(file_location, 'rb') as f:
file_content = f.read()
content = base64.b64encode(file_content)
attachment = mail.Attachment()
attachment.content = content.decode('utf-8')
attachment.type = content_type
attachment.filename = filename
attachment.disposition = disposition
attachment.content_id = content_id
return attachment