Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 222 Vote(s) - 3.63 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sending email via AWS SES within AWS Lambda function

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



Reply

#2
This is because Lambda freezes the container when the function exits and any async processes are frozen, such as your email. This is especially true with Node. See Lambda Programming Model.

[To see links please register here]

Reply

#3
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);
}
});};
Reply

#4
My case is: when you set VPC, the issue happens cause of internet limitation access.

If you remove VPC, everything works fine.

It seems a AWS bug for me.

I opened today a AWS Support for it.

No anwers yet.
Reply

#5
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()
};
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through