我們宣布
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在 Amazon SES 中使用電子郵件模板
這個 Node.js 程式碼範例會說明:
取得您所有的電子郵件範本清單。
擷取並更新電子郵件範本。
建立並刪除電子郵件範本。
Amazon SES 可讓您使用電子郵件範本傳送個人化的電子郵件。有關如何在 Amazon 簡單電子郵件服務中建立和使用電子郵件範本的詳細資訊,請參閱 Amazon 簡單電子郵件服務開發人員指南中的使用 Amazon SES API 傳送個人化電子郵件。
使用案例
在此範例中,您會使用一系列的 Node.js 模組以使用電子郵件範本。Node.js 模組使用 SDK JavaScript 來建立和使用用AWS.SES
戶端類別的下列方法的電子郵件範本:
先決條件任務
若要設定和執行此範例,您必須先完成這些任務:
安裝 Node.js。如需安裝 Node.js 的詳細資訊,請參閱 Node.js 網站
。 透過使用者登入資料建立共用組態檔。如需建立登入資料檔案的詳細資訊,請參閱 從共用登入資料檔案中在 Node.js 中載入登入資料。
列出您的電子郵件範本
在此範例中,使用 Node.js 模組建立電子郵件範本以搭配 Amazon SES 使用。以檔名 ses_listtemplates.js
建立一個 Node.js 模組。依前述內容設定軟體開發套件。
建立物件以傳遞 AWS.SES
用戶端類別的 listTemplates
方法之參數。若要呼叫 listTemplates
方法,請建立叫用 Amazon SES 服務物件的 promise 來傳遞參數。然後,在 promise 回呼中處理 response
。
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });
// Create the promise and SES service object
var templatePromise = new AWS.SES({ apiVersion: "2010-12-01" })
.listTemplates({ MaxItems: ITEMS_COUNT })
.promise();
// Handle promise's fulfilled/rejected states
templatePromise
.then(function (data) {
console.log(data);
})
.catch(function (err) {
console.error(err, err.stack);
});
若要執行範例,請在命令列中輸入以下內容。Amazon SES 返回模板列表。
node ses_listtemplates.js
您可以在這裡
取得電子郵件範本
在此範例中,使用 Node.js 模組取得要搭配 Amazon SES 使用的電子郵件範本。以檔名 ses_gettemplate.js
建立一個 Node.js 模組。依前述內容設定軟體開發套件。
建立物件以傳遞 AWS.SES
用戶端類別的 getTemplate
方法之 TemplateName
參數。若要呼叫 getTemplate
方法,請建立叫用 Amazon SES 服務物件的 promise 來傳遞參數。然後,在 promise 回呼中處理 response
。
// Load the AWS SDK for Node.js.
var AWS = require("aws-sdk");
// Set the AWS Region.
AWS.config.update({ region: "REGION" });
// Create the promise and Amazon Simple Email Service (Amazon SES) service object.
var templatePromise = new AWS.SES({ apiVersion: "2010-12-01" })
.getTemplate({ TemplateName: "TEMPLATE_NAME" })
.promise();
// Handle promise's fulfilled/rejected states
templatePromise
.then(function (data) {
console.log(data.Template.SubjectPart);
})
.catch(function (err) {
console.error(err, err.stack);
});
若要執行範例,請在命令列中輸入以下內容。Amazon SES 返回模板詳細信息。
node ses_gettemplate.js
您可以在這裡
建立電子郵件範本
在此範例中,使用 Node.js 模組建立電子郵件範本以搭配 Amazon SES 使用。以檔名 ses_createtemplate.js
建立一個 Node.js 模組。依前述內容設定軟體開發套件。
建立物件以傳遞 AWS.SES
用戶端類別的 createTemplate
方法 (包括 TemplateName
、HtmlPart
、SubjectPart
和 TextPart
) 之參數。若要呼叫 createTemplate
方法,請建立叫用 Amazon SES 服務物件的 promise 來傳遞參數。然後,在 promise 回呼中處理 response
。
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });
// Create createTemplate params
var params = {
Template: {
TemplateName: "TEMPLATE_NAME" /* required */,
HtmlPart: "HTML_CONTENT",
SubjectPart: "SUBJECT_LINE",
TextPart: "TEXT_CONTENT",
},
};
// Create the promise and SES service object
var templatePromise = new AWS.SES({ apiVersion: "2010-12-01" })
.createTemplate(params)
.promise();
// Handle promise's fulfilled/rejected states
templatePromise
.then(function (data) {
console.log(data);
})
.catch(function (err) {
console.error(err, err.stack);
});
若要執行範例,請在命令列中輸入以下內容。該模板被添加到 Amazon SES。
node ses_createtemplate.js
您可以在這裡
更新電子郵件範本
在此範例中,使用 Node.js 模組建立電子郵件範本以搭配 Amazon SES 使用。以檔名 ses_updatetemplate.js
建立一個 Node.js 模組。依前述內容設定軟體開發套件。
建立一個物件,並搭配需要的 TemplateName
參數 (傳遞至 AWS.SES
用戶端類別的 updateTemplate
方法之參數),以傳遞您要在範本中更新的 Template
參數值。若要呼叫 updateTemplate
方法,請建立叫用 Amazon SES 服務物件的 promise 來傳遞參數。然後,在 promise 回呼中處理 response
。
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });
// Create updateTemplate parameters
var params = {
Template: {
TemplateName: "TEMPLATE_NAME" /* required */,
HtmlPart: "HTML_CONTENT",
SubjectPart: "SUBJECT_LINE",
TextPart: "TEXT_CONTENT",
},
};
// Create the promise and SES service object
var templatePromise = new AWS.SES({ apiVersion: "2010-12-01" })
.updateTemplate(params)
.promise();
// Handle promise's fulfilled/rejected states
templatePromise
.then(function (data) {
console.log("Template Updated");
})
.catch(function (err) {
console.error(err, err.stack);
});
若要執行範例,請在命令列中輸入以下內容。Amazon SES 返回模板詳細信息。
node ses_updatetemplate.js
您可以在這裡
刪除電子郵件範本
在此範例中,使用 Node.js 模組建立電子郵件範本以搭配 Amazon SES 使用。以檔名 ses_deletetemplate.js
建立一個 Node.js 模組。依前述內容設定軟體開發套件。
建立物件以傳遞需要的 TemplateName
參數至 AWS.SES
用戶端類別的 deleteTemplate
方法。若要呼叫 deleteTemplate
方法,請建立叫用 Amazon SES 服務物件的 promise 來傳遞參數。然後,在 promise 回呼中處理 response
。
// Load the AWS SDK for Node.js
var AWS = require("aws-sdk");
// Set the region
AWS.config.update({ region: "REGION" });
// Create the promise and SES service object
var templatePromise = new AWS.SES({ apiVersion: "2010-12-01" })
.deleteTemplate({ TemplateName: "TEMPLATE_NAME" })
.promise();
// Handle promise's fulfilled/rejected states
templatePromise
.then(function (data) {
console.log("Template Deleted");
})
.catch(function (err) {
console.error(err, err.stack);
});
若要執行範例,請在命令列中輸入以下內容。Amazon SES 返回模板詳細信息。
node ses_deletetemplate.js
您可以在這裡