Menu
Amazon Simple Email Service
Developer Guide

Send an Email Through Amazon SES Using the AWS SDK for Python (Boto)

This topic shows how to use the AWS SDK for Python (Boto) to send an email through Amazon SES.

Important

In this tutorial, you send an email to yourself so that you can check to see if you received it. For further experimentation or load testing, use the Amazon SES mailbox simulator. Emails that you send to the mailbox simulator do not count toward your sending quota or your bounce and complaint rates. For more information, see Testing Amazon SES Email Sending.

Prerequisites

Before you begin, perform the following tasks:

  • Verify your email address with Amazon SES—Before you can send an email with Amazon SES, you must verify that you own the sender's email address. If your account is still in the Amazon SES sandbox, you must also verify the recipient email address. The easiest way to verify email addresses is by using the Amazon SES console. For more information, see Verification Procedures.

  • Get your AWS credentials—You need an AWS access key ID and AWS secret access key to access Amazon SES using an SDK. You can find your credentials by using the Security Credentials page of the AWS Management Console. For more information about credentials, see Using Credentials With Amazon SES.

  • Install Python—Python is available at https://www.python.org/downloads/. The code in this tutorial was tested using Python 2.7.6. After you install Python, add the path to Python in your environment variables so that you can run Python from any command prompt.

  • Install the AWS SDK for Python (Boto)—For download and installation instructions, see the AWS SDK for Python (Boto) documentation. The sample code in this tutorial was tested using version 1.4.4 of the SDK for Python.

  • Create a shared credentials file—For the sample code in this section to function properly, you must create a shared credentials file. For more information, see Create a Shared Credentials File.

Procedure

The following procedure shows how to send an email through Amazon SES using the SDK for Python.

To send an email through Amazon SES using the SDK for Python

  1. In a text editor, create a file named amazon-ses-sample.py. Paste the following code into the file:

    Copy
    import boto3 # Replace sender@example.com with your "From" address. # This address must be verified with Amazon SES. sender = "sender@example.com" # Replace recipient@example.com with a "To" address. If your account # is still in the sandbox, this address must be verified. recipient = "recipient@example.com" # If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES. awsregion = "us-west-2" # The subject line for the email. subject = "Amazon SES Test (SDK for Python)" # The HTML body of the email. htmlbody = "<h1>Amazon SES Test (SDK for Python)</h1><p>This email was sent with <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the <a href='https://aws.amazon.com/sdk-for-python/'>AWS SDK for Python (Boto)</a>.</p>" # The email body for recipients with non-HTML email clients. textbody = "This email was sent with Amazon SES using the AWS SDK for Python (Boto)" # The character encoding for the email. charset = "UTF-8" # Create a new SES resource and specify a region. client = boto3.client('ses',region_name=awsregion) # Try to send the email. try: #Provide the contents of the email. response = client.send_email( Destination={ 'ToAddresses': [ recipient, ], }, Message={ 'Body': { 'Html': { 'Charset': charset, 'Data': htmlbody, }, 'Text': { 'Charset': charset, 'Data': textbody, }, }, 'Subject': { 'Charset': charset, 'Data': subject, }, }, Source=sender, ) # Display an error if something goes wrong. except Exception as e: print "Error: ", e else: print "Email sent!"
  2. In amazon-ses-sample.py, replace the following with your own values:

    • sender@example.com—Replace with an email address that you have verified with Amazon SES. For more information, see Verifying Email Addresses and Domains. Email addresses in Amazon SES are case-sensitive. Make sure that the address you enter is exactly the same as the one you verified.

    • recipient@example.com—Replace with the address of the recipient. If your account is still in the sandbox, you must verify this address before you use it. For more information, see Moving Out of the Amazon SES Sandbox. Make sure that the address you enter is exactly the same as the one you verified.

    • (Optional) us-west-2—If you want to use Amazon SES in a region other than US West (Oregon), replace this with the region you want to use. For a list of regions in which Amazon SES is available, see Regions and Amazon SES.

  3. Save amazon-ses-sample.py.

  4. To run the program, open a command prompt in the same directory as amazon-ses-sample.py, and type python amazon-ses-sample.py

  5. Review the output. If the email was successfully sent, the console displays "Email sent!" Otherwise, it displays an error message.

  6. Sign in to the email client of the recipient address. You will find the message that you sent.

On this page: