Skip to content

Instantly share code, notes, and snippets.

@kljensen
Created October 4, 2010 13:12
Show Gist options
  • Save kljensen/609663 to your computer and use it in GitHub Desktop.
Save kljensen/609663 to your computer and use it in GitHub Desktop.
#!python
from google.appengine.api import mail
from google.appengine.api import urlfetch
try:
import json
except ImportError:
try:
import simplejson as json
except ImportError:
from django.utils import simplejson as json
class PMEmailMessage(mail.EmailMessage):
""" PMEmailMessage subclass that enables
you to call the pmsend()
method in order to send via Postmark
instead of using GAE's
built-in mail service.
"""
PM_API_KEY = 'PUTYOURKEYHERE'
PM_URL = 'http://api.postmarkapp.com/email'
@classmethod
def get_pm_headers(cls, test=False):
"""docstring for get_pm_headers"""
d = {
'Accept' : 'application/json',
"Content-Type" : "application/json",
}
if test:
k = 'POSTMARK_API_TEST'
else:
k = cls.PM_API_KEY
d['X-Postmark-Server-Token'] = k
return d
@property
def postmark_dict(self):
"""docstring for postmark_dict"""
param_mapping = {
'Headers' : 'headers',
'ReplyTo' : 'reply_to',
'Tag' : 'tag',
'To' : 'to',
'From' : 'sender',
'Cc' : 'cc',
'Bcc' : 'bcc',
'Subject' : 'subject',
'HtmlBody' : 'html',
'TextBody' : 'body',
}
d = {}
for k,v in param_mapping.iteritems():
val = getattr(self, v, None)
if val:
d[k] = val
return d
@property
def postmark_json(self):
"""docstring for postmark_json"""
return json.dumps(self.postmark_dict)
def pmsend(self):
"""docstring for pmsend"""
response = urlfetch.fetch(self.PM_URL,
method=urlfetch.POST,
payload=self.postmark_json,
headers=self.get_pm_headers())
return response
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment