在Lambda异步export.handler调用下,Nodemailer静默失败

时间:2019-04-13 18:42:44

标签: asynchronous aws-lambda nodemailer

我可能缺少一些简单的东西,但是已经用光了尝试/测试的东西。

我有两个要在AWS Lambda下测试的(几乎相同)nodemail函数,一个使用回调调用,一个使用较新的异步调用。当我在本地测试(使用“ Lambda-本地”)时,两者均按预期工作(即,我收到了预期的电子邮件)。当我在Lambda中进行测试时,回调版本可以按预期工作,而异步版本会以静默方式失败-没有错误,也没有任何提示。

注意:在Lambda下,我一直在通过回调使用Nodemail进行生产,没有任何问题,我只是想尝试新的异步export.handler包装器。...

异步版本(无提示失败):

'use strict'

var aws = require("aws-sdk");
var nodemailer = require("nodemailer");

const ses = new aws.SES({ region: 'us-east-1' });

exports.handler = async (event, context) => {

    var mailOptions = {
        from: "mymail@mydomain.com",
        subject: "This is an email sent from a Lambda function using async!",
        html: `<p>Test message</p>`,
        to: "mymail@hotmail.com",
    };

    // create Nodemailer SES transporter
    var transporter = nodemailer.createTransport({
        SES: ses
    });

    // send email
    transporter.sendMail(mailOptions)
    .then( results => {

        console.log("Success:", results);

        return {
            statusCode: 200,
            body: JSON.stringify({ "API Success": results }, null),
            headers: {
               'Access-Control-Allow-Origin': '*',
           },
        };    

    })
    .catch ( error => {
        console.log("Error:", error);

        return {
            statusCode: 500,
            body: JSON.stringify({ "API Error": error }, null),
            headers: {
               'Access-Control-Allow-Origin': '*',
           },
        };    

    })

};

回调版本(按预期工作):

'use strict'

var aws = require("aws-sdk");
var nodemailer = require("nodemailer");

const ses = new aws.SES({ region: 'us-east-1' });

exports.handler = function (event, context, callback) {

    var mailOptions = {
        from: "mymail@mydomain.com",
        subject: "This is an email sent from a Lambda function using callbacks!",
        html: `<p>Test message</p>`,
        to: "mymail@hotmail.com",
    };

    // create Nodemailer SES transporter
    var transporter = nodemailer.createTransport({
        SES: ses
    });

    // send email
    transporter.sendMail(mailOptions)
    .then( results => {

        console.log("Success:", results);

        callback(null, {
            statusCode: 200,
            body: JSON.stringify(results),
            headers: {
               'Access-Control-Allow-Origin': '*',
           },
        });  

    })
    .catch ( error => {
        console.log("Error:", error);

        callback(null, {
            statusCode: 500,
            body: JSON.stringify(error),
            headers: {
               'Access-Control-Allow-Origin': '*',
           },
        });  

    })

};

0 个答案:

没有答案