

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 管理电子邮件模板
<a name="send-personalized-email-manage-templates"></a>

除了[创建电子邮件模板](send-personalized-email-api.md)之外，您还可以使用 Amazon SES v2 API 来更新或删除现有模板、列出所有现有模板或者查看模板的内容。

本节包含使用执行 AWS CLI 与 SES 模板相关的任务的过程。

**注意**  
本节中的过程假定您已安装和配置 AWS CLI。有关安装和配置的更多信息 AWS CLI，请参阅《[AWS Command Line Interface 用户指南》](https://docs.aws.amazon.com/cli/latest/userguide/)。

## 查看电子邮件模板列表
<a name="send-personalized-email-manage-templates-list"></a>

您可以使用 [`ListEmailTemplate`](https://docs.aws.amazon.com/ses/latest/APIReference-V2/API_ListEmailTemplate.html) SES v2 API 操作来查看您的所有现有电子邮件模板的列表。

**查看电子邮件模板列表**
+ 在命令行输入以下命令：

  ```
  aws sesv2 list-email-templates
  ```

  如果您的 SES 账户在当前区域中存在现有电子邮件模板，那么此命令将返回类似于以下示例的响应：

  ```
  {
      "TemplatesMetadata": [
          {
              "Name": "SpecialOffers",
              "CreatedTimestamp": "2020-08-05T16:04:12.640Z"
          },
          {
              "Name": "NewsAndUpdates",
              "CreatedTimestamp": "2019-10-03T20:03:34.574Z"
          }
      ]
  }
  ```

  如果您尚未创建任何模板，那么该命令会返回没有任何成员的 `TemplatesMetadata` 对象。

## 查看特定电子邮件模板的内容
<a name="send-personalized-email-manage-templates-get"></a>

您可以使用 [`GetEmailTemplate`](https://docs.aws.amazon.com/ses/latest/APIReference-V2/API_GetEmailTemplate.html) SES v2 API 操作来查看特定电子邮件模板的内容。

**查看电子邮件模板的内容**
+ 在命令行输入以下命令：

  ```
  aws sesv2 get-email-template --template-name {{MyTemplate}}
  ```

  在前面的命令中，{{MyTemplate}}替换为要查看的模板的名称。

  如果您提供的模板名称与 SES 账户中存在的模板匹配，那么此命令将返回类似于以下示例的响应：

  ```
  {
      "Template": {
          "TemplateName": "TestMessage",
          "SubjectPart": "Amazon SES Test Message",
          "TextPart": "Hello! This is the text part of the message.",
          "HtmlPart": "<html>\n<body>\n<h2>Hello!</h2>\n<p>This is the HTML part of the message.</p></body>\n</html>"
      }
  }
  ```

  如果您提供的模板名称与 SES 账户中存在的模板不匹配，那么该命令将返回 `NotFoundException` 错误。

## 删除电子邮件模板
<a name="send-personalized-email-manage-templates-delete"></a>

您可以使用 [`DeleteEmailTemplate`](https://docs.aws.amazon.com/ses/latest/APIReference-V2/API_DeleteEmailTemplate.html) SES v2 API 操作来删除特定电子邮件模板。

**删除电子邮件模板**
+ 在命令行输入以下命令：

  ```
  aws sesv2 delete-email-template --template-name {{MyTemplate}}
  ```

  在前面的命令中，{{MyTemplate}}替换为要删除的模板的名称。

  此命令不提供任何输出。您可以使用该[GetTemplate](#send-personalized-email-manage-templates-get)操作验证模板是否已删除。

## 更新电子邮件模板
<a name="send-personalized-email-manage-templates-update"></a>

您可以使用 [`UpdateEmailTemplate`](https://docs.aws.amazon.com/ses/latest/APIReference-V2/API_UpdateEmailTemplate.html) SES v2 API 操作来更新现有电子邮件模板。例如，如果您要更改电子邮件模板的主题行，或者如果您需要修改邮件本身的正文，那么此操作很有用。

**更新电子邮件模板**

1. 使用 `GetEmailTemplate` 命令，在命令行中输入以下命令来检索现有模板：

   ```
   aws sesv2 get-email-template --template-name {{MyTemplate}}
   ```

   在前面的命令中，{{MyTemplate}}替换为要更新的模板的名称。

   如果您提供的模板名称与 SES 账户中存在的模板匹配，那么此命令将返回类似于以下示例的响应：

   ```
   {
       "Template": {
           "TemplateName": "TestMessage",
           "SubjectPart": "Amazon SES Test Message",
           "TextPart": "Hello! This is the text part of the message.",
           "HtmlPart": "<html>\n<body>\n<h2>Hello!</h2>\n<p>This is the HTML part of the message.</p></body>\n</html>"
       }
   }
   ```

1. 在文本编辑器中，创建一个新文件。将上一个命令的输出粘贴到文件中。

1. 根据需要修改模板。您省略的任何行都将从模板中删除。例如，如果您只想更改模板的 `SubjectPart`，您仍然需要包含 `TextPart` 和 `HtmlPart` 属性。

   完成后，将文件另存为 `update_template.json`。

1. 在命令行输入以下命令：

   ```
   aws sesv2 update-email-template --cli-input-json file://{{path/to/update_template.json}}
   ```

   在前面的命令中，{{path/to/update\_template.json}}替换为您在上一步中创建的`update_template.json`文件的路径。

   如果模板更新成功，则此命令不提供任何输出。您可以使用 [`GetEmailTemplate`](https://docs.aws.amazon.com/ses/latest/APIReference-V2/API_GetEmailTemplate.html) 操作来验证模板是否已更新。

   如果您指定的模板不存在，则此命令会返回 `TemplateDoesNotExist` 错误。如果模板不包含 `TextPart` 和/或 `HtmlPart` 属性，则此命令会返回 `InvalidParameterValue` 错误。