

 [適用於 JavaScript 的 AWS SDK V3 API 參考指南](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/)詳細說明 第 3 版 適用於 JavaScript 的 AWS SDK (V3) 的所有 API 操作。

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

# 在 Amazon SES 中使用電子郵件範本
<a name="ses-examples-creating-template"></a>

![\[JavaScript code example that applies to Node.js execution\]](http://docs.aws.amazon.com/zh_tw/sdk-for-javascript/v3/developer-guide/images/nodeicon.png)

**這個 Node.js 程式碼範例會說明：**
+ 如何取得所有電子郵件範本的清單。
+ 如何擷取和更新電子郵件範本。
+ 如何建立和刪除電子郵件範本。

Amazon SES 可讓您使用電子郵件範本傳送個人化電子郵件訊息。如需如何在 Amazon SES 中建立和使用電子郵件範本的詳細資訊，請參閱《Amazon Simple Email Service 開發人員指南》中的[使用 Amazon SES API 傳送個人化](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/send-personalized-email-api.html)電子郵件。

## 案例
<a name="ses-examples-creating-template-scenario"></a>

在此範例中，您會使用一系列的 Node.js 模組以使用電子郵件範本。Node.js 模組使用適用於 JavaScript 的 SDK，使用以下`SES`用戶端類別的方法建立和使用電子郵件範本：
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/ListTemplatesCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/ListTemplatesCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/CreateTemplateCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/CreateTemplateCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/GetTemplateCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/GetTemplateCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/DeleteTemplateCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/DeleteTemplateCommand/)
+ [https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/UpdateTemplateCommand/](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/Package/-aws-sdk-client-ses/Class/UpdateTemplateCommand/)

## 先決條件任務
<a name="ses-examples-creating-template-prerequisites"></a>

若要設定和執行此範例，您必須先完成這些任務：
+ 設定專案環境以執行這些 Node TypeScript 範例，並安裝必要的 適用於 JavaScript 的 AWS SDK 和第三方模組。遵循[ GitHub](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javascriptv3/example_code/ses/README.md) 上的指示。
+ 透過使用者登入資料建立共用組態檔。如需提供共用登入資料檔案的詳細資訊，請參閱 *AWS SDKs * [和工具參考指南中的共用組態和登入資料檔案](https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html)。

**重要**  
這些範例示範如何使用 ECMAScript6 (ES6) 匯入/匯出用戶端服務物件和命令。  
這需要 Node.js 13.x 版或更新版本。若要下載並安裝最新版本的 Node.js，請參閱 [Node.js 下載。](https://nodejs.org/en/download)
如果您偏好使用 CommonJS 語法，請參閱 [JavaScript ES6/CommonJS 語法](sdk-example-javascript-syntax.md)。

## 列出您的電子郵件範本
<a name="ses-examples-listing-templates"></a>

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

建立`libs`目錄，並建立檔案名稱為 的 Node.js 模組`sesClient.js`。複製下面的程式碼並將其貼入其中，這會建立 Amazon SES 用戶端物件。將 *REGION* 取代為您的 AWS 區域。

```
import { SESClient } from "@aws-sdk/client-ses";
// Set the AWS Region.
const REGION = "us-east-1";
// Credentials are automatically resolved using the AWS SDK credential provider chain.
// For more information, see https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html
// Create SES service object.
const sesClient = new SESClient({ region: REGION });
export { sesClient };
```

您可以在 [ GitHub 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/libs/sesClient.js)此範例程式碼。

以檔名 `ses_listtemplates.js` 建立一個 Node.js 模組。如先前所示設定 SDK，包括安裝所需的用戶端和套件。

建立物件以傳遞 `SES` 用戶端類別的 `ListTemplatesCommand` 方法之參數。若要呼叫 `ListTemplatesCommand`方法，請叫用 Amazon SES 用戶端服務物件，並傳遞參數。

**注意**  
此範例會匯入並使用所需的 AWS Service V3 套件用戶端、V3 命令，並以非同步/等待模式使用 `send`方法。您可以使用 V2 命令來建立此範例，方法是進行一些次要變更。如需詳細資訊，請參閱[使用 v3 命令](migrating.md#using_v3_commands)。

```
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 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_listtemplates.js)此範例程式碼。

## 取得電子郵件範本
<a name="ses-examples-get-template"></a>

在此範例中，使用 Node.js 模組取得要與 Amazon SES 搭配使用的電子郵件範本。

建立`libs`目錄，並建立檔案名稱為 的 Node.js 模組`sesClient.js`。複製並貼上以下程式碼，以建立 Amazon SES 用戶端物件。將 *REGION* 取代為您的 AWS 區域。

```
import { SESClient } from "@aws-sdk/client-ses";
// Set the AWS Region.
const REGION = "us-east-1";
// Credentials are automatically resolved using the AWS SDK credential provider chain.
// For more information, see https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html
// Create SES service object.
const sesClient = new SESClient({ region: REGION });
export { sesClient };
```

您可以在 [ GitHub 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/libs/sesClient.js)此範例程式碼。

以檔名 `ses_gettemplate.js` 建立一個 Node.js 模組。如先前所示設定 SDK，包括安裝所需的用戶端和套件。

建立物件以傳遞 `SES` 用戶端類別的 `GetTemplateCommand` 方法之 `TemplateName` 參數。若要呼叫 `GetTemplateCommand`方法，請叫用 Amazon SES 用戶端服務物件，傳遞參數。

**注意**  
此範例會匯入並使用所需的 AWS Service V3 套件用戶端、V3 命令，並以非同步/等待模式使用 `send`方法。您可以使用 V2 命令來建立此範例，方法是進行一些次要變更。如需詳細資訊，請參閱[使用 v3 命令](migrating.md#using_v3_commands)。

**注意**  
將 *TEMPLATE\$1NAME* 取代為要傳回的範本名稱。

```
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 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_gettemplate.js)此範例程式碼。

## 建立電子郵件範本
<a name="ses-examples-create-template"></a>

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

建立`libs`目錄，並建立檔案名稱為 的 Node.js 模組`sesClient.js`。複製並貼上以下程式碼，以建立 Amazon SES 用戶端物件。將 *REGION* 取代為您的 AWS 區域。

```
import { SESClient } from "@aws-sdk/client-ses";
// Set the AWS Region.
const REGION = "us-east-1";
// Credentials are automatically resolved using the AWS SDK credential provider chain.
// For more information, see https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html
// Create SES service object.
const sesClient = new SESClient({ region: REGION });
export { sesClient };
```

您可以在 [ GitHub 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/libs/sesClient.js)此範例程式碼。

以檔名 `ses_createtemplate.js` 建立一個 Node.js 模組。如先前所示設定 SDK，包括安裝所需的用戶端和套件。

建立物件以傳遞 `SES` 用戶端類別的 `CreateTemplateCommand` 方法 (包括 `TemplateName`、`HtmlPart`、`SubjectPart` 和 `TextPart`) 之參數。若要呼叫 `CreateTemplateCommand`方法，請叫用 Amazon SES 用戶端服務物件，並傳遞參數。

**注意**  
此範例會匯入並使用所需的 AWS Service V3 套件用戶端、V3 命令，並以非同步/等待模式使用 `send`方法。您可以使用 V2 命令來建立此範例，方法是進行一些次要變更。如需詳細資訊，請參閱[使用 v3 命令](migrating.md#using_v3_commands)。

**注意**  
將 *TEMPLATE\$1NAME* 取代為新範本的名稱、將 *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 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_createtemplate.js)此範例程式碼。

## 更新電子郵件範本
<a name="ses-examples-update-template"></a>

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

建立`libs`目錄，並建立檔案名稱為 的 Node.js 模組`sesClient.js`。複製並貼上以下程式碼，以建立 Amazon SES 用戶端物件。將 *REGION* 取代為您的 AWS 區域。

```
import { SESClient } from "@aws-sdk/client-ses";
// Set the AWS Region.
const REGION = "us-east-1";
// Credentials are automatically resolved using the AWS SDK credential provider chain.
// For more information, see https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html
// Create SES service object.
const sesClient = new SESClient({ region: REGION });
export { sesClient };
```

您可以在 [ GitHub 上找到此](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/libs/sesClient.js)範例程式碼。

以檔名 `ses_updatetemplate.js` 建立一個 Node.js 模組。如先前所示設定 SDK，包括安裝所需的用戶端和套件。

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

**注意**  
此範例會匯入並使用所需的 AWS Service V3 套件用戶端、V3 命令，並以非同步/等待模式使用 `send`方法。您可以使用 V2 命令來建立此範例，方法是進行一些次要變更。如需詳細資訊，請參閱[使用 v3 命令](migrating.md#using_v3_commands)。

**注意**  
將 *TEMPLATE\$1NAME* 取代為範本的名稱，並將 *HTML\$1PART* 取代為電子郵件的 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 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_updatetemplate.js)此範例程式碼。

## 刪除電子郵件範本
<a name="ses-examples-delete-template"></a>

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

建立`libs`目錄，並建立檔案名稱為 的 Node.js 模組`sesClient.js`。複製並貼上以下程式碼，以建立 Amazon SES 用戶端物件。將 *REGION* 取代為您的 AWS 區域。

```
import { SESClient } from "@aws-sdk/client-ses";
// Set the AWS Region.
const REGION = "us-east-1";
// Credentials are automatically resolved using the AWS SDK credential provider chain.
// For more information, see https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/setting-credentials-node.html
// Create SES service object.
const sesClient = new SESClient({ region: REGION });
export { sesClient };
```

您可以在 [ GitHub 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/libs/sesClient.js)此範例程式碼。

以檔名 `ses_deletetemplate.js` 建立一個 Node.js 模組。如先前所示設定 SDK，包括安裝所需的用戶端和套件。

建立物件以傳遞需要的 `TemplateName` 參數至 `SES` 用戶端類別的 `DeleteTemplateCommand` 方法。若要呼叫 `DeleteTemplateCommand`方法，請叫用 Amazon SES 服務物件，傳遞參數。

**注意**  
此範例會匯入並使用所需的 AWS Service V3 套件用戶端、V3 命令，並以非同步/等待模式使用 `send`方法。您可以使用 V2 命令來建立此範例，方法是進行一些次要變更。如需詳細資訊，請參閱[使用 v3 命令](migrating.md#using_v3_commands)。

**注意**  
將 *TEMPLATE\$1NAME* 取代為要刪除的範本名稱。

```
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 上找到](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/javascriptv3/example_code/ses/src/ses_deletetemplate.js)此範例程式碼。