AWS SDK for PHP
Developer Guide

Creating Custom Email Templates Using the Amazon SES API and the AWS SDK for PHP Version 3

Amazon Simple Email Service (Amazon SES) enables you to send emails that are personalized for each recipient by using templates. Templates include a subject line and the text and HTML parts of the email body. The subject and body sections can also contain unique values that are personalized for each recipient.

For more information, see Sending Personalized Email Using the Amazon SES in the Amazon SES Developer Guide.

The following examples show how to:

All the example code for the AWS SDK for PHP Version 3 is available here on GitHub.

Credentials

Before running the example code, configure your AWS credentials, as described in Credentials for the AWS SDK for PHP Version 3. Then import the AWS SDK for PHP, as described in Basic Usage Patterns of the AWS SDK for PHP Version 3.

For more information about using Amazon SES, see the Amazon SES Developer Guide.

Create an Email Template

To create a template to send personalized email messages, use the CreateTemplate operation. The template can be used by any account authorized to send messages in the AWS Region to which the template is added.

Note

Amazon SES doesn't validate your HTML, so be sure that HtmlPart is valid before sending an email.

Imports

require 'vendor/autoload.php'; use Aws\SES\SESClient; use Aws\Exception\AwsException;

Sample Code

$SesClient = new Aws\SES\SESClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $name = 'Template_Name'; $html_body = '<h1>AWS Amazon Simple Email Service Test Email</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-php/">' . 'AWS SDK for PHP</a>.</p>'; $subject = 'Amazon SES test (AWS SDK for PHP)'; $plaintext_body = 'This email was send with Amazon SES using the AWS SDK for PHP.'; try { $result = $SesClient->createTemplate([ 'Template' => [ 'HtmlPart' => $html_body, 'SubjectPart' => $subject, 'TemplateName' => $name, 'TextPart' => $plaintext_body, ], ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

Get an Email Template

To view the content for an existing email template including the subject line, HTML body, and plain text, use the GetTemplate operation. Only TemplateName is required.

Imports

require 'vendor/autoload.php'; use Aws\SES\SESClient; use Aws\Exception\AwsException;

Sample Code

$SesClient = new Aws\SES\SESClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $name = 'Template_Name'; try { $result = $SesClient->getTemplate([ 'TemplateName' => $name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

List All Email Templates

To retrieve a list of all email templates that are associated with your AWS account in the current AWS Region, use the ListTemplates operation.

Imports

require 'vendor/autoload.php'; use Aws\SES\SESClient; use Aws\Exception\AwsException;

Sample Code

$SesClient = new Aws\SES\SESClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); try { $result = $SesClient->listTemplates([ 'MaxItems' => 10, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

Update an Email Template

To change the content for a specific email template including the subject line, HTML body, and plain text, use the UpdateTemplate operation.

Imports

require 'vendor/autoload.php'; use Aws\SES\SESClient; use Aws\Exception\AwsException;

Sample Code

$SesClient = new Aws\SES\SESClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $name = 'Template_Name'; $html_body = '<h1>AWS Amazon Simple Email Service Test Email</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-php/">' . 'AWS SDK for PHP</a>.</p>'; $subject = 'Amazon SES test (AWS SDK for PHP)'; $plaintext_body = 'This email was send with Amazon SES using the AWS SDK for PHP.'; try { $result = $SesClient->updateTemplate([ 'Template' => [ 'HtmlPart' => $html_body, 'SubjectPart' => $subject, 'TemplateName' => $name, 'TextPart' => $plaintext_body, ], ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

Delete an Email Template

To remove a specific email template, use the DeleteTemplate operation. All you need is the TemplateName.

Imports

require 'vendor/autoload.php'; use Aws\SES\SESClient; use Aws\Exception\AwsException;

Sample Code

$SesClient = new Aws\SES\SESClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $name = 'Template_Name'; try { $result = $SesClient->deleteTemplate([ 'TemplateName' => $name, ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }

Send an Email with a Template

To use a template to send an email to recipients, use the SendTemplatedEmail operation.

Imports

require 'vendor/autoload.php'; use Aws\SES\SESClient; use Aws\Exception\AwsException;

Sample Code

$SesClient = new Aws\SES\SESClient([ 'profile' => 'default', 'version' => '2010-12-01', 'region' => 'us-east-2' ]); $template_name = 'Template_Name'; $sender_email = 'email_address'; $recipeint_emails = ['email_address']; try { $result = $SesClient->sendTemplatedEmail([ 'Destination' => [ 'ToAddresses' => $verified_recipeint_emails, ], 'ReplyToAddresses' => [$sender_email], 'Source' => $sender_email, 'Template' => $template_name, 'TemplateData' => '{ }' ]); var_dump($result); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; }