Package: django-amazon-ses
A Django email backend that usesBoto 3to interact withAmazon Simple Email Service (SES).Table of ContentsInstallationAWS Credential SetupAWS Named ProfileAWS EC2 Instance ProfileDjango ConfigurationUsageSignalspre_sendpost_sendTestingInstallationFirst, install the Django Amazon SES email backend:$pipinstalldjango-amazon-sesNext, 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 SetupAWS Named ProfileCreate an AWS API credential profile namedtestusing theAWS CLI:$aws--profiletestconfigureEnsure that theAWS_PROFILEenvironment variable is set so that Boto 3 knows which credentials profile to use:$AWS_PROFILE="test"gunicornmy:appAWS EC2 Instance ProfileCreate aninstance profilewith at least theses:SendRawEmailaction. Then, associate it with the instance/s running your application. An example policy that enables access to theses:SendRawEmailaction is below:{"Version":"2012-10-17","Statement":[{"Effect":"Allow","Action":["ses:SendRawEmail"],"Resource":"*"}]}Django ConfigurationLastly, override theEMAIL_BACKENDsetting 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'UsageOnce the configuration above is complete, usesend_emailto send email messages with Amazon SES from within your application:fromdjango.core.mailimportsend_mailsend_mail('Subject here','Here is the message.','from@example.com',['to@example.com'],fail_silently=False,)SignalsTwo signals are provided for the backend,pre_sendandpost_send. Both signals receive the message object being sent. Thepost_sendsignal also receives the Amazon SES message ID of the sent message.pre_sendYou can modify the email message onpre_send. For example, if you have a blacklist of email addresses that should never receive emails, you can filter them from the recipients:fromdjango.dispatch.dispatcherimportreceiverfromdjango_amazon_sesimportpre_send@receiver(pre_send)defremove_blacklisted_emails(sender,message=None,**kwargs):blacklisted_emails=Blacklisted.objects.values_list('email',flat)message.to=[emailforemailinmessage.toifemailnotinblacklisted_emails]If thepre_sendreceiver function ends up removing all of the recipients from the message, the email is not processed and thepost_sendsignal is not sent.post_sendSimilarly, thepost_sendsignal 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.fromdjango.dispatch.dispatcherimportreceiverfromdjango.utilsimporttimezonefromdjango_amazon_sesimportpost_send@receiver(post_send)deflog_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())TestingThe 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