【问题标题】:lambda function email not sending through nodeJSlambda函数电子邮件不通过nodeJS发送
【发布时间】:2018-06-16 03:53:42
【问题描述】:

在查看了一些教程后,我尝试实现一个 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;

【问题讨论】:

  • 同时检查垃圾邮件文件夹
  • @PranavCBalan 是的,我已经检查过了。我按照这个 git 存储库在 AWS lambda github.com/eleven41/aws-lambda-send-ses-email 中构建我的系统
  • 您是否检查过您的 SES 资源是否处于沙盒模式?
  • @TomMelo Tx 供您发表评论。我正在尝试,但不确定从哪里可以检查。

标签: node.js amazon-web-services aws-lambda aws-sdk amazon-ses


【解决方案1】:

您指出的存储库没有提到 SES 设置。您需要先验证电子邮件发件人,然后才能发送电子邮件。

这里是亚马逊关于如何做到这一点的 SES 快速入门指南。 https://docs.aws.amazon.com/ses/latest/DeveloperGuide/quick-start.html

验证发件人后,您应该可以发送电子邮件了。

【讨论】:

  • 是的,你是对的。需要进行电子邮件验证。我申请并发送了电子邮件:)。您能给我一些建议吗?如何使用 API 将其集成到我的项目联系表中?
  • 您只需要确认发件人,这是一个一次性的任务。无需验证接收方。
猜你喜欢
  • 2020-10-01
  • 2015-12-13
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 1970-01-01
  • 2019-03-05
  • 1970-01-01
  • 2016-04-22
相关资源
最近更新 更多