-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathflask_sendgrid.py
More file actions
executable file
·82 lines (68 loc) · 2.52 KB
/
flask_sendgrid.py
File metadata and controls
executable file
·82 lines (68 loc) · 2.52 KB
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
# -*- coding: utf-8 -*-
"""
flask_sendgrid
~~~~
Adds SendGrid support to Flask applications
"""
from sendgrid import SendGridAPIClient
from sendgrid.helpers.mail import Mail as SGMail
from sendgrid.helpers.mail import Email, Content, Personalization
__version__ = '0.7.1'
__versionfull__ = __version__
class SendGrid(SGMail):
app = None
api_key = None
client = None
default_from = None
def __init__(self, app=None, **opts):
if app:
self.init_app(app)
super(SGMail, self).__init__()
self.from_email = None
self.subject = None
self._personalizations = None
self._contents = None
self._attachments = None
self._template_id = None
self._sections = None
self._headers = None
self._categories = None
self._custom_args = None
self._send_at = None
self._batch_id = None
self._asm = None
self._ip_pool_name = None
self._mail_settings = None
self._tracking_settings = None
self._reply_to = None
def init_app(self, app):
self.app = app
self.api_key = app.config['SENDGRID_API_KEY']
self.default_from = app.config['SENDGRID_DEFAULT_FROM']
self.client = SendGridAPIClient(self.api_key).client
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 _extract_emails(self, emails):
if type(emails[0]) is Email:
for email in emails:
yield email
elif type(emails[0]) is dict:
for email in emails:
yield Email(email['email'])