Created
February 14, 2016 18:00
-
-
Save mdesanti/339aa541a97491a87b1b to your computer and use it in GitHub Desktop.
Lambda + SimpleDb + Mandrillapp
This file contains hidden or 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'); | |
var mandrill = require('mandrill-api/mandrill'); | |
function sendTemplate(to, context) { | |
var mandrill_client = new mandrill.Mandrill('YOUR_MANDRILLAPP_KEY'); | |
var template_name = "your_template"; | |
var template_content = []; | |
var message = { | |
"subject": "Email Subject", | |
"from_email": "[email protected]", | |
"from_name": "Someone", | |
"to": [{ | |
"email": to, | |
"type": "to" | |
}] | |
}; | |
var async = true; | |
var ip_pool = undefined; | |
var send_at = new Date(); | |
console.log('About to send email to: ' + to) | |
mandrill_client.messages.sendTemplate({"template_name": template_name, "template_content": template_content, "message": message, "async": async, "ip_pool": ip_pool, "send_at": send_at}, function(result) { | |
console.log(result); | |
context.succeed(result) | |
}, function(e) { | |
// A mandrill error occurred: Unknown_Subaccount - No subaccount exists with the id 'customer-123' | |
console.log('A mandrill error occurred: ' + e.name + ' - ' + e.message); | |
// Mandrill returns the error as an object with name and message keys | |
context.fail("Internal Error: " + JSON.stringify(e)); | |
}); | |
} | |
exports.handler = function(event, context) { | |
var AWS = require('aws-sdk'); | |
var simpledb = new AWS.SimpleDB({ | |
endpoint: 'sdb.amazonaws.com', | |
region: 'us-east-1' | |
}); | |
console.log(event) | |
var params = { | |
Attributes: [ /* required */ | |
{ | |
Name: 'Email', /* required */ | |
Value: event.subscriber.email, /* required */ | |
}, | |
{ | |
Name: 'Timestamp', /* required */ | |
Value: (new Date()).toString(), /* required */ | |
} | |
], | |
DomainName: 'YOUR_DOMAIN_NAME', /* required */ | |
ItemName: event.subscriber.email, /* required */ | |
}; | |
simpledb.putAttributes(params, function(err, data) { | |
if (err) { | |
console.log(err); | |
context.fail("Internal Error: " + JSON.stringify(err)); // an error occurred | |
} | |
else { | |
sendTemplate(event.subscriber.email, context) | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment