python-boto3/docs/source/guide/ses-template.rst

182 lines
5.7 KiB
ReStructuredText
Raw Normal View History

2019-10-20 18:51:09 +02:00
.. Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2019-01-28 22:47:44 +01:00
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0
International License (the "License"). You may not use this file except in compliance with the
License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/.
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions and
limitations under the License.
.. _aws-boto3-ses-template:
###############################################
2021-09-22 18:34:33 +02:00
Creating custom email templates with Amazon SES
2019-01-28 22:47:44 +01:00
###############################################
.. meta::
:description: Use the Amazon SES API to create and use email templates.
2019-10-20 18:51:09 +02:00
:keywords: SES Python
2019-01-28 22:47:44 +01:00
2019-10-20 18:51:09 +02:00
Amazon Simple Email Service (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.
2019-01-28 22:47:44 +01:00
2019-10-20 18:51:09 +02:00
For more information, see `Sending Personalized Email Using the Amazon SES
API <https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-personalized-email-api.html>`__.
2019-01-28 22:47:44 +01:00
The following examples show how to:
* Create an email template using `create_template() <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses.html#SES.Client.create_template>`_.
* List all email templates using `list_templates() <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses.html#SES.Client.list_templates>`_.
* Retrieve an email template using `get_template() <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses.html#SES.Client.get_template>`_.
* Update an email template using `update_template() <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses.html#SES.Client.update_template>`_.
* Remove an email template using `delete_template() <https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/ses.html#SES.Client.delete_template>`_.
* 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>`_.
2021-09-22 18:34:33 +02:00
Prerequisite tasks
2019-01-28 22:47:44 +01:00
==================
To set up and run this example, you must first complete these tasks:
* Configure your AWS credentials, as described in :doc:`quickstart`.
2021-09-22 18:34:33 +02:00
Create an email template
2019-01-28 22:47:44 +01:00
========================
2019-10-20 18:51:09 +02:00
To create a template to send personalized email messages, use the
`CreateTemplate <https://docs.aws.amazon.com/ses/latest/APIReference/API_CreateTemplate.html>`_
operation. The template can be used by any account authorized to send
messages in the AWS Region to which the template is added.
2019-01-28 22:47:44 +01:00
.. note::
2019-10-20 18:51:09 +02:00
SES doesn't validate your HTML, so be sure that ``HtmlPart`` is
valid before sending an email.
2019-01-28 22:47:44 +01:00
Example
-------
.. code-block:: python
2019-10-20 18:51:09 +02:00
import boto3
2019-01-28 22:47:44 +01:00
# Create SES client
ses = boto3.client('ses')
response = ses.create_template(
Template = {
'TemplateName' : 'TEMPLATE_NAME',
'SubjectPart' : 'SUBJECT_LINE',
'TextPart' : 'TEXT_CONTENT',
'HtmlPart' : 'HTML_CONTENT'
}
)
print(response)
2021-09-22 18:34:33 +02:00
Get an email template
2019-01-28 22:47:44 +01:00
=====================
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.
Example
-------
.. code-block:: python
import boto3
# Create SES client
ses = boto3.client('ses')
response = ses.get_template(
TemplateName = 'TEMPLATE_NAME'
)
print(response)
2021-09-22 18:34:33 +02:00
List all email templates
2019-01-28 22:47:44 +01:00
========================
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.
Example
-------
.. code-block:: python
import boto3
# Create SES client
ses = boto3.client('ses')
response = ses.list_templates(
MaxItems=10
)
print(response)
2021-09-22 18:34:33 +02:00
Update an email template
2019-01-28 22:47:44 +01:00
========================
2019-10-20 18:51:09 +02:00
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.
2019-01-28 22:47:44 +01:00
Example
-------
.. code-block:: python
import boto3
# Create SES client
ses = boto3.client('ses')
response = ses.update_template(
Template={
'TemplateName': 'TEMPLATE_NAME',
'SubjectPart' : 'SUBJECT_LINE',
'TextPart' : 'TEXT_CONTENT',
'HtmlPart' : 'HTML_CONTENT'
}
)
print(response)
2021-09-22 18:34:33 +02:00
Send an email with a template
2019-01-28 22:47:44 +01:00
=============================
2019-10-20 18:51:09 +02:00
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.
2019-01-28 22:47:44 +01:00
Example
-------
.. code-block:: python
import boto3
# Create SES client
ses = boto3.client('ses')
response = ses.send_templated_email(
Source='EMAIL_ADDRESS',
Destination={
'ToAddresses': [
'EMAIL_ADDRESS',
],
'CcAddresses': [
'EMAIL_ADDRESS',
]
},
ReplyToAddresses=[
'EMAIL_ADDRESS',
],
Template='TEMPLATE_NAME',
TemplateData='{ \"REPLACEMENT_TAG_NAME\":\"REPLACEMENT_VALUE\" }'
)
print(response)