Amazon Simple Email Service
Developer Guide

Send an Email Using the AWS SDK for Ruby

This topic shows how to use the AWS SDK for Ruby to send an email through Amazon SES.


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 Email Sending in Amazon SES.


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 Verifying Email Addresses in Amazon SES.

  • 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 Ruby—Ruby is available at The code in this tutorial was tested using Ruby 1.9.3. After you install Ruby, add the path to Ruby in your environment variables so that you can run Ruby from any command prompt.

  • Install the AWS SDK for Ruby—For download and installation instructions, see Installing the AWS SDK for Ruby in the AWS SDK for Ruby Developer Guide. The sample code in this tutorial was tested using version 2.9.36 of the AWS SDK for Ruby.

  • 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.


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

To send an email through Amazon SES using the AWS SDK for Ruby

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

    require 'aws-sdk' # Replace with your "From" address. # This address must be verified with Amazon SES. sender = "" # Replace with a "To" address. If your account # is still in the sandbox, this address must be verified. recipient = "" # Specify a configuration set. If you do not want to use a configuration # set, comment the following variable and the # configuration_set_name: configsetname argument below. configsetname = "ConfigSet" # 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 (AWS SDK for Ruby)" # The HTML body of the email. htmlbody = '<h1>Amazon SES test (AWS SDK for Ruby)</h1>'\ '<p>This email was sent with <a href="">'\ 'Amazon SES</a> using the <a href="">'\ 'AWS SDK for Ruby</a>.' # The email body for recipients with non-HTML email clients. textbody = "This email was sent with Amazon SES using the AWS SDK for Ruby." # Specify the text encoding scheme. encoding = "UTF-8" # Create a new SES resource and specify a region ses = awsregion) # Try to send the email. begin # Provide the contents of the email. resp = ses.send_email({ destination: { to_addresses: [ recipient, ], }, message: { body: { html: { charset: encoding, data: htmlbody, }, text: { charset: encoding, data: textbody, }, }, subject: { charset: encoding, data: subject, }, }, source: sender, # Comment or remove the following line if you are not using # a configuration set configuration_set_name: configsetname, }) puts "Email sent!" # If something goes wrong, display an error message. rescue Aws::SES::Errors::ServiceError => error puts "Email not sent. Error message: #{error}" end
  2. In amazon-ses-sample.rb, replace the following with your own values:

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

    •—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 where Amazon SES is available, see Amazon Simple Email Service (Amazon SES) in the AWS General Reference.

  3. Save amazon-ses-sample.rb.

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

  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: