Created
August 25, 2018 02:05
-
-
Save marcieltorres/1171718a18b33b9533105b7784e90918 to your computer and use it in GitHub Desktop.
NodeJS: Sending mail with AWS Lambda Function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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