简体   繁体   English

在 AWS Lambda 函数中通过 AWS SES 发送电子邮件

[英]Sending email via AWS SES within AWS Lambda function

I have created a very basic simple function on AWS Lambda which will be used to accept form submissions.我在 AWS Lambda 上创建了一个非常基本的简单函数,用于接受表单提交。

Part of the function will be to send an email to a particular person, pretty simple.部分功能是向特定的人发送电子邮件,非常简单。 I am trying to use AWS SES in order to do this.我正在尝试使用 AWS SES 来做到这一点。 I have setup the SES service etc, and verified the account I wish to send to and have been able to send out a test email.我已经设置了 SES 服务等,并验证了我希望发送到的帐户并能够发送测试电子邮件。 All works!!所有作品!

Now when I try and do the same within AWS Lambda and use the aws sdk it doesn't send out the email.现在,当我尝试在 AWS Lambda 中执行相同操作并使用 aws sdk 时,它不会发送电子邮件。 I don't get an error or anything.我没有收到错误或任何东西。

Below is the code that I am using for the AWS Lambda function.下面是我用于 AWS Lambda 函数的代码。

Has anyone had any experience using lambda and sending emails via ses, through a lambda function?有没有人有任何使用 lambda 和通过 ses 通过 lambda 函数发送电子邮件的经验? Or even just using the node.js aws sdk would more than likely be helpful.或者甚至只是使用 node.js aws sdk 很可能会有所帮助。

var aws = require('aws-sdk');
var ses = new aws.SES({
   accessKeyId: 'myAccessKey',
   secretAccesskey: 'mySecretKey',
   region: 'eu-west-1' 
});

exports.handler = function(event, context) {
    console.log("Incoming: ", event);
    var output = querystring.parse(event);

    var eParams = {
        Destination: {
            ToAddresses: ["toAddress@email.com"]
        },
        Message: {
            Body: {
                Text: {
                    Data: output.Key1
                }
            },
            Subject: {
                Data: "Ses Test Email"
            }
        },
        Source: "mysource@source.com"
    };

    console.log('===SENDING EMAIL===');
    var email = ses.sendEmail(eParams, function(err, data){
        if(err) console.log(err);
        else {
            console.log("===EMAIL SENT===");
            console.log(data);
        }
    });
    console.log("EMAIL CODE END");
    console.log('EMAIL: ', email);
    context.succeed(event);
};

It would appear that I had the context.succeed(event) placed in the wrong area of code.看起来我把 context.succeed(event) 放在了错误的代码区域。

Once I moved it into the sendEmail callback all worked.一旦我将它移动到 sendEmail 回调中,一切都有效。

var aws = require('aws-sdk');
var ses = new aws.SES({
  accessKeyId: 'myAccessKey',
  secretAccesskey: 'mySecretKey',
  region: 'eu-west-1' 
});

exports.handler = function(event, context) {
  console.log("Incoming: ", event);
  var output = querystring.parse(event);

  var eParams = {
    Destination: {
        ToAddresses: ["toAddress@email.com"]
    },
    Message: {
        Body: {
            Text: {
                Data: output.Key1
            }
        },
        Subject: {
            Data: "Ses Test Email"
        }
    },
    Source: "mysource@source.com"
};

console.log('===SENDING EMAIL===');
var email = ses.sendEmail(eParams, function(err, data){
    if(err) {
       console.log(err);
       context.fail(err);
    } else {
        console.log("===EMAIL SENT===");
        console.log("EMAIL CODE END");
        console.log('EMAIL: ', email);
        console.log(data);
        context.succeed(event);
    }
});};

This is because Lambda freezes the container when the function exits and any async processes are frozen, such as your email.这是因为当函数退出并且任何异步进程(例如您的电子邮件)被冻结时,Lambda 会冻结容器。 This is especially true with Node.对于 Node.js 来说尤其如此。 See Lambda Programming Model.请参阅 Lambda 编程模型。 http://docs.aws.amazon.com/lambda/latest/dg/lambda-introduction.html http://docs.aws.amazon.com/lambda/latest/dg/lambda-introduction.html

My case is: when you set VPC, the issue happens cause of internet limitation access.我的情况是:当您设置 VPC 时,问题发生的原因是互联网限制访问。

If you remove VPC, everything works fine.如果您删除 VPC,则一切正常。

It seems a AWS bug for me.这对我来说似乎是一个 AWS 错误。

I opened today a AWS Support for it.我今天为它打开了 AWS Support。

No anwers yet.还没有答案。

var aws = require("aws-sdk");
var ses = new aws.SES({ region: "us-west-2" });
exports.handler = async function (event) {
  var params = {
    Destination: {
      ToAddresses: ["RecipientEmailAddress", ...],
    },
    Message: {
      Body: {
        Text: { Data: "Test" },
      },

      Subject: { Data: "Test Email" },
    },
    Source: "SourceEmailAddress",
  };

  return ses.sendEmail(params).promise()
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM