Skip to main content

A Django email backend that uses Boto 3 to interact with Amazon Simple Email Service (SES).

Usage

First, install the Django Amazon SES email backend:

$ pip install django-amazon-ses

Next, ensure that your Amazon Web Services (AWS) API credentials are setup, or that you are running on an Amazon EC2 instance with an instance profile that has access to the Amazon SES service.

AWS API Credential Setup

Create an AWS API credential profile named test using the AWS CLI:

$ aws --profile test configure

Ensure that the AWS_PROFILE environment variable is set so that Boto 3 knows which credentials profile to use:

$ AWS_PROFILE="test" gunicorn my:app

AWS EC2 Instance Profile

Create an instance profile with at least the ses:SendRawEmail action. Then, associate it with the instance/s running your application. An example policy that enables access to the ses:SendRawEmail action is below:

{
   "Version": "2012-10-17",
   "Statement": [
      {
         "Effect": "Allow",
         "Action": ["ses:SendRawEmail"],
         "Resource":"*"
      }
   ]
}

Django Configuration

Lastly, override the EMAIL_BACKEND setting within your Django settings file:

EMAIL_BACKEND = 'django_amazon_ses.backends.boto.EmailBackend'

Optionally, you can set the AWS region to be used (default is 'us-east-1'):

DJANGO_AMAZON_SES_REGION = 'eu-west-1'

Signals

Two signals are provided for the backend, pre_send and post_send. Both signals receive the message object being sent. The post_send signal also receives the Amazon SES message ID of the sent message.

pre_send

You can modify the email message on pre_send. For example, if you have a blacklist of email addresses that should never receive emails, you can filter them from the recipients:

from django.dispatch.dispatcher import receiver
from django_amazon_ses.backends.boto import pre_send

@receiver(pre_send)
def remove_blacklisted_emails(sender, message=None, **kwargs):
    blacklisted_emails = Blacklisted.objects.values_list('email', flat)
    message.to = [email for email in message.to if email not in blacklisted_emails]

If the pre_send receiver function ends up removing all of the recipients from the message, the email is not processed and the post_send signal is not sent.

post_send

Similarly, the post_send signal can be used to log messages sent by the system. This is useful if you want to log the subject line of a message that bounced or received a complaint.

from django.dispatch.dispatcher import receiver
from django.utils import timezone

from django_amazon_ses.backends.boto import post_send

@receiver(post_send)
def log_message(sender, message=None, message_id=None, **kwargs):
    SentMessage.objects.create(
        subject = message.subject,
        body = message.body,
        message_id = message_id,
        date_sent = timezone.now()
    )

Testing

The test suite execution process is managed by tox and takes care to mock out the Boto 3 interactions with Amazon’s API, so there is no need for a valid set of credentials to execute it:

$ tox

Release files for django-amazon-ses 0.3.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-amazon-ses 0.3.2
File Size Uploaded
django-amazon-ses-0.3.2.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-amazon-ses 0.3.2
File Interpreter ABI Platform
django_amazon_ses-0.3.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 20.6 kB

Release files / django-amazon-ses-0.3.2.tar.gz

Download URL django-amazon-ses-0.3.2.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
1b19d13e836a45730fa931b8c4e85cb9cbbfabbb10bf0d956cca2edfc01bfb59
BLAKE2b-256 checksum
How to use checksums
372b582496fc520586a21f432af95e83e6f1c136a9fd304754e65c1c82fd3597
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / django_amazon_ses-0.3.2-py2.py3-none-any.whl

Download URL django_amazon_ses-0.3.2-py2.py3-none-any.whl
Size 11.0 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
7b9c7abac9dbdb4526b636060535305bb32feab4ca5514f03075a237191308da
BLAKE2b-256 checksum
How to use checksums
99bc6785e0944be0532bbf1432aac1c8a20c25fc849d7b53a560ed9d452c029a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

4.0.1

2 release files

4.0.0

2 release files

3.0.2

2 release files

3.0.1

2 release files

2.1.1

2 release files

2.1.0

2 release files

2.0.0

2 release files

1.0.0

2 release files

This release

0.3.2 This release

2 release files

0.3.1

1 release file

0.3.0

1 release file

0.1.3

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page