Send bulk templated email using Amazon SES API

By Renjith R Nair on February 8, 2018

In most cases, while sending bulk email newsletters the email template will be the same. These templates will contain custom tokens which should be replaced accordingly for every destination address. Using Amazon SES sendBulkTemplatedEmail API we can pass tokens and replace text as arguments, then Amazon will do the replace and sending procedure. Quick steps for sending bulk templated email using Amazon SES API is explained below.

Import Amazon aws-sdk-php library to project
use composer to install library to your project

composer require aws/aws-sdk-php

then use these classes,

use Aws\Credentials\Credentials;
use Aws\Ses\SesClient;

Start client instance
Start an Amazon SES client instance using your amazon credentials and set region accordingly. Available regions are email.us-east-1.amazonaws.com, email.us-west-2.amazonaws.com, email.eu-west-1.amazonaws.com

$client = new SesClient([
                            'credentials' => new Credentials(USERNAME, PASSWORD),
                            'region'      => REGION,
                            'version'     => 'latest',
                            ]);

Create template
CreateTemplate API operation can be used to create email templates. These templates include a subject line, and the text and HTML parts of the email body. The subject and body sections may also contain unique values that are personalized for each recipient

$client->createTemplate([
                'Template' => [
				  'TemplateName' => 'MyTemplate', //unique template name
				  'SubjectPart'  => "Hello, {{name}}!",
				  'TextPart'     => "Hello {{name}},
                                                      Accessory for your {{favcar}}.",
                                  'HtmlPart'     => "Hello {{name}},
                                                      \r\nAccessory for your {{favcar}}.",
                                ],
                ]);

Send templated email to different destinations
After creating an email template, you can use it to send email. The SendBulkTemplatedEmail operation is useful for sending unique emails to multiple destinations in a single call to the Amazon SES API.

$client->sendBulkTemplatedEmail([
   'DefaultTemplateData' => '',//JSON dencoded, key as token and value as replacement text.
                                    Common data for any destination
   'Destinations'        => [
        [
           'Destination'    => [
              'BccAddresses' => ['', ...],
              'CcAddresses'  => ['', ...],
              'ToAddresses'  => ['', ...],
            ],
           'ReplacementTemplateData' => '',//template data for current destination
        ],
        // ...
   ],
   'ReplyToAddresses'  => ['', ...],
   'ReturnPath'        => '',
   'Source'            => '',
   'Template'          => '',
]);

Note: Utilize available Amazon SES Exception handlers for error management. Use asynchronous methods to get promise objects, createTemplateAsync for template creation & sendBulkTemplatedEmailAsync to send bulk email.

Leave a Reply

SCROLL TO TOP