在 Amazon 中使用电子邮件模板 SES - Amazon SDK for JavaScript
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

Amazon SDK for JavaScript V3 API 参考指南详细描述了 Amazon SDK for JavaScript 版本 3 (V3) 的所有API操作。

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

在 Amazon 中使用电子邮件模板 SES

JavaScript code example that applies to Node.js execution

此 Node.js 代码示例演示:

  • 如何获取所有电子邮件模板的列表。

  • 如何检索和更新电子邮件模板。

  • 如何创建和删除电子邮件模板。

Amazon SES 允许您使用电子邮件模板发送个性化电子邮件。有关如何在亚马逊中创建和使用电子邮件模板的详细信息SES,请参阅亚马逊简单电子邮件服务开发者指南SESAPI中的使用亚马逊发送个性化电子邮件。

情景

在本示例中,您使用一系列 Node.js 模块来处理电子邮件模板。Node.js 模块使用 f SDK o JavaScript r 通过SES客户端类的以下方法创建和使用电子邮件模板:

完成先决条件任务

要设置和运行此示例,您必须先完成以下任务:

  • 设置项目环境以运行这些 Node TypeScript 示例,并安装所需的模块 Amazon SDK for JavaScript 和第三方模块。按照上的说明进行操作 GitHub

重要

这些示例演示了如何使用 ECMAScript6 () ES6 导入/导出客户端服务对象和命令。

列出电子邮件模板

在此示例中,使用 Node.js 模块创建用于亚马逊的电子邮件模板SES。

创建一个 libs 目录,然后使用文件名 sesClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon SES 客户端对象。Replace(替换) REGION 与您所在 Amazon 的地区。

import { SESClient } from "@aws-sdk/client-ses"; // Set the AWS Region. const REGION = "us-east-1"; // Create SES service object. const sesClient = new SESClient({ region: REGION }); export { sesClient };

可以在此处找到此示例代码 GitHub。

创建文件名为 ses_listtemplates.js 的 Node.js 模块。SDK如前所示进行配置,包括安装所需的客户端和软件包。

创建对象,为 SES 客户端类的 ListTemplatesCommand 方法传递参数。要调用该ListTemplatesCommand方法,请调用 Amazon SES 客户端服务对象,并传递参数。

注意

此示例导入并使用所需的 S Amazon ervice V3 包客户端、V3 命令,并以异步/等待模式使用该send方法。您可以改用 V2 命令创建此示例,方法是进行一些细微的更改。有关更多信息,请参阅 使用 v3 命令

import { ListTemplatesCommand } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; const createListTemplatesCommand = (maxItems) => new ListTemplatesCommand({ MaxItems: maxItems }); const run = async () => { const listTemplatesCommand = createListTemplatesCommand(10); try { return await sesClient.send(listTemplatesCommand); } catch (err) { console.log("Failed to list templates.", err); return err; } };

要运行示例,请在命令提示符中键入以下内容。Amazon 会SES返回模板列表。

node ses_listtemplates.js

可以在此处找到此示例代码 GitHub。

获取电子邮件模板

在此示例中,使用 Node.js 模块获取用于亚马逊的电子邮件模板SES。

创建一个 libs 目录,然后使用文件名 sesClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon SES 客户端对象。Replace(替换) REGION 与您所在 Amazon 的地区。

import { SESClient } from "@aws-sdk/client-ses"; // Set the AWS Region. const REGION = "us-east-1"; // Create SES service object. const sesClient = new SESClient({ region: REGION }); export { sesClient };

可以在此处找到此示例代码 GitHub。

创建文件名为 ses_gettemplate.js 的 Node.js 模块。SDK如前所示进行配置,包括安装所需的客户端和软件包。

创建对象,为 SES 客户端类的 GetTemplateCommand 方法传递 TemplateName 参数。要调用该GetTemplateCommand方法,请调用 Amazon SES 客户端服务对象,并传递参数。

注意

此示例导入并使用所需的 S Amazon ervice V3 包客户端、V3 命令,并以异步/等待模式使用该send方法。您可以改用 V2 命令创建此示例,方法是进行一些细微的更改。有关更多信息,请参阅 使用 v3 命令

注意

Replace(替换) TEMPLATE_NAME 并附上要返回的模板的名称。

import { GetTemplateCommand } from "@aws-sdk/client-ses"; import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; import { sesClient } from "./libs/sesClient.js"; const TEMPLATE_NAME = getUniqueName("TemplateName"); const createGetTemplateCommand = (templateName) => new GetTemplateCommand({ TemplateName: templateName }); const run = async () => { const getTemplateCommand = createGetTemplateCommand(TEMPLATE_NAME); try { return await sesClient.send(getTemplateCommand); } catch (caught) { if (caught instanceof Error && caught.name === "MessageRejected") { /** @type { import('@aws-sdk/client-ses').MessageRejected} */ const messageRejectedError = caught; return messageRejectedError; } throw caught; } };

要运行示例,请在命令提示符中键入以下内容。Amazon 会SES返回模板详情。

node ses_gettemplate.js

可以在此处找到此示例代码 GitHub。

创建电子邮件模板

在此示例中,使用 Node.js 模块创建用于亚马逊的电子邮件模板SES。

创建一个 libs 目录,然后使用文件名 sesClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon SES 客户端对象。Replace(替换) REGION 与您所在 Amazon 的地区。

import { SESClient } from "@aws-sdk/client-ses"; // Set the AWS Region. const REGION = "us-east-1"; // Create SES service object. const sesClient = new SESClient({ region: REGION }); export { sesClient };

可以在此处找到此示例代码 GitHub。

创建文件名为 ses_createtemplate.js 的 Node.js 模块。SDK如前所示进行配置,包括安装所需的客户端和软件包。

创建一个对象来为 SES 客户端类的 CreateTemplateCommand 方法传递参数,其中包括 TemplateNameHtmlPartSubjectPartTextPart。要调用该CreateTemplateCommand方法,请调用 Amazon SES 客户端服务对象,并传递参数。

注意

此示例导入并使用所需的 S Amazon ervice V3 包客户端、V3 命令,并以异步/等待模式使用该send方法。您可以改用 V2 命令创建此示例,方法是进行一些细微的更改。有关更多信息,请参阅 使用 v3 命令

注意

Replace(替换) TEMPLATE_NAME 用新模板的名字,HtmlPart 带有HTML标签的电子邮件内容,以及 SubjectPart 附上电子邮件的主题。

import { CreateTemplateCommand } from "@aws-sdk/client-ses"; import { sesClient } from "./libs/sesClient.js"; import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; const TEMPLATE_NAME = getUniqueName("TestTemplateName"); const createCreateTemplateCommand = () => { return new CreateTemplateCommand({ /** * The template feature in Amazon SES is based on the Handlebars template system. */ Template: { /** * The name of an existing template in Amazon SES. */ TemplateName: TEMPLATE_NAME, HtmlPart: ` <h1>Hello, {{contact.firstName}}!</h1> <p> Did you know Amazon has a mascot named Peccy? </p> `, SubjectPart: "Amazon Tip", }, }); }; const run = async () => { const createTemplateCommand = createCreateTemplateCommand(); try { return await sesClient.send(createTemplateCommand); } catch (err) { console.log("Failed to create template.", err); return err; } };

要运行示例,请在命令提示符中键入以下内容。该模板已添加到 Amazon SES。

node ses_createtemplate.js

可以在此处找到此示例代码 GitHub。

更新电子邮件模板

在此示例中,使用 Node.js 模块创建用于亚马逊的电子邮件模板SES。

创建一个 libs 目录,然后使用文件名 sesClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon SES 客户端对象。Replace(替换) REGION 与您所在 Amazon 的地区。

import { SESClient } from "@aws-sdk/client-ses"; // Set the AWS Region. const REGION = "us-east-1"; // Create SES service object. const sesClient = new SESClient({ region: REGION }); export { sesClient };

可以在此处找到此示例代码 GitHub。

创建文件名为 ses_updatetemplate.js 的 Node.js 模块。SDK如前所示进行配置,包括安装所需的客户端和软件包。

创建一个对象来传递您在模板中要更新的 Template 参数值,并将必需的 TemplateName 参数传递到 SES 客户端类的 UpdateTemplateCommand 方法。要调用该UpdateTemplateCommand方法,请调用 Amazon SES 服务对象,传递参数。

注意

此示例导入并使用所需的 S Amazon ervice V3 包客户端、V3 命令,并以异步/等待模式使用该send方法。您可以改用 V2 命令创建此示例,方法是进行一些细微的更改。有关更多信息,请参阅 使用 v3 命令

注意

Replace(替换) TEMPLATE_NAME 使用模板的名称和 HTML_PART 带有HTML标签的电子邮件内容。

import { UpdateTemplateCommand } from "@aws-sdk/client-ses"; import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; import { sesClient } from "./libs/sesClient.js"; const TEMPLATE_NAME = getUniqueName("TemplateName"); const HTML_PART = "<h1>Hello, World!</h1>"; const createUpdateTemplateCommand = () => { return new UpdateTemplateCommand({ Template: { TemplateName: TEMPLATE_NAME, HtmlPart: HTML_PART, SubjectPart: "Example", TextPart: "Updated template text.", }, }); }; const run = async () => { const updateTemplateCommand = createUpdateTemplateCommand(); try { return await sesClient.send(updateTemplateCommand); } catch (err) { console.log("Failed to update template.", err); return err; } };

要运行示例,请在命令提示符中键入以下内容。Amazon 会SES返回模板详情。

node ses_updatetemplate.js

可以在此处找到此示例代码 GitHub。

删除电子邮件模板

在此示例中,使用 Node.js 模块创建用于亚马逊的电子邮件模板SES。

创建一个 libs 目录,然后使用文件名 sesClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon SES 客户端对象。Replace(替换) REGION 与您所在 Amazon 的地区。

import { SESClient } from "@aws-sdk/client-ses"; // Set the AWS Region. const REGION = "us-east-1"; // Create SES service object. const sesClient = new SESClient({ region: REGION }); export { sesClient };

可以在此处找到此示例代码 GitHub。

创建文件名为 ses_deletetemplate.js 的 Node.js 模块。SDK如前所示进行配置,包括安装所需的客户端和软件包。

创建对象,将必需的 TemplateName 参数传递到 SES 客户端类的 DeleteTemplateCommand 方法。要调用该DeleteTemplateCommand方法,请调用 Amazon SES 服务对象,传递参数。

注意

此示例导入并使用所需的 S Amazon ervice V3 包客户端、V3 命令,并以异步/等待模式使用该send方法。您可以改用 V2 命令创建此示例,方法是进行一些细微的更改。有关更多信息,请参阅 使用 v3 命令

注意

Replace(替换) TEMPLATE_NAME 并附上要删除的模板的名称。

import { DeleteTemplateCommand } from "@aws-sdk/client-ses"; import { getUniqueName } from "@aws-doc-sdk-examples/lib/utils/util-string.js"; import { sesClient } from "./libs/sesClient.js"; const TEMPLATE_NAME = getUniqueName("TemplateName"); const createDeleteTemplateCommand = (templateName) => new DeleteTemplateCommand({ TemplateName: templateName }); const run = async () => { const deleteTemplateCommand = createDeleteTemplateCommand(TEMPLATE_NAME); try { return await sesClient.send(deleteTemplateCommand); } catch (err) { console.log("Failed to delete template.", err); return err; } };

要运行示例,请在命令提示符中键入以下内容。Amazon 会SES返回模板详情。

node ses_deletetemplate.js

可以在此处找到此示例代码 GitHub。