Menu
Amazon Simple Email Service
Developer Guide

Amazon SES Python Code Examples

This section contains the following sample code:

Sending Email Using the AWS SDK for Python (Boto)

The following sample code is a complete solution for sending email through Amazon SES using the AWS SDK for Python (Boto). This sample code assumes that you have installed the AWS SDK for Python (Boto), and that you have created a shared credentials file. For more information about creating a shared credentials file, see Create a Shared Credentials 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!"

Sending Email through the Amazon SES SMTP Interface Using Python

The following sample code is a complete solution for sending email through the Amazon SES SMTP interface using Python. In order to run this sample code, you must obtain SMTP credentials; for more information, see Obtaining Your Amazon SES SMTP Credentials.

Copy
import smtplib import email.utils from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText # 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' # The recipient's name recipientname = 'Recipient Name' # Replace sender@example.com with your "From" address. # This address must be verified. sender = 'sender@example.com' # The sender's name sendername = 'Sender Name' # If you're using Amazon SES in an AWS Region other than US West (Oregon), # replace email-smtp.us-west-2.amazonaws.com with the Amazon SES SMTP # endpoint in the appropriate region. host = "email-smtp.us-west-2.amazonaws.com" port = 465 # Replace smtp_username with your Amazon SES SMTP user name. smtp_username = "smtp_username" # Replace smtp_password with your Amazon SES SMTP password. smtp_password = "smtp_password" # The subject line of the email. subject = 'Amazon SES Test (Python smtplib)' # Create message container - the correct MIME type is multipart/alternative. msg = MIMEMultipart('alternative') msg['Subject'] = subject msg['From'] = email.utils.formataddr((sendername, sender)) msg['To'] = email.utils.formataddr((recipientname, recipient)) # Create the body of the message (a plain-text and an HTML version). text = """\ AWS SES Test\nThis email was sent through the Amazon SES SMTP Interface using the Python smtplib package.""" html = """\ <html> <head></head> <body> <h1>AWS SES SMTP Email Test</h1> <p>This email was sent with Amazon SES using the <a href="https://www.python.org/">Python</a> <a href="https://docs.python.org/3/library/smtplib.html">smtplib</a> library.</p> </body> </html> """ # Record the MIME types of both parts - text/plain and text/html. part1 = MIMEText(text, 'plain') part2 = MIMEText(html, 'html') # Attach parts into message container. # According to RFC 2046, the last part of a multipart message, in this case # the HTML message, is best and preferred. msg.attach(part1) msg.attach(part2) # Try to send the message. If it fails, show the error message. try: server = smtplib.SMTP_SSL(host, port) server.ehlo() server.login(smtp_username, smtp_password) server.sendmail(sender, recipient, msg.as_string()) server.close() except Exception as e: print ("Error: ", e) else: print ("Email sent!")