| 1 |
.. Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
|
| 3 |
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 |
| 4 |
International License (the "License"). You may not use this file except in compliance with the |
| 5 |
License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/. |
| 6 |
|
| 7 |
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 8 |
either express or implied. See the License for the specific language governing permissions and |
| 9 |
limitations under the License. |
| 10 |
|
| 11 |
.. _aws-boto3-ses-template: |
| 12 |
|
| 13 |
############################################### |
| 14 |
Creating custom email templates with Amazon SES |
| 15 |
############################################### |
| 16 |
|
| 17 |
.. meta:: |
| 18 |
:description: Use the Amazon SES API to create and use email templates. |
| 19 |
:keywords: SES Python |
| 20 |
|
| 21 |
Amazon Simple Email Service (SES) enables you to send emails that are |
| 22 |
personalized for each recipient by using templates. Templates include |
| 23 |
a subject line and the text and HTML parts of the email body. The subject |
| 24 |
and body sections can also contain unique values that are personalized for |
| 25 |
each recipient. |
| 26 |
|
| 27 |
For more information, see `Sending Personalized Email Using the Amazon SES |
| 28 |
API <https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-personalized-email-api.html>`__. |
| 29 |
|
| 30 |
The following examples show how to: |
| 31 |
|
| 32 |
* Create an email template using `create_template() <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses.html#SES.Client.create_template>`_. |
| 33 |
* List all email templates using `list_templates() <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses.html#SES.Client.list_templates>`_. |
| 34 |
* Retrieve an email template using `get_template() <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses.html#SES.Client.get_template>`_. |
| 35 |
* Update an email template using `update_template() <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses.html#SES.Client.update_template>`_. |
| 36 |
* Remove an email template using `delete_template() <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses.html#SES.Client.delete_template>`_. |
| 37 |
* Send a templated email using `send_templated_email() <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses.html#SES.Client.send_templated_email>`_. |
| 38 |
|
| 39 |
Prerequisite tasks |
| 40 |
================== |
| 41 |
|
| 42 |
To set up and run this example, you must first complete these tasks: |
| 43 |
|
| 44 |
* Configure your AWS credentials, as described in :doc:`quickstart`. |
| 45 |
|
| 46 |
Create an email template |
| 47 |
======================== |
| 48 |
|
| 49 |
To create a template to send personalized email messages, use the |
| 50 |
`CreateTemplate <https://docs.aws.amazon.com/ses/latest/APIReference/API_CreateTemplate.html>`_ |
| 51 |
operation. The template can be used by any account authorized to send |
| 52 |
messages in the AWS Region to which the template is added. |
| 53 |
|
| 54 |
.. note:: |
| 55 |
SES doesn't validate your HTML, so be sure that ``HtmlPart`` is |
| 56 |
valid before sending an email. |
| 57 |
|
| 58 |
Example |
| 59 |
------- |
| 60 |
|
| 61 |
.. code-block:: python |
| 62 |
|
| 63 |
import boto3 |
| 64 |
|
| 65 |
# Create SES client |
| 66 |
ses = boto3.client('ses') |
| 67 |
|
| 68 |
response = ses.create_template( |
| 69 |
Template = { |
| 70 |
'TemplateName' : 'TEMPLATE_NAME', |
| 71 |
'SubjectPart' : 'SUBJECT_LINE', |
| 72 |
'TextPart' : 'TEXT_CONTENT', |
| 73 |
'HtmlPart' : 'HTML_CONTENT' |
| 74 |
} |
| 75 |
) |
| 76 |
|
| 77 |
|
| 78 |
print(response) |
| 79 |
|
| 80 |
Get an email template |
| 81 |
===================== |
| 82 |
|
| 83 |
To view the content for an existing email template including the subject line, HTML body, and plain text, use the `GetTemplate <https://docs.aws.amazon.com/ses/latest/APIReference/API_GetTemplate.html>`_ operation. Only TemplateName is required. |
| 84 |
|
| 85 |
Example |
| 86 |
------- |
| 87 |
|
| 88 |
.. code-block:: python |
| 89 |
|
| 90 |
import boto3 |
| 91 |
|
| 92 |
# Create SES client |
| 93 |
ses = boto3.client('ses') |
| 94 |
|
| 95 |
response = ses.get_template( |
| 96 |
TemplateName = 'TEMPLATE_NAME' |
| 97 |
) |
| 98 |
|
| 99 |
print(response) |
| 100 |
|
| 101 |
List all email templates |
| 102 |
======================== |
| 103 |
|
| 104 |
To retrieve a list of all email templates that are associated with your AWS account in the current AWS Region, use the `ListTemplates <https://docs.aws.amazon.com/ses/latest/APIReference/API_ListTemplates.html>`_ operation. |
| 105 |
|
| 106 |
Example |
| 107 |
------- |
| 108 |
|
| 109 |
.. code-block:: python |
| 110 |
|
| 111 |
import boto3 |
| 112 |
|
| 113 |
# Create SES client |
| 114 |
ses = boto3.client('ses') |
| 115 |
|
| 116 |
response = ses.list_templates( |
| 117 |
MaxItems=10 |
| 118 |
) |
| 119 |
|
| 120 |
print(response) |
| 121 |
|
| 122 |
|
| 123 |
Update an email template |
| 124 |
======================== |
| 125 |
|
| 126 |
To change the content for a specific email template including the subject line, HTML body, and plain text, use the `UpdateTemplate <https://docs.aws.amazon.com/ses/latest/APIReference/API_UpdateTemplate.html>`_ operation. |
| 127 |
|
| 128 |
Example |
| 129 |
------- |
| 130 |
|
| 131 |
.. code-block:: python |
| 132 |
|
| 133 |
import boto3 |
| 134 |
|
| 135 |
# Create SES client |
| 136 |
ses = boto3.client('ses') |
| 137 |
|
| 138 |
response = ses.update_template( |
| 139 |
Template={ |
| 140 |
'TemplateName': 'TEMPLATE_NAME', |
| 141 |
'SubjectPart' : 'SUBJECT_LINE', |
| 142 |
'TextPart' : 'TEXT_CONTENT', |
| 143 |
'HtmlPart' : 'HTML_CONTENT' |
| 144 |
} |
| 145 |
) |
| 146 |
|
| 147 |
print(response) |
| 148 |
|
| 149 |
Send an email with a template |
| 150 |
============================= |
| 151 |
|
| 152 |
To use a template to send an email to recipients, use the `SendTemplatedEmail <https://docs.aws.amazon.com/ses/latest/APIReference/API_SendTemplatedEmail.html>`__ operation. |
| 153 |
|
| 154 |
|
| 155 |
Example |
| 156 |
------- |
| 157 |
|
| 158 |
.. code-block:: python |
| 159 |
|
| 160 |
import boto3 |
| 161 |
|
| 162 |
# Create SES client |
| 163 |
ses = boto3.client('ses') |
| 164 |
|
| 165 |
response = ses.send_templated_email( |
| 166 |
Source='EMAIL_ADDRESS', |
| 167 |
Destination={ |
| 168 |
'ToAddresses': [ |
| 169 |
'EMAIL_ADDRESS', |
| 170 |
], |
| 171 |
'CcAddresses': [ |
| 172 |
'EMAIL_ADDRESS', |
| 173 |
] |
| 174 |
}, |
| 175 |
ReplyToAddresses=[ |
| 176 |
'EMAIL_ADDRESS', |
| 177 |
], |
| 178 |
Template='TEMPLATE_NAME', |
| 179 |
TemplateData='{ \"REPLACEMENT_TAG_NAME\":\"REPLACEMENT_VALUE\" }' |
| 180 |
) |
| 181 |
|
| 182 |
print(response) |