aws

Sending Email with SMTP on AWS (and Django)

marcbeaujean

Marc Philippe Beaujean

Posted on January 26, 2020

Sending Email with SMTP on AWS (and Django)

Email is one of the most common ways by which applications communicate with their users. In this tutorial, I will show you guys how to setup Simple Email Service (SES) on Amazon Web Services (AWS), which will allow you to start sending 65000 emails for free each month.

Before we get started, you will need to have an AWS account. It is quite simple to set up, so I won't be going into much detail about this here.

Why bother sending Email with AWS?

Sending automated email can be complicated, because most email providers limit the number of emails you can send in an attempt to prevent spamming and abuse of the service. As a result, most large companies use their own email server that uses SMTP (which stands for Simple Mail Transfer Protocol) and is dedicated to sending emails. As a solo developer or smaller company, the maintenance costs of running your own SMTP server are not going to be worth it. As a result, providers like Mailgun or Amazon offer simpler, less expensive ways to send emails. The reason I will be covering SES (Simple Email Service) by Amazon is because their free tier is by far the most generous and is unlikely to cost you any money for your MVP or side project.

Getting started with SES

The first step after logging into your AWS account and entering the console, is to create a new user. This user is going to have certain permissions that allow him to send emails with SES. Go to the services tab and navigate to the IAM service, which is how AWS manages users and permissions.

Next, we want to create our new user. Navigate to Users on the sidebar and click on the New User button at the top left.

We only care about two things when we are creating this user. On the first view, we need to give them programmatic access.

Next, we need to give our user full access to SES. Click on attach existing policies and add Amazon SES.

After that, you will be given your access key. This can be important if you want to be sending emails without SMTP, which SES also allows. Make sure you store these keys somewhere.

Now, it is time to setup SES. Click on the service.

We need to verify an email from which we can send message out to users. SES makes this process fairly straightforward - go to the Emails tab and click on Verify Email. AWS takes you by the hand from there.

Your email should appear where the black box is right now. AWS will send you a confirmation email to the one that you defined, to confirm that it is legit.

After you confirmed your email, you are not ready to start using SES right away. AWS has an additional layer of security to prevent people from abusing it, called sandbox mode. To get out of sandbox mode, you need to send an email to Amazon support.

Create a case for SES, stating why you want to use it. You will probably get a reply the next day.

We can now get started with SMTP on SES - fortunately, this only takes a few clicks. Go onto the correct tab and create your credentials. Make sure that you store your username and password somewhere.

Sending emails with Django

Now that we have our credentials for using SMTP with SES and we got the confirmation email from Amazon that we are out of sandbox mode, we can get going with sending emails. Add the following configuration code to the settings.py file of your Django app:

EMAIL_BACKEND = 'django_smtp_ssl.SSLEmailBackend'
EMAIL_HOST = 'email-smtp.eu-central-1.amazonaws.com'
EMAIL_PORT = 465
EMAIL_HOST_USER = os.environ.get('AWS_SES_SMTP_USER')
EMAIL_HOST_PASSWORD = os.environ.get('AWS_SES_PASSWORD')
EMAIL_USE_SSL = True

We are using the SMTP credentials we stored earlier. Notice that I am using eu-central-1 in the host url - this needs to be whatever region you are using for AWS.

Now, you should be able to send emails using the standard Django email utility functions. I hope you guys found this short introduction to Amazon SES helpful!

💖 💪 🙅 🚩
marcbeaujean
Marc Philippe Beaujean

Posted on January 26, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related