-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathflask_mailgun.py
42 lines (32 loc) · 1.13 KB
/
flask_mailgun.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
import requests
class Mailgun(object):
app = None
mailgun_api = None
def __init__(self, app=None):
if app:
self.init_app(app)
def init_app(self, app):
self.mailgun_api = MailgunApi(app.config['MAILGUN_DOMAIN'],
app.config['MAILGUN_API_KEY'])
self.app = app
def send_email(self, **kwargs):
if not self.mailgun_api:
raise ValueError('A valid app instance has not been provided')
default_from = self.app.config.get('MAILGUN_DEFAULT_FROM')
if default_from:
kwargs.setdefault('from', default_from)
return self.mailgun_api.send_email(**kwargs)
class MailgunApi(object):
def __init__(self, domain, api_key):
self.domain = domain
self.api_key = api_key
def send_email(self, **kwargs):
response = requests.post(self.endpoint, data=kwargs, auth=self.auth)
response.raise_for_status()
return response
@property
def endpoint(self):
return 'https://api.mailgun.net/v2/{}/messages'.format(self.domain)
@property
def auth(self):
return ('api', self.api_key)