Skip to content

Instantly share code, notes, and snippets.

@diegofcornejo
Last active January 22, 2019 04:35
Show Gist options
  • Save diegofcornejo/8c7e2147a319f57ce82a0ed9cb102142 to your computer and use it in GitHub Desktop.
Save diegofcornejo/8c7e2147a319f57ce82a0ed9cb102142 to your computer and use it in GitHub Desktop.
Send email with nodemailer in lambda
'use strict';
var nodemailer = require('nodemailer');
var aws = require('aws-sdk');
// create Nodemailer SES transporter
var transporter = nodemailer.createTransport({
SES: new aws.SES({
apiVersion: '2010-12-01'
})
});
exports.handler = function(event, context, callback) {
console.log(event);
var mail = event;
var res = {};
transporter.sendMail({
from: typeof mail.from == 'string' ? mail.from : mail.from.name + ' ' + '<' + mail.from.email + '>',
to: mail.to,
cc: mail.cc,
bcc: mail.bcc,
subject: mail.subject,
text: mail.text,
html: mail.html,
attachments: mail.attachments
}, (err, info) => {
if (err) {
console.log(err)
res.message = err;
callback(null, res);
} else {
console.log(info.envelope);
console.log(info.messageId);
res.message = info;
callback(null, res);
}
});
};
//Create a Test event like this way
{
"from": {
"name": "Diego Cornejo",
"email": "[email protected]"
},
"to": [
"[email protected]",
"[email protected]"
],
"subject": "Test DKIM",
"html": "<b>Test mail<b/>",
"text": "Test text plain"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment