Created
January 27, 2019 17:08
-
-
Save jpomykala/a3548903e3454f7d65443053ec412b65 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
<div> | |
<form action="#" id="callbackForm" class="contact-form"> | |
<div class="form-group"> | |
<label for="email">Email</label> | |
<input type="email" required id="email" class="form-control" placeholder="" autocomplete="email" name="email" /> | |
</div> | |
<div class="form-group"> | |
<label for="name">Message</label> | |
<input id="message" type="text" class="form-control" placeholder="" name="message" /> | |
</div> | |
<button type="submit" id="sendMessageButton" class="btn btn-primary btn-block"> | |
Send message | |
</button> | |
</form> | |
<script> | |
$("#callbackForm").submit(function(e) { | |
e.preventDefault(); | |
var replyTo = $("#email"); | |
var name = $("#name"); | |
var data = { | |
"_sendTo": "<your_email>", | |
"_replyTo": replyTo.val(), | |
"message": message.val() | |
}; | |
var url = "<API_GATEWAY_URL>"; | |
$.ajax({ | |
url: url, | |
type: 'POST', | |
crossDomain: true, | |
data: JSON.stringify(data), | |
dataType: 'json', | |
contentType: "application/json" | |
}); | |
}); | |
</script> | |
</div> |
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"); | |
const successResponse = { | |
"statusCode": 200, | |
"headers": { | |
"Content-Type": "application/json", | |
}, | |
"body": JSON.stringify({ message: ":)" }), | |
"isBase64Encoded": false | |
}; | |
const errorResponse = { | |
"statusCode": 500, | |
"headers": { | |
"Content-Type": "application/json", | |
}, | |
"body": JSON.stringify({ message: "something bad happen, check logs" }), | |
"isBase64Encoded": false | |
}; | |
exports.handler = async(event, context, callback) => { | |
aws.config.update({region: 'eu-west-1'}); | |
const requestBody = event.body; | |
const request = JSON.parse(requestBody); | |
console.log(request); | |
const allowedDomains = ['example.com', 'jpomykala.me', 'yourdomain.com']; | |
const sendToEmail = request._sendTo || ""; | |
let domain = ""; | |
try { | |
const emailSplit = sendToEmail.split('@'); | |
const arraySize = emailSplit.length; | |
domain = emailSplit[arraySize - 1]; | |
const domainIsAllowed = allowedDomains.includes(domain); | |
if (!domainIsAllowed) { | |
throw new Error("Destination email not allowed for domain=", domain); | |
} | |
} | |
catch (e) { | |
console.warn(e.message, e.name, sendToEmail); | |
callback(JSON.stringify(errorResponse)); | |
} | |
const params = { | |
Destination: { | |
ToAddresses: [sendToEmail] | |
}, | |
Message: { | |
Body: { | |
Html: { | |
Charset: "UTF-8", | |
Data: ` | |
<body> | |
<p>${request.message}</p> | |
<pre>${JSON.stringify(request, undefined, 2)}</pre> | |
</body> | |
` | |
} | |
}, | |
Subject: { | |
Charset: "UTF-8", | |
Data: `New submission` | |
} | |
}, | |
Source: `${request.name || "Name unknown"} <[email protected]>`, | |
ReplyToAddresses: [request._replyTo] | |
}; | |
const sendPromise = new aws.SES() | |
.sendEmail(params) | |
.promise(); | |
await sendPromise | |
.then(data => { | |
console.log(`E-mail sent to ${sendToEmail}`); | |
console.log(successResponse); | |
callback(null, successResponse); | |
}) | |
.catch(err => { | |
console.log("E-mail NOT sent", err); | |
console.log(errorResponse); | |
callback(errorResponse); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment