简体   繁体   English

如何使用AWS SES和Lambda解决发送电子邮件的问题?

[英]How to fix sending emails with AWS SES and Lambda?

I'm setting up a simple NodeJS Lambda function on AWS to send emails. 我正在AWS上设置一个简单的NodeJS Lambda函数来发送电子邮件。

The code below is working when I run it locally. 当我在本地运行时,下面的代码正在工作。 I have received all the credentials from AWS, verified both sender and recipient emails and granted all permissions to SES for my Lamba 我已收到来自AWS的所有凭据,已验证发件人和收件人电子邮件,并为我的Lamba授予了对SES的所有权限

const aws = require("aws-sdk");
const config = require('../config')

aws.config.update({
  accessKeyId: config.mailUser,
  secretAccessKey: config.mailPassword,
  region:'us-east-1'
});

const ses = new aws.SES({apiVersion: '2010-12-01'});

const sendEmail = async (mailOptions) => {
  const {
    from,
    to,
    subject,
    html
  } = mailOptions
  console.log('foo')
  ses.sendEmail({
    Source: from,
    Destination: {
      ToAddresses: [to]
    },
    Message: {
      Subject: {
        Data: subject,
        Charset: 'UTF-8'
      },
      Body: {
        Html: {
          Data: html,
          Charset: 'UTF-8'
        }
      }
    }
  }, 
  (err, data) => {
    console.log('baz')
    if (err) {
      console.error(err);
    } else {
      console.log('Email sent:');
      console.log(data);
    }
  });
};
console.log('bar')
module.exports = { 
  sendEmail 
};

It seems that ses.sendEmail() never fires when the function is deployed - I get foo and bar in the CloudWatch logs but never baz . 似乎ses.sendEmail()永远不会在该函数部署时触发-我在CloudWatch日志中获取了foobar但从没有baz Again, everything is running smoothly if run locally. 同样,如果在本地运行,则一切运行会很平稳。
What is it that I am missing? 我想念的是什么?

https://www.reddit.com/r/aws/comments/bf2iss/lambda_function_not_able_to_send_email_using_ses/elb8vzr/ https://www.reddit.com/r/aws/comments/bf2iss/lambda_function_not_able_to_send_email_using_ses/elb8vzr/

Here is a very good explanation - apparently you need to wrap your ses.sendMail call in a promise for it to work with AWS Lambda. 这是一个很好的解释-显然,您需要将ses.sendMail调用包装在一个与AWS Lambda一起使用的承诺中。

All credit goes to https://www.reddit.com/user/jsdfkljdsafdsu980p/ and https://www.reddit.com/user/Enoxice/ 所有功劳归于https://www.reddit.com/user/jsdfkljdsafdsu980p/https://www.reddit.com/user/Enoxice/

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

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