/django-amazon-ses

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

Primary LanguagePythonApache License 2.0Apache-2.0

django-amazon-ses

image

image

image

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

Table of Contents

Installation

First, install the Django Amazon SES email backend:

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 Credential Setup

AWS Named Profile

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

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

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:

Django Configuration

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

Optionally, you can set the AWS credentials. If unset, the backend will gracefully fall back to other Boto 3 credential providers.

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

Alternatively, provide AWS credentials using the settings below. This is useful in situations where you want to use separate credentials to send emails via SES than you would for other AWS services.

If you want to force the use of a SES configuration set you can set the option below. This is useful when you want to do more detailed tracking of your emails such as opens and clicks. You can see more details at: https://docs.aws.amazon.com/ses/latest/DeveloperGuide/using-configuration-sets.html.

Usage

Once the configuration above is complete, use send_email to send email messages with Amazon SES from within your application:

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:

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.

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: