在 Amazon 中使用電子郵件模板 SES - AWS SDK for JavaScript

AWS SDK for JavaScript V3 參API考指南詳細描述了 AWS SDK for JavaScript 版本 3(V3)的所有API操作。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

在 Amazon 中使用電子郵件模板 SES

JavaScript code example that applies to Node.js execution

這個 Node.js 程式碼範例會說明:

  • 如何獲取所有電子郵件模板的列表。

  • 如何檢索和更新電子郵件模板。

  • 如何建立和刪除電子郵件範本。

Amazon SES 可讓您使用電子郵件範本傳送個人化的電子郵件訊息。有關如何在 Amazon 中建立和使用電子郵件範本的詳細資訊SES,請參閱 Amazon 簡單電子郵件服務開發人員指南SESAPI中的使用 Amazon 傳送個人化電子郵件。

該方案

在此範例中,您會使用一系列的 Node.js 模組以使用電子郵件範本。Node.js 模組會使SDK用 JavaScript 來建立和使用用SES戶端類別下列方法的電子郵件範本:

必要工作

若要設定和執行此範例,您必須先完成這些任務:

  • 設置項目環境以運行這些節點 TypeScript 示例,並安裝所需 AWS SDK for JavaScript 的第三方模塊。按照上的說明進行操作 GitHub

  • 透過使用者登入資料建立共用組態檔。如需有關提供共用認證檔案的詳細資訊,請參閱《工具參考指南》中的共用設定AWS SDKs和認證檔案。

重要

這些範例示範如何使用 ECMAScript6 (ES6) 匯入/匯出用戶端服務物件和指令。

列出您的電子郵件範

在此範例中,請使用 Node.js 模組建立要搭配 Amazon 使用的電子郵件範本SES。

創建一個libs目錄,並使用文件名創建一個 Node.js 模塊sesClient.js。將下面的代碼複製並粘貼到其中,以創建 Amazon SES 客戶端對象。Replace (取代) REGION 與您的 AWS 區域。

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 客戶端服務對象,並傳遞參數。

注意

此範例會匯入並使用所需的 AWS Service 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 模塊獲取與 Amazon 一起使用的電子郵件模板SES。

創建一個libs目錄,並使用文件名創建一個 Node.js 模塊sesClient.js。將下面的代碼複製並粘貼到其中,以創建 Amazon SES 客戶端對象。Replace (取代) REGION 與您的 AWS 區域。

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 客戶端服務對象,並傳遞參數。

注意

此範例會匯入並使用所需的 AWS Service 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 模組建立要搭配 Amazon 使用的電子郵件範本SES。

創建一個libs目錄,並使用文件名創建一個 Node.js 模塊sesClient.js。將下面的代碼複製並粘貼到其中,以創建 Amazon SES 客戶端對象。Replace (取代) REGION 與您的 AWS 區域。

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 客戶端服務對象,並傳遞參數。

注意

此範例會匯入並使用所需的 AWS Service 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 模組建立要搭配 Amazon 使用的電子郵件範本SES。

創建一個libs目錄,並使用文件名創建一個 Node.js 模塊sesClient.js。將下面的代碼複製並粘貼到其中,以創建 Amazon SES 客戶端對象。Replace (取代) REGION 與您的 AWS 區域。

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如前所示設定,包括安裝所需的用戶端和套件。

建立一個物件,並搭配需要的 TemplateName 參數 (傳遞至 SES 用戶端類別的 UpdateTemplateCommand 方法之參數),以傳遞您要在範本中更新的 Template 參數值。要調用該UpdateTemplateCommand方法,請調用 Amazon SES 服務對象,並傳遞參數。

注意

此範例會匯入並使用所需的 AWS Service 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 模組建立要搭配 Amazon 使用的電子郵件範本SES。

創建一個libs目錄,並使用文件名創建一個 Node.js 模塊sesClient.js。將下面的代碼複製並粘貼到其中,以創建 Amazon SES 客戶端對象。Replace (取代) REGION 與您的 AWS 區域。

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 服務對象,並傳遞參數。

注意

此範例會匯入並使用所需的 AWS Service 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。