AWS SES发送的Email中包含一些特殊的Header信息,可以在邮件客户端或服务提供者那里显示一些额外的信息。其中一个Header是“X-SES-CONFIGURATION-SET”,可以配置规则集名称。在规则集中设置一个规则,将所有包含“X-SES-MESSAGE-TAGS” Header的邮件标记为“非垃圾邮件”(Not Spam)。下面给出Node.js代码示例:
const aws = require('aws-sdk')
aws.config.update({region: 'us-east-1'})
exports.handler = async (event) => {
console.log(event)
var sesNotification = event.Records[0].Sns.Message
var mailNotification = JSON.parse(sesNotification)
var messageId = mailNotification.mail.messageId
var configurationSet = 'MyConfigurationSet' // 规则集名称
var ses = new aws.SES()
var params = {
ConfigurationSetName: configurationSet,
RawMessage: { Data: event.Records[0].Sns.Message }
}
try {
await ses.createConfigurationSetTrackingOptions(params).promise()
console.log(`Set configuration set tracking options for ${configurationSet}`)
var tags = mailNotification.mail.tags // 过去的标记信息
tags.push({ Name: 'X-SES-MESSAGE-TAGS', Value: 'MyCustomTag=Yes' }) // 添加标记
params = {
ConfigurationSetName: configurationSet,
MessageId: messageId,
Tags: tags
}
await ses.createConfigurationSetEventDestination(params).promise()
console.log(`Set tracking event destination for message ID: ${messageId}`)
} catch (err) {
console.log(`Error setting tracking options: ${err}`)
}
const response = {
statusCode: 200,
body: JSON.stringify('Hello from Lambda!'),
};
return response;
};