Sending Email with SMTP on AWS (and Django)
Tags: aws
Published: 2020-01-26 12:30:13

<!-- wp:paragraph -->
<p>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 <a href="https://aws.amazon.com/about-aws/whats-new/2011/12/13/amazon-simple-email-service-gets-simpler-with-smtp/">Simple Email Service (SES)</a> on Amazon Web Services (AWS), which will allow you to start sending 65000 emails for free each month.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Before we get started, you will need to have an <a href="https://portal.aws.amazon.com/billing/signup?nc2=h_ct&src=header_signup&redirect_url=https%3A%2F%2Faws.amazon.com%2Fregistration-confirmation#/start">AWS account</a>. It is quite simple to set up, so I won't be going into much detail about this here.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Why bother sending Email with AWS?</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3>Getting started with SES</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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 <em>IAM</em> service, which is how AWS manages users and permissions.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":499,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/01/iam-service-1024x171.png" alt="" class="wp-image-499"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":500,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/01/IAM_view.png" alt="" class="wp-image-500"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>We only care about two things when we are creating this user. On the first view, we need to give them programmatic access.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":501,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/01/add_user.png" alt="" class="wp-image-501"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Next, we need to give our user full access to SES. Click on attach existing policies and add Amazon SES.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":502,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/01/ses_permissions.png" alt="" class="wp-image-502"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":503,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/01/access_keys.png" alt="" class="wp-image-503"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Now, it is time to setup SES. Click on the service.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":504,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/01/email_service-1024x480.png" alt="" class="wp-image-504"/></figure></div>
<!-- /wp:image -->

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

<!-- wp:image {"id":505,"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/01/email-verify.png" alt="" class="wp-image-505"/></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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. </p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":506,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/01/support-case-1024x203.png" alt="" class="wp-image-506"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>Create a case for SES, stating why you want to use it. You will probably get a reply the next day.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":507,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/01/ses_smtp.png" alt="" class="wp-image-507"/></figure></div>
<!-- /wp:image -->

<!-- wp:heading {"level":3} -->
<h3>Sending emails with Django</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>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:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code">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</pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>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.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":508,"sizeSlug":"large"} -->
<div class="wp-block-image"><figure class="aligncenter size-large"><img src="https://wp.jdit.eu/wp-content/uploads/2020/01/ses-servername.png" alt="" class="wp-image-508"/></figure></div>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>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!</p>
<!-- /wp:paragraph -->