Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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!!

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.

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

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: ["[email protected]"]         },         Message: {             Body: {                 Text: {                     Data: output.Key1                 }             },             Subject: {                 Data: "Ses Test Email"             }         },         Source: "[email protected]"     };      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); }; 
like image 882
Darren Avatar asked Sep 16 '15 12:09

Darren


People also ask

How do I send a message in Lambda function?

Enter a “sendsms” as the name of the Lambda function and “Send an SMS text message to a phone.” for the description field. In the Runtime drop-down, select Python 3.6 for the programming language. Below the Runtime drop-down there is a large text box for code, pre-populated with a stub lambda_handler function.

How do you trigger Lambda function in SES?

When you use Amazon SES to receive messages, you can configure Amazon SES to call your Lambda function when messages arrive. The service can then invoke your Lambda function by passing in the incoming email event, which in reality is an Amazon SES message in an Amazon SNS event, as a parameter.


1 Answers

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

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: ["[email protected]"]     },     Message: {         Body: {             Text: {                 Data: output.Key1             }         },         Subject: {             Data: "Ses Test Email"         }     },     Source: "[email protected]" };  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);     } });}; 
like image 52
Darren Avatar answered Sep 24 '22 04:09

Darren