Skip to content

Instantly share code, notes, and snippets.

@marcieltorres
Created August 25, 2018 02:05
Show Gist options
  • Save marcieltorres/1171718a18b33b9533105b7784e90918 to your computer and use it in GitHub Desktop.
Save marcieltorres/1171718a18b33b9533105b7784e90918 to your computer and use it in GitHub Desktop.
NodeJS: Sending mail with AWS Lambda Function
var aws = require('aws-sdk');
exports.handler = (event, context, callback) => {
var _returnMessage = "";
var _destinationAddress, _sourceAddress = '[email protected]';
var _defaultSubject = '[Subject]';
var _defaultMessageData = '[Empty Message]';
//params SES
var params = {
Destination: { ToAddresses: _destinationAddress },
Message: {
Subject: {
Data: event.Subject ? event.Subject : _defaultSubject ,
Charset: 'UTF-8'
},
Body: {
Text: {
Data: event.Message ? event.Message : _defaultMessageData,
Charset: 'UTF-8'
}
},
},
Source: _sourceAddress
};
_returnMessage = "Sending...";
//Send email SES
var ses = new aws.SES();
ses.sendEmail(params, function (err, data) {
if (err) {
console.log(err, err.stack);
context.fail('Error');
_returnMessage = "Error";
} else {
console.log(data); // successful response
context.succeed('Successful');
_returnMessage = "Successful.";
}
});
callback(null, _returnMessage);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment