我們宣布
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
在 Amazon SES 中使用接收規則
這個 Node.js 程式碼範例會說明:
建立並刪除接收規則。
將接收規則編入接收規則組。
Amazon SES 中的接收規則會指定如何處理針對您擁有的電子郵件地址或網域收到的電子郵件。接收規則包括條件與動作排序清單。如果內送電子郵件的收件者符合接收規則條件中指定的收件者,Amazon SES 會執行接收規則指定的動作。
若要使用 Amazon SES 做為電子郵件接收者,您必須至少有一個作用中的接收規則集。接收規則集是一組已排序的接收規則集合,可指定 Amazon SES 應如何處理透過驗證網域接收的郵件。如需詳細資訊,請參閱 Amazon SES 電子郵件接收建立接收規則和 Amazon SES 電子郵件接收建立 Amazon SES 電子郵件接收的接收規則集 (英文)。
使用案例
在此範例中會使用一系列的 Node.js 模組,以多種不同方式傳送電子郵件。Node.js 模組使用 SDK JavaScript 來建立和使用用AWS.SES
戶端類別的下列方法的電子郵件範本:
先決條件任務
若要設定和執行此範例,您必須先完成這些任務:
安裝 Node.js。如需安裝 Node.js 的詳細資訊,請參閱 Node.js 網站
。 透過使用者登入資料建立共用組態檔。如需有關提供登入資料 JSON 檔案的詳細資訊,請參閱 從共用登入資料檔案中在 Node.js 中載入登入資料。
建立 Amazon S3 收據規則
Amazon SES 的每個接收規則都包含一份已排序的動作清單。此範例使用 Amazon S3 動作建立接收規則,該動作可將郵件訊息傳送到 Amazon S3 儲存貯體。如需接收規則動作的詳細資訊,請參閱 Amazon 簡易電子郵件服務開發人員指南中的動作選項。
若要讓 Amazon SES 將電子郵件寫入 Amazon S3 儲存貯體,請建立PutObject
可授予 Amazon SES 許可的儲存貯體政策。如需有關建立此儲存貯體政策的資訊,請參閱 Amazon 簡單電子郵件服務開發人員指南中的授予 Amazon SES 寫入 Amazon S3 儲存貯體的權限。
在此範例中,使用 Node.js 模組在 Amazon SES 中建立接收規則,以將接收的訊息儲存在 Amazon S3 儲存貯體中。以檔名 ses_createreceiptrule.js
建立一個 Node.js 模組。依前述內容設定軟體開發套件。
建立參數物件以傳遞建立接收規則集所需的值。若要呼叫 createReceiptRuleSet
方法,請建立叫用 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 createReceiptRule params
var params = {
Rule: {
Actions: [
{
S3Action: {
BucketName: "S3_BUCKET_NAME",
ObjectKeyPrefix: "email",
},
},
],
Recipients: [
"DOMAIN | EMAIL_ADDRESS",
/* more items */
],
Enabled: true | false,
Name: "RULE_NAME",
ScanEnabled: true | false,
TlsPolicy: "Optional",
},
RuleSetName: "RULE_SET_NAME",
};
// Create the promise and SES service object
var newRulePromise = new AWS.SES({ apiVersion: "2010-12-01" })
.createReceiptRule(params)
.promise();
// Handle promise's fulfilled/rejected states
newRulePromise
.then(function (data) {
console.log("Rule created");
})
.catch(function (err) {
console.error(err, err.stack);
});
若要執行範例,請在命令列中輸入以下內容。Amazon SES 創建接收規則。
node ses_createreceiptrule.js
您可以在這裡
刪除接收規則
在此範例中,使用 Node.js 模組以搭配 Amazon SES 傳送電子郵件。以檔名 ses_deletereceiptrule.js
建立一個 Node.js 模組。依前述內容設定軟體開發套件。
建立參數物件以傳遞接收規則所要刪除的名稱。若要呼叫 deleteReceiptRule
方法,請建立叫用 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 deleteReceiptRule params
var params = {
RuleName: "RULE_NAME" /* required */,
RuleSetName: "RULE_SET_NAME" /* required */,
};
// Create the promise and SES service object
var newRulePromise = new AWS.SES({ apiVersion: "2010-12-01" })
.deleteReceiptRule(params)
.promise();
// Handle promise's fulfilled/rejected states
newRulePromise
.then(function (data) {
console.log("Receipt Rule Deleted");
})
.catch(function (err) {
console.error(err, err.stack);
});
若要執行範例,請在命令列中輸入以下內容。Amazon SES 會建立接收規則集清單。
node ses_deletereceiptrule.js
您可以在這裡
建立接收規則集
在此範例中,使用 Node.js 模組以搭配 Amazon SES 傳送電子郵件。以檔名 ses_createreceiptruleset.js
建立一個 Node.js 模組。依前述內容設定軟體開發套件。
建立參數物件以為新接收規則集傳遞名稱。若要呼叫 createReceiptRuleSet
方法,請建立叫用 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 newRulePromise = new AWS.SES({ apiVersion: "2010-12-01" })
.createReceiptRuleSet({ RuleSetName: "NAME" })
.promise();
// Handle promise's fulfilled/rejected states
newRulePromise
.then(function (data) {
console.log(data);
})
.catch(function (err) {
console.error(err, err.stack);
});
若要執行範例,請在命令列中輸入以下內容。Amazon SES 會建立接收規則集清單。
node ses_createreceiptruleset.js
您可以在這裡
刪除接收規則集
在此範例中,使用 Node.js 模組以搭配 Amazon SES 傳送電子郵件。以檔名 ses_deletereceiptruleset.js
建立一個 Node.js 模組。依前述內容設定軟體開發套件。
建立物件以傳遞接收規則集所要刪除的名稱。若要呼叫 deleeReceiptRuleSet
方法,請建立叫用 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 newRulePromise = new AWS.SES({ apiVersion: "2010-12-01" })
.deleteReceiptRuleSet({ RuleSetName: "NAME" })
.promise();
// Handle promise's fulfilled/rejected states
newRulePromise
.then(function (data) {
console.log(data);
})
.catch(function (err) {
console.error(err, err.stack);
});
若要執行範例,請在命令列中輸入以下內容。Amazon SES 會建立接收規則集清單。
node ses_deletereceiptruleset.js
您可以在這裡