在检查了一些教程后,我试图实现nodeJS函数。编译完代码后,我没有发现任何错误,并且正在通过电子邮件发送消息。但在检查我的收件箱后,我没有找到任何电子邮件。 请告诉我我错误的地方。
console.log('Version 0.1.3');
var aws = require('aws-sdk');
var ses = new aws.SES();
var s3 = new aws.S3();
exports.handler = function (event, context) {
console.log("Event: " + JSON.stringify(event));
// Check required parameters
if (event.email == null) {
context.fail('Bad Request: Missing required member: email');
return;
}
var config = require('./config.js');
// Make sure some expected results are present
if (event.name == null) {
event.name = event.email;
}
// Make sure we have a subject.
// If the event didn't include it, then
// pull it from the configuration.
// If we still don't have a subject, then
// just make one up.
if (event.subject == null) {
event.subject = config.defaultSubject;
if (event.subject == null) {
event.subject = "Mail from {{name}}";
}
}
console.log('Loading template from ' + config.templateKey + ' in ' + config.templateBucket);
// Read the template file
s3.getObject({
Bucket: config.templateBucket,
Key: config.templateKey
}, function (err, data) {
if (err) {
// Error
console.log(err, err.stack);
context.fail('Internal Error: Failed to load template from s3.')
} else {
var templateBody = data.Body.toString();
console.log("Template Body: " + templateBody);
// Convert newlines in the message
if (event.message != null) {
event.message = event.message
.replace("\r\n", "<br />")
.replace("\r", "<br />")
.replace("\n", "<br />");
}
// Perform the substitutions
var subject = event.subject;
console.log("Final subject: " + subject);
var message = templateBody;
console.log("Final message: " + message);
var params = {
Destination: {
ToAddresses: [
config.targetAddress
]
},
Message: {
Subject: {
Data: subject,
Charset: 'UTF-8'
}
},
Source: config.fromAddress,
ReplyToAddresses: [
event.name + '<' + event.email + '>'
]
};
var fileExtension = config.templateKey.split(".").pop();
if (fileExtension.toLowerCase() == 'html') {
params.Message.Body = {
Html: {
Data: message,
Charset: 'UTF-8'
}
};
} else if (fileExtension.toLowerCase() == 'txt') {
params.Message.Body = {
Text: {
Data: message,
Charset: 'UTF-8'
}
};
} else {
context.fail('Internal Error: Unrecognized template file extension: ' + fileExtension);
return;
}
// Send the email
ses.sendEmail(params, function (err, data) {
if (err) {
console.log(err, err.stack);
context.fail('Internal Error: The email could not be sent.');
} else {
console.log(data); // successful response
context.succeed('The email was successfully sent to ' + event.email);
}
});
}
});
};
这是测试json数据。
{
"email": "example@gmail.com",
"name": "Customer Name",
"message": "Hello World!\nGoing to the moon!"
}
在config.js中,我放置了发件人电子邮件和模板文件
"use strict";
var config = {
"templateBucket" : "XXXXX",
"templateKey" : "Template.html",
"targetAddress" : "example@email.com",
"fromAddress": "Me <example@email.com>",
"defaultSubject" : "Email From {{Lemon}}"
};
module.exports = config;
答案 0 :(得分:2)
您指出的存储库没有提到有关SES设置的信息。您需要先验证电子邮件发件人,然后才能发送电子邮件。
这是亚马逊SES上关于如何做到这一点的快速入门指南。 https://docs.aws.amazon.com/ses/latest/DeveloperGuide/quick-start.html
您应该能够在验证发件人后发送电子邮件。