This repository has been archived by the owner on Aug 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathsendgrid_email.py
39 lines (34 loc) · 1.61 KB
/
sendgrid_email.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
import os
import sendgrid
from sendgrid.helpers.mail import Content, Email, Mail
from bs4 import BeautifulSoup
class SendGrid(object):
"""Send an email through SendGrid"""
def __init__(self):
# Check if we are not in heroku
sendgrid_api_key = os.environ.get('SENDGRID_APY_KEY') if \
os.environ.get('ENV') != 'prod' else os.environ['SENDGRID_API_KEY']
self.sendgrid = sendgrid.SendGridAPIClient(apikey=sendgrid_api_key)
def send_email(self, to_email, from_email, subject, body):
"""Send the email
:param to_email: who the email is going to
(e.g. 'First Last <[email protected]>')
:param from_email: who the email is coming from
(e.g. 'First Last <[email protected]>')
:param subject: the email subject line
:param body: the email body in HTML format
:type to_email: string
:type from_email: string
:type subject: string
:type body: string
:returns: HTML status code and JSON message from SendGrid's API
:rtype: Integer, JSON
"""
from_email = Email(from_email)
subject = subject
to_email = Email(to_email)
soup = BeautifulSoup(body, "html.parser")
content = Content("text/plain", soup.get_text())
mail = Mail(from_email, subject, to_email, content)
response = self.sendgrid.client.mail.send.post(request_body=mail.get())
return response.status_code, response.body