我正在尝试使用先前经过SES身份验证的帐户从Lambda发送电子邮件;使用Nodemailer使用node.js。没有错误,但也不会发送任何错误。

这是我正在使用的过程:

module.exports.eviarCorreoPrueba = (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;
  console.log('inicia envio de correos');
    var transporter = nodemailer.createTransport({
        //pool: true,
        host: 'email-smtp.us-east-1.amazonaws.com',
        port: 465,
        secure: true,
        auth: {
            user: 'user',
            pass: 'pass'
        }
    });
    console.log('se crea transporte ');

    var mailOptions = {
        from: 'test@email.com',
        to: 'test@email.com',
        subject: 'Prueba Lambda',
        html: 'hello World'
    };
    console.log('se asignan las opciones de correo');
    console.log('inicia envio de correo');
    transporter.sendMail(mailOptions, function (error, info) {
        if (error) {
            callback(null, {
              statusCode: 200,
              body: JSON.stringify({
                input: 'not send'
              })
            })
        } else {
            console.log('Email sent');
        }
    });
    console.log('funcion finalizada');
};


这些是日志答案结果:

node.js - 使用Lambda,NodeJ和Nodemailer发送电子邮件不起作用-LMLPHP

最佳答案

您的Lambda超时。查看您的Cloudwatch日志中的最后一条消息。我认为您将lambda超时设置为6秒,我想这不足以让lambda发送请求(通过transporter.sendMail)并获得响应。尝试增加Lambda时间。大概要30秒?

我为我设计的新lambda设置超时的方式是将它们运行几次,直到获得平均完成时间。然后我增加20秒的时间。

另外,transporter.sendMail是异步函数。这意味着console.log('funcion finalizada')可能会在您的函数完成之前运行(但是您的函数尚未实际完成)。

阅读有关异步javascript和回调的更多信息:https://medium.com/codebuddies/getting-to-know-asynchronous-javascript-callbacks-promises-and-async-await-17e0673281ee

另外,如果要以同步方式编写异步代码,请使用async/await

关于node.js - 使用Lambda,NodeJ和Nodemailer发送电子邮件不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54296952/

10-10 00:50
查看更多