Sending email using AWS SES service

Amazon Simple Email Service (Amazon SES) is very simple and cost effective way to send emails. You can use AWS SES service to send customer notifications, marketing messages, or any other type using high quality rich content email themes.

These are the few simple steps to start with AWS SES services:

1) Sign-up for AWS Account

This will be a prerequisite to use any service. If you do not have an AWS account, you can sign-up for a new account by going to https://aws.amazon.com and following up the sign-up instruction on the page.

2) Verify your email address or domain

AWS requires you to authenticate each sender’s address to prevent any misuse from any unauthorized usage your email address. This process will associate sender’s email address (or domain) to your AWS account.

Here are the steps to to verify your mail address:

a) Log in the AWS management console

b) Open the SES Management Console

c) Click the link ‘Email Address’ on the left hand side panel.

d) You may verify a new email address by clicking the ‘Verify Email Address’ on the top.

Once you complete the process, you will get a verification email on the email address you have provided in this process. Please follow the instructions in the email to verify your  email address.

3) Request for production access

By default AWS provides you a sandbox environment to test your applications. If you want to use your AWS account for sending emails, you will have to raise a request to grant you the production access.

4) Write an application / script to send email

I this example, I’ll be using PHP script to send an email.

I have created two files:

Config.php

This is file where we keep our AWS access identifier. In my example, this is the content of the file:


// Enter your Access Key ID
$AWSAccessKeyId = 'YOUR ACCESS KEY';

// Enter your Secret Access Key
$AWSSecretKey = 'YOUR SECRET KEY';

// Enter your Region
$AWSRegion = 'YOUR AWS REGION, eg, us-east-1';

Note: This would require you Access Key / Secret access Key.  If you haven’t created one, please go AWS IAM Management console to create a user.

SendEmail.php

This is the main script. Here are the steps to create a basic scrip to send email:

a) Start with prerequisite. I’ll be using the AWS composer from the AWS sample installation.

//Include AWS credentials

require ‘config.php’;

// Include the SDK using the Composer autoloader and use SES client namespace
require ‘aws-php-sample/vendor/autoload.php’;
use Aws\Common\Aws;
use Aws\Ses\SesClient;

In this case AWS PHP sample has been installed in aws-php-sample.

b) I’ll be using AWS factory method reate a new Amazon Simple Email Service client using an array of configuration options:


/* Reference to the ses client */
$client = SesClient::factory(array(
   'key' => $AWSAccessKeyId,
   'secret' => $AWSSecretKey,
   'region' => $AWSRegion
));

c) Now the next thing would be create the email message to send. First we will add the sender’s email address, To, CC, BCC etc. Sender’s email address should be the one that you validated in the step 2.


//Now that you have the client ready, you can build the message
$msg = array();
$msg['Source'] = "someone@example.com";
//ToAddresses must be an array
$msg['Destination'] = array(
   'ToAddresses' => array('someone@example.com'),
   'CcAddresses' => array('someone@example.com' ),
   'BccAddresses' => array('someone@example.com')
);

Next we will form the body of the email:

$msg['Message'] = array(
     // Subject is required
     'Subject' => array(
       // Data is required
      'Data' => 'Test Msg',
      'Charset' => 'UTF-8',
   ),
   // Body
  'Body' => array(
    'Text' => array(
     // Data
     'Data' => 'This is a test msg',
     'Charset' => 'UTF-8',
     ),
    'Html' => array(
     // Data is required
     'Data' => 'Dear Customer, <br /> This is a test message. <br /> Regards <br /> Prakash Kumar <br />',
     'Charset' => 'UTF-8',
    ),
  )
);

Now you are all set. You call sendEmail to send the email:

try{
$result = $client->sendEmail($msg);

//Msg id from AWS. Can be used for tracking.
$msg_id = $result->get('MessageId');
echo("MessageId: $msg_id");

//view sample output
print_r($result);
} catch (Exception $e) {
//Exception Caught.
echo($e->getMessage());
}

//Print the result to check if it is successful
print_r($result);

This is a very basic example. You can do a lot using the AWS SES API. This is fully capable of handling outbound production email for any size of business.

Please feel free to reach me by posting a question. Please contact me if you need source code. I’ll be happy to share the same with you.