Created
October 2, 2017 21:41
-
-
Save pbzona/dcfac4627161942d1bd7e76c9ff7ab1f to your computer and use it in GitHub Desktop.
Full code for serverless contact form
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
'use strict'; | |
console.log('Loading function'); | |
const AWS = require('aws-sdk'); | |
const sesClient = new AWS.SES(); | |
const sesConfirmedAddress = "<your-verified-ses-email>"; | |
/** | |
* Lambda to process HTTP POST for contact form with the following body | |
* { | |
"email": <contact-email>, | |
"subject": <contact-subject>, | |
"message": <contact-message> | |
} | |
* | |
*/ | |
exports.handler = (event, context, callback) => { | |
console.log('Received event:', JSON.stringify(event, null, 2)); | |
var emailObj = JSON.parse(event.body); | |
var params = getEmailMessage(emailObj); | |
var sendEmailPromise = sesClient.sendEmail(params).promise(); | |
var response = { | |
statusCode: 200 | |
}; | |
sendEmailPromise.then(function(result) { | |
console.log(result); | |
callback(null, response); | |
}).catch(function(err) { | |
console.log(err); | |
response.statusCode = 500; | |
callback(null, response); | |
}); | |
}; | |
function getEmailMessage (emailObj) { | |
var emailRequestParams = { | |
Destination: { | |
ToAddresses: [ sesConfirmedAddress ] | |
}, | |
Message: { | |
Body: { | |
Text: { | |
Data: emailObj.message | |
} | |
}, | |
Subject: { | |
Data: emailObj.subject | |
} | |
}, | |
Source: sesConfirmedAddress, | |
ReplyToAddresses: [ emailObj.email ] | |
}; | |
return emailRequestParams; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment