Sending emails with Python and Amazon SES

Transactionnal emails with python-emails and Amazon SES

There are tons of reasons you may want to use transactionnal emails in your software.
For instance, I use it to send reports when a task is completed, or alerts when something went wrong during a process.

I used a few different solutions in the past, from setting up my own SMTP server to using Email as a Service providers. Each one came with its own issues.
I’ve been using AWS Simple Email Service for a while, and for the first time I can say this works like a charm.
Plus it’s free for 10,000 emails per month, which is a lot more than what I need.

Here is a quick recap on how I set it up with Python.

Sign up to AWS and verify your email

The Quick Start handles the very basic first steps to creating your account and sending your first email.
You’ll need to:

  1. Sign up to AWS SES
  2. Verify your email adress or your domain
  3. Check if everything works fine by sending an email via SES console

Once everything is in order, follow these instructions to obtain a username and password for the SMTP servers.

Send emails with Amazon SES in Python

A few different solutions are available for emails in Python. My favorite is emails, which is very simple and effective.
Install it using pip in the command line:

$ pip install emails  

You can then send emails in any Python script with only a few lines of code. To use Amazon SES with SMTP, you simply need to use port 587 and TLS authentication. The rest is quite straigt forward.
Here’s a quick example:

import emails

# Prepare the email
message = emails.html(
    html="<h1>My message</h1><strong>I've got something to tell you!</strong>",
    subject="A very important message",

# Send the email
r = message.send(
        "host": "my-aws-smtp-server", 
        "port": 587, 
        "timeout": 5,
        "user": "my-aws-smtp-user",
        "password": "my-aws-smtp-pass",
        "tls": True,

# Check if the email was properly sent
assert r.status_code == 250  

You can use the emails library with any other SMTP host, as well as attach files and a lot of other features. Check the doc to know more!

Let's work together !

Contact me !