Setting up AWS SES Email Backend for Production

May 1, 2020, 1:52 p.m.
Django AWS Production · 4 min read
Setting up AWS SES Email Backend for Production

Let's sign up for Amazon's Simple Email Service (SES) so our contact form will work in production.  Previous information on setting up Django's email backend can be found in Build a Django Contact Form with Email Backend.


Create an AWS account

First, create an AWS free account at AWS offers a 12-month free tier for all of the tools you'll need to deploy your web app. You will need to enter your billing and credit card information to create an account but you will not be charged unless you go over the free tier threshold.

AWS create account


Create a set of access keys

To connect your project to AWS, we need to create a set of AWS keys. Like a username and password, access keys come with an access key ID and a secret access key that only you should know. AWS gives you only one opportunity to view and save the access keys together to maintain security.

Click on your username at the top right side of the page. In the dropdown menu, select My Security Credentials. Find the section that says Access keys for CLI, SDK, & API access. Click the button Create access key. You will be brought to a Create access key menu that states Your new access key is now available. Two keys are listed below. The first is the Access key ID and the second is the Secret access key. Click the button Download .csv file and save the access keys in a safe location on your computer. You can only view the secret key once on the AWS menu so make sure you download the .csv before closing the menu.

NOTE: If you do happen to lose or forget to save the set of access keys, simply create a new access key and make sure to save the file.

AWS access keys gifGIF


Connect the AWS email backend to Django

macOS Terminal

(Ctrl + C)

(env)User-Macbook:mysite user$ pip install django-ses

Windows Command Prompt

(Ctrl + C)

(env) C:\Users\Owner\desktop\code\env\mysite>pip install django-ses

Go to the CLI where your project is running and quit the server. Install django-ses to the project. This Python package routes emails through Amazon Web Services SES instead of a traditional SMTP mail server.


env > mysite > mysite >


# EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
EMAIL_BACKEND = 'django_ses.SESBackend'
AWS_SES_REGION_NAME = 'REGION-NAME' #(ex: us-east-2)

Go to the file of your project. Comment out the Django Core EMAIL_BACKEND and add the code above to the file. django-ses is a drop-in replacement for Django Core Mail and requires your unique AWS set of access keys.

Change 'YOUR-ACCESS-KEY-ID' and 'YOUR-SECRET-ACCESS-KEY' to your unique access keys created earlier. Change the 'REGION_NAME' to a nearby region. AWS SES only supports us-east-1 (N. Virginia) and us-west-2 is (Oregon). Change the 'REGION_ENDPOINT' address to match the region selected. All fields should be strings so the quotation marks are required for all fields.

We highly recommend using Python Decouple to properly secure these Django configuration settings from attackers.


Verify your email address

It's time to verify the email address you wish to use in the Django project in AWS SES. Go to AWS Management Console page and click on for the Simple Email Service (SES) in the search menu. Once on the SES Home page, click the Email Addresses tab on the left side menu.

Click the button "Verify this email address" and enter the email address you want to receive the Django web app emails. This is the email specified in the Django file. Go to the inbox of the email you just verified and find the email from AWS. Click the URL link in the email to verify the email address.

verify email address


Change the email address in the

env > mysite > mysite >

def contact(request):
	if request.method == 'POST':
		form = ContactForm(request.POST)
		if form.is_valid():
			subject = "Summer Camp Inquiry" 
		    body = {
			'first_name': form.cleaned_data['first_name'], 
			'last_name': form.cleaned_data['last_name'], 
			'email': form.cleaned_data['email'], 
			message = "\n".join(body.values())

				send_mail(subject, message, 'verified_address', ['verified_address']) 
			except BadHeaderError: #add this
				return HttpResponse('Invalid header found.') #add this
	form = ContactForm()
	return render(request, "main/contact.html", {'form':form})

Change the 'from' and 'to' addresses from to the AWS verified email address in the contact view. Keep the quotation marks around the email addresses and save the file. Amazon SES only allows emails to be sent and received by AWS verified email addresses unless you upgrade your account out of the Amazon SES sandbox. This is unnecessary since we include a user's email and info in the body of the message.

Save all of the changes, run the server again, and now fill out the contact form in the browser. Instead of checking the CLI go to the verified email's inbox. All submitted contact forms will now appear in that inbox.

Post a Comment
Join the community

Written By
I enjoy writing about saas, founders, and Django. Let me know if I should write about you!