Skip to content

Instantly share code, notes, and snippets.

@voscausa
Created November 27, 2011 16:39
Show Gist options
  • Save voscausa/1397784 to your computer and use it in GitHub Desktop.
Save voscausa/1397784 to your computer and use it in GitHub Desktop.
Google App engine Python 2.5 version of pankratiev / python-amazon-ses-api, using method override
#!/usr/bin/python
# -*- coding: utf-8 -*-
from ek_settings import AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_ACCESS_KEY
from amazon_ses import AmazonSES, EmailMessage
from google.appengine.api import urlfetch
from google.appengine.runtime import DeadlineExceededError
import urllib, logging
#===============================================================================================================================
# GAE vesion of amszon_ses : ovverride performAction and sendMail methods of AmazonSES
# Added support for 1 or more reply_to addresses
# No support for sending attachments. For this you have to use another SES API : send raw e-mails
#===============================================================================================================================
class gaeAmazonSES(AmazonSES): # make it work on GAE Pyton 2.5
def _performAction(self, actionName, params=None):
if not params:
params = {}
params['Action'] = actionName
#https://email.us-east-1.amazonaws.com/
reason = '' # exception
retry = 0 # download error retry
while retry <= 1 : # a one time retry
try :
url = 'https://email.us-east-1.amazonaws.com'
response = urlfetch.fetch(url=url, payload=urllib.urlencode(params), method=urlfetch.POST, headers=self._getHeaders())
break
except (urlfetch.DownloadError, DeadlineExceededError),e :
logging.debug('Amazon SES download or deadline error : %d' %(retry + 1))
if retry == 0 :
retry = retry + 1
continue # retry
else :
reason = e
break
if reason == '' : # check for fetch errors
responseResult = response.content
status_code = response.status_code
logging.debug(response.headers)
else :
responseResult = None
status_code = -1
result = self._responseParser.parse(actionName, status_code, reason, responseResult)
logging.debug(result)
return result
def sendEmail(self, source, toAddresses, message, replyToAddresses=None, returnPath=None, ccAddresses=None, bccAddresses=None):
params = { 'Source': source }
if not replyToAddresses : replyToAddresses = [source,] # always reply_to
for objName, addresses in zip(["ToAddresses", "CcAddresses", "BccAddresses", "ReplyToAddresses" ],
[toAddresses, ccAddresses, bccAddresses, replyToAddresses]):
if addresses:
if not isinstance(addresses, basestring) and getattr(addresses, '__iter__', False):
for i, address in enumerate(addresses): # change for 2.5
if objName == 'ReplyToAddresses' : # reply_to
params['ReplyToAddresses.member.%d' %(i + 1)] = address # change for 2.5
else : # destination
params['Destination.%s.member.%d' % (objName, i + 1)] = address
else:
if objName == 'ReplyToAddresses' :
params['ReplyToAddresses.member.1'] = addresses
else :
params['Destination.%s.member.1' % objName] = addresses
if not returnPath:
returnPath = source
params['ReturnPath'] = returnPath
params['Message.Subject.Charset'] = message.charset
params['Message.Subject.Data'] = message.subject.encode('utf-8')
if message.bodyText:
params['Message.Body.Text.Charset'] = message.charset
params['Message.Body.Text.Data'] = message.bodyText.encode('utf-8')
if message.bodyHtml:
params['Message.Body.Html.Charset'] = message.charset
params['Message.Body.Html.Data'] = message.bodyHtml.encode('utf-8')
logging.debug(params)
return self._performAction('SendEmail', params)
def sesMail(sender, mail_to, subject, body_text, reply_to=None, bcc=None): # use the API
amazonSes = gaeAmazonSES(AMAZON_ACCESS_KEY_ID, AMAZON_SECRET_ACCESS_KEY) # keys in my_settings
message = EmailMessage()
message.subject = subject
message.bodyText = body_text
result = amazonSes.sendEmail(sender, mail_to, message, reply_to,'[email protected]', None, bcc)
logging.debug(result)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment