Harry Marr

Using Amazon's Simple Email Service (SES) with Django

Yesterday, Amazon announced their Simple Email Service (SES). Sending emails from EC2 instances has been a well-known problem for some time, due to the likelihood that messages coming from there will often be marked as spam (supposedly Hotmail blacklist all EC2 IPs). The announcement of SES appears to be the cure to this problem. It also removes the need to setup your own SMTP server on EC2 - another thing off your mind that has now become Amazon’s problem. Here’s a quick guide to sending emails through SES from Django.

First, you’ll need to sign up for SES. Once you’ve signed up, follow the instructions on the getting started guide for verifying email address that you want to use to send mail from. To start with, you’ll only be able to send email to these verified addresses as well - to send emails to anyone, you’ll need to request production access.

The next stage is integrating the service with a Django project. I’ll assume you’ve got an already running Django project, with a recent (1.2+) version of Django installed on your system. You need two libraries to get going. Firstly, you should install Boto, the Python library that wraps the AWS API. The stable version doesn’t have support for SES just yet as it was only released yesterday (although I’ve submitted a patch, pending approval), but you can install my fork with SES support by running the following command My patch that adds SES support has been merged in to Boto, so install the usual way:

$ pip install boto

The you need to install the Django-SES library. It isn’t on PyPI just yet, so follow a similar procedure for installing this:

$ pip install django-ses

Django-SES needs access to your AWS API key ID and secret key. These can either be available in environment variables (see here for more details), or you can add them to your settings.py:

AWS_ACCESS_KEY_ID = 'YOUR-ACCESS-KEY-ID'
AWS_SECRET_ACCESS_KEY = 'YOUR-SECRET-ACCESS-KEY'

The final step is to tell Django to use SES by default. Add the following to your settings.py:

EMAIL_BACKEND = 'django_ses.SESBackend'

That’s it! Now all emails sent using django.core.mail.send_mail should go through SES.