How to send HTML mails using Amazon SES (Simple Email Service) in python

As the title suggests, I was looking for a way to send HTML mails in python using Amazon SES but did not find anything (or maybe my search skills are bad).
So, once I found the solution, I thought I might share it with everyone.

The basic idea is that contents of the mail (raw-message) must be a JSON dictionary with “Data” as main key whose value is a key value pair containing entries with keys like “From”, “To” etc.
Sample code can be seen below.

import commands
import json
import logging
import sys
import tempfile

def sendEmail(mail_sender, mail_subject, mail_content, mail_receivers_list):
  mail_sender: str, sender's email address.
  mail_subject: str, subject of the email.
  mail_content: str, content (html data) of the email.
  mail_receivers_list: str, comma separated list of receivers.
  # This style is specific to AWS SES.
  message_dict = { 'Data':
  'From: ' + mail_sender + '\n'
  'To: ' + mail_receivers_list + '\n'
  'Subject: ' + mail_subject + '\n'
  'MIME-Version: 1.0\n'
  'Content-Type: text/html;\n\n' +
    _, message_dict_filename = tempfile.mkstemp()
    f = open(message_dict_filename,'w')
  except IOError as e:
  aws_send_email_cmd = [
      'aws', 'ses', 'send-raw-email', '--region=us-east-1',
      '--raw-message=file://%s' % message_dict_filename]
  result = commands.getstatusoutput(' '.join(aws_send_email_cmd))'Sending message with subject "%s", result:%s.', mail_subject,
  if result[0] != 0:
    logging.error('Mail sending failed. Command: %s',
        ' '.join(aws_send_email_cmd))
  # It is usually a good idea to delete the file "f" at this stage.

def main():
  sendEmail('', 'mail subject', 'content of the mail',

if __name__ == '__main__':

2 Replies to “How to send HTML mails using Amazon SES (Simple Email Service) in python”

  1. is there any way not to define the region? I want to use the method but not for a particular region, can you help me with that?

  2. Afaik, the region parameter is mandatory, you can try executing the code by omitting it but I don’t think it will succeed.

Leave a Reply