AWS SES send email lambda not sending every time

白昼怎懂夜的黑 提交于 2020-03-05 02:22:00

问题


I want to send emails using the ses from aws from lambda. The problem is that the email is only sent some times using the same code. We don't get errors.

Here's the code:

const AWS = require('aws-sdk');
var ses = new AWS.SES();

exports.handler = async (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false;

    await new Promise((resolve, reject) => {

      var params = {
        Destination: {
            ToAddresses: [myEmail]
        },
        Message: {
            Body: {
                Text: { Data: "Test"

                }

            },

            Subject: { Data: "Test Email"

            }
        },
        Source: "sourceMail"
    };

    ses.sendEmail(params, function (err, data) {

        if (err) {
            console.log(err);
            context.fail(err);
        } else {
            console.log(data);
            context.succeed(event);
        }
     callback(null, {err: err, data: data});
    });

    });
}

回答1:


I would be careful with using callbackWaitsForEmptyEventLoop as it can lead to unexpected results (If this is false, any outstanding events continue to run during the next invocation.).

Can you try using this simplified version:

const AWS = require('aws-sdk');
var ses = new AWS.SES();

exports.handler = async (event, context, callback) => {
  const params = {
    Destination: {
      ToAddresses: [myEmail],
    },
    Message: {
      Body: {
        Text: { Data: 'Test' },
      },

      Subject: { Data: 'Test Email' },
    },
    Source: 'sourceMail',
  };

  await ses.sendEmail(params).promise();

  return event;
};


来源:https://stackoverflow.com/questions/57495748/aws-ses-send-email-lambda-not-sending-every-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!