Skip to main content

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

Project description

https://travis-ci.org/azavea/django-amazon-ses.svg?branch=develop https://api.codeclimate.com/v1/badges/b69dce91215b7003066b/maintainability https://api.codeclimate.com/v1/badges/b69dce91215b7003066b/test_coverage

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:

$ 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.

Note: Versions 1.0.x of django-amazon-ses are the last versions compatible with Django versions earlier than 1.11. If you are using Django versions earlier than 1.11.x, please pin your django-amazon-ses version.

AWS Credential Setup

AWS Named Profile

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.EmailBackend'

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

AWS_ACCESS_KEY_ID = 'my_access_key...'
AWS_SECRET_ACCESS_KEY = 'my_secret...'

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

AWS_DEFAULT_REGION = 'eu-west-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.

AWS_SES_ACCESS_KEY_ID = 'my_access_key...'
AWS_SES_SECRET_ACCESS_KEY = 'my_secret...'
AWS_SES_REGION = 'us-west-2'

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.

AWS_SES_CONFIGURATION_SET_NAME = 'my_configuration_set'

Usage

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

from django.core.mail import send_mail

send_mail(
    'Subject here',
    'Here is the message.',
    'from@example.com',
    ['to@example.com'],
    fail_silently=False,
)

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-amazon-ses-3.0.2.tar.gz (10.9 kB view details)

Uploaded Source

Built Distribution

django_amazon_ses-3.0.2-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file django-amazon-ses-3.0.2.tar.gz.

File metadata

  • Download URL: django-amazon-ses-3.0.2.tar.gz
  • Upload date:
  • Size: 10.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

File hashes

Hashes for django-amazon-ses-3.0.2.tar.gz
Algorithm Hash digest
SHA256 f39503d5d16c7788b0ac45d7781100074ff3a19858e5c754031cd2281c7e4658
MD5 74b9b17411379f981701ac0811a4554e
BLAKE2b-256 243a6ec2d989948664c6288d3bdfaafdd950ed3b1d00b9e70d20ae9bf31c7f59

See more details on using hashes here.

File details

Details for the file django_amazon_ses-3.0.2-py3-none-any.whl.

File metadata

  • Download URL: django_amazon_ses-3.0.2-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.1.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/3.6.7

File hashes

Hashes for django_amazon_ses-3.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 d0e79422584e35e24788f60190fb5a1c4809560572e78fd7380a2ae864704ad6
MD5 28285929ef4d6a3831a71935c1752e5f
BLAKE2b-256 8def16371fb057d9bb4b160f759b87e19b902a08e5ff088cbc97083388ba1f6c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page