Created
June 12, 2022 18:41
-
-
Save pacarvalho/88961576f38d1a91448c2ff260c06493 to your computer and use it in GitHub Desktop.
Medium - Serverless Form - index.js
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
const aws = require("aws-sdk"); // Library to interact with AWS resources | |
const ses = new aws.SES({ region: "us-east-1" }); | |
exports.handler = async (eventObject, context, callback) => { | |
// Log event data to ease debugging | |
console.log( | |
"Received event:", | |
JSON.stringify(eventObject, context, callback) | |
); | |
const emailParams = { | |
Destination: { | |
ToAddresses: ["admin@my-domain.com"], | |
}, | |
Message: { | |
Body: { | |
Text: { | |
Data: | |
JSON.stringify(eventObject, context, callback) // TODO: Format your data for email here | |
}, | |
}, | |
Subject: { Data: "Contact Us Website Form" }, | |
}, | |
Source: "admin@my-domain.com", | |
}; | |
// Send the email | |
await ses.sendEmail(emailParams).promise(); | |
// API response | |
const response = { | |
statusCode: 204, | |
headers: { | |
"Access-Control-Allow-Headers": "Content-Type", | |
"Access-Control-Allow-Origin": "https://my-domain.com", | |
"Access-Control-Allow-Methods": "OPTIONS,POST", | |
}, | |
isBase64Encoded: false, | |
body: null, | |
}; | |
return response; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment