Creating custom email templates using the Amazon SES API and the AWS SDK for PHP Version 3 - AWS SDK for PHP

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 Simple Email Service Developer Guide.

The following examples show how to:

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

Credentials

Before running the example code, configure your AWS credentials, as described in Credentials. Then import the AWS SDK for PHP, as described in Basic usage.

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\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\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\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\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\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\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'; $recipient_emails = ['email_address']; try { $result = $SesClient->sendTemplatedEmail([ 'Destination' => [ 'ToAddresses' => $recipient_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"; }