在AWS Lambda函数中通过AWS SES发送电子邮件 [英] Sending email via AWS SES within AWS Lambda function

查看:348
本文介绍了在AWS Lambda函数中通过AWS SES发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AWS Lambda上创建了一个非常基本的简单函数,该函数将用于接受表单提交.

I have created a very basic simple function on AWS Lambda which will be used to accept form submissions.

该功能的一部分将是向特定的人发送电子邮件,非常简单.我正在尝试使用AWS SES来执行此操作.我已经设置了SES服务等,并验证了我希望发送到的帐户并能够发送测试电子邮件.所有作品!!

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. 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. All works!!

现在,当我尝试在AWS Lambda中执行相同的操作并使用aws sdk时,它不会发送电子邮件.我没有收到任何错误.

Now when I try and do the same within AWS Lambda and use the aws sdk it doesn't send out the email. I don't get an error or anything.

以下是我用于AWS Lambda函数的代码.

Below is the code that I am using for the AWS Lambda function.

有人通过lambda函数使用lambda并通过ses发送电子邮件有任何经验吗?甚至只是使用node.js aws sdk很有可能会有所帮助.

Has anyone had any experience using lambda and sending emails via ses, through a lambda function? Or even just using the node.js aws sdk would more than likely be helpful.

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);
};

推荐答案

似乎我将context.succeed(event)放在错误的代码区域中.

It would appear that I had the context.succeed(event) placed in the wrong area of code.

一旦将其移到sendEmail回调中,一切正常.

Once I moved it into the sendEmail callback all worked.

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);
    }
});};

这篇关于在AWS Lambda函数中通过AWS SES发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆