谁能告诉我,如何发送这封邮件后,Post Confirmation仅限注册。此代码在每次确认后发送邮件,无论是忘记密码/重置密码还是注册。
var aws = require('aws-sdk');
var ses = new aws.SES();
exports.handler = (event, context, callback) => {
console.log(event);
if (event.request.userAttributes.email) {
sendEmail(event.request.userAttributes.email, "Congratulations " + event.userName + ", you have been confirmed: ", function(status) {
// Return to Amazon Cognito
callback(null, event);
});
} else {
// Nothing to do, the user's email ID is unknown
callback(null, event);
}
};
function sendEmail(to, body, completedCallback) {
var eParams = {
Destination: {
ToAddresses: [to]
},
Message: {
Body: {
Text: {
Data: body
}
},
Subject: {
Data: "Cognito Identity Provider registration completed"
}
},
// Replace source_email with your SES validated email address
Source: "<source_email>"
};
var email = ses.sendEmail(eParams, function(err, data){
if (err) {
console.log(err);
} else {
console.log("===EMAIL SENT===");
}
completedCallback('Email sent');
});
console.log("EMAIL CODE END");
};
可以查看event
对象的triggerSource
属性
Post Confirmation of registration事件触发源为PostConfirmation_ConfirmSignUp
你的句柄函数看起来像这样:
exports.handler = (event, context, callback) => {
console.log(event);
if (
event.request.userAttributes.email
&& event.triggerSource === "PostConfirmation_ConfirmSignUp") {
sendEmail(event.request.userAttributes.email, "Congratulations " + event.userName + ", you have been confirmed: ", function (status) {
// Return to Amazon Cognito
callback(null, event);
});
} else {
// Nothing to do, the user's email ID is unknown
callback(null, event);
}
};
参考:User Pool Lambda Trigger Sources