Created
October 29, 2020 22:13
-
-
Save DukeRupert/0d3d3775cd66e5d5c2845a9cdd9740fb to your computer and use it in GitHub Desktop.
Netlify function to email form submission using Postmark
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
// Require: | |
const postmark = require('postmark'); | |
// Declare env variables | |
const token = process.env.POSTMARK; | |
const from = process.env.FROM_ADDRESS; | |
const to = process.env.TO_ADDRESS; | |
var client = new postmark.Client(token); | |
// Send an email | |
exports.handler = async (event, context) => { | |
console.log('submission created error testing'); | |
let date = new Date(); | |
let body = '' | |
// Grab form submission payload | |
let payload = JSON.parse(event.body).payload; | |
for (const [key, value] of Object.entries(payload.human_fields)) { | |
console.log(`${key}: ${value}`); | |
let text = JSON.stringify(`${key}: ${value}`); | |
body += text + "\n"; | |
} | |
// Send the email | |
try { | |
client.sendEmail({ | |
"From": from, | |
"To": to, | |
"Subject": "Form Submission", | |
"TextBody": body, | |
}); | |
return { | |
statusCode: 200, | |
body: 'Email sent successfully' | |
} | |
} catch (err) { | |
return { statusCode: 500, body: err.toString() } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment