forked from aschulak/mailgun-python-appengine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailgun.py
28 lines (24 loc) · 948 Bytes
/
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
import urllib
import base64
from google.appengine.api import urlfetch
class Mailgun(object):
def __init__(self, url, api_key, deadline=5):
self.url = url
self.api_key = api_key
self.deadline = deadline
def send_mail(self, sender, to, subject, text, html=None, campaign=None):
payload = {}
payload['from'] = sender
payload['to'] = to
payload['subject'] = subject
payload['text'] = text
if html:
payload['html'] = html
if campaign:
payload['o:campaign'] = campaign
encoded_payload = urllib.urlencode(payload)
base64string = base64.encodestring('api:' + self.api_key).replace('\n', '')
headers = {}
headers['Authorization'] = "Basic %s" % base64string
result = urlfetch.fetch(self.url, deadline=self.deadline, payload=encoded_payload, method=urlfetch.POST, headers=headers)
return result