Last active
September 7, 2017 21:05
-
-
Save yanzv/1191c48c715f24d6b99592cc4af08e6f to your computer and use it in GitHub Desktop.
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
// Load the SES API. | |
var AWS = require('aws-sdk'); | |
var ses = new AWS.SES(); | |
const https = require('https'); | |
const googleRecapchaSecret = "yoursecret"; | |
function validateRecaptcha(recaptchaResponse, sourceIP, callback, error) | |
{ | |
var querystring = require('querystring'); | |
var postData = querystring.stringify({secret : googleRecapchaSecret,response : recaptchaResponse,remoteip : sourceIP}); | |
var options = { | |
hostname: 'www.google.com', | |
path: '/recaptcha/api/siteverify', | |
method: 'POST', | |
port: 443, | |
headers: { | |
'Content-Type': 'application/x-www-form-urlencoded', | |
'Content-Length': Buffer.byteLength(postData) | |
} | |
}; | |
var req = https.request(options, (res) => { | |
res.on('data', (d) => { | |
if (d === undefined){ error('undefined'); return} | |
var response = JSON.parse(d); | |
console.log(response); | |
if(response.success === true){ | |
callback('true'); | |
}else{ | |
callback(response); | |
} | |
}); | |
}); | |
req.write(postData); | |
req.end(); | |
req.on('error', (e) => { | |
//console.log(e); | |
callback('false'); | |
}); | |
} | |
function validateEmail(email) { | |
var tester = /^[-!#$%&'*+\/0-9=?A-Z^_a-z{|}~](\.?[-!#$%&'*+/0-9=?A-Z^_a-z`{|}~])*@[a-zA-Z0-9](-?\.?[a-zA-Z0-9])*(\.[a-zA-Z](-?[a-zA-Z0-9])*)+$/; | |
if (!email) return false; | |
if(email.length>254) return false; | |
var valid = tester.test(email); | |
if(!valid) return false; | |
// Further checking of some things regex can't handle | |
var parts = email.split("@"); | |
if(parts[0].length>64) return false; | |
var domainParts = parts[1].split("."); | |
if(domainParts.some(function(part) { return part.length>63; })) return false; | |
return true; | |
} | |
function sendEmail(name, email,message, callback) | |
{ | |
var messageParts = []; | |
var replyTo = name + " <" + email + ">"; | |
messageParts.push("Name: " + name); | |
messageParts.push("Email: " + email); | |
messageParts.push("Message: " + message); | |
var subject = message.replace(/\s+/g, " ").split(" ").slice(0,10).join(" "); | |
var params = { | |
Destination: { ToAddresses: [ 'your@email.com <your@email.com>' ] }, | |
Message: { | |
Body: { Text: { Data: messageParts.join("\r\n"), Charset: 'UTF-8' } }, | |
Subject: { Data: subject, Charset: 'UTF-8' } | |
}, | |
Source: 'yanzcontactform <your@email.com>', | |
ReplyToAddresses: [ replyTo ] | |
}; | |
ses.sendEmail(params, function(err, data) { | |
if (err) { | |
console.log(err, err.stack); | |
callback('error'); | |
return; | |
} else { | |
console.log(data); | |
callback('sent'); | |
return; | |
} | |
}); | |
} | |
exports.handler = function(event, context) | |
{ | |
if(event.body === undefined) { context.fail('Must provide parameters!'); return; } | |
console.log('Received Event:',event.body); | |
if(event.body.recaptcha === undefined){ | |
context.fail('Recaptcha cannot be validated!'); | |
return; | |
} | |
var recaptchaResponse = event.body.recaptcha; | |
if (!event.body.email) { context.fail('Must provide email'); return; } | |
if (!event.body.message || event.message === '') { context.fail('Must provide message'); return; } | |
var email = unescape(event.body.email); | |
if (!validateEmail(email)) { context.fail('Must provide valid email'); return; } | |
validateRecaptcha(recaptchaResponse, event.sourceIp, function(status){ | |
if(status === 'true'){ | |
sendEmail(event.body.name, event.body.email, event.body.message, function(status){ | |
if(status === 'sent'){ context.succeed('Thank you for dropping us a line!'); return} | |
else if(status === 'error'){ context.fail('Error sending an email. Please resubmit. Thank You!'); return} | |
}); | |
}else{ | |
context.fail('Recaptcha cannot be validated!'); | |
} | |
}, | |
function(error){ | |
context.fail('Recaptcha cannot be validated!'); | |
return; | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment