Skip to content

Instantly share code, notes, and snippets.

@chriskief
Created October 24, 2013 02:48
Embed
What would you like to do?
from email.utils import COMMASPACE
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from boto.ses import SESConnection
from django.conf import settings
class SESMessage(object):
def __init__(self, source, to_addresses, subject, **kw):
self.ses = SESConnection(settings.AWS_ACCESS_KEY, settings.AWS_SECRET_KEY)
self._source = source
self._to_addresses = to_addresses
self._cc_addresses = None
self._bcc_addresses = None
self.subject = subject
self.text = None
self.html = None
self.attachments = []
def send(self):
if not self.ses:
raise Exception, 'No connection found'
if (self.text and not self.html and not self.attachments) or\
(self.html and not self.text and not self.attachments):
return self.ses.send_email(self._source, self.subject,
self.text or self.html,
self._to_addresses, self._cc_addresses,
self._bcc_addresses,
format='text' if self.text else 'html')
else:
if not self.attachments:
message = MIMEMultipart('alternative')
message['Subject'] = self.subject
message['From'] = self._source
if isinstance(self._to_addresses, (list, tuple)):
message['To'] = COMMASPACE.join(self._to_addresses)
else:
message['To'] = self._to_addresses
message.attach(MIMEText(self.text, 'plain'))
message.attach(MIMEText(self.html, 'html'))
else:
raise NotImplementedError, 'Attachments are not currently supported.'
return self.ses.send_raw_email(message.as_string(), source=self._source,
destinations=self._to_addresses)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment