Skip to content

Instantly share code, notes, and snippets.

@mshuber1981
Last active June 30, 2024 17:05
Show Gist options
  • Save mshuber1981/591573070957a6102770ce99dec2f936 to your computer and use it in GitHub Desktop.
Save mshuber1981/591573070957a6102770ce99dec2f936 to your computer and use it in GitHub Desktop.
AWS Contact Form Lambda (Node.js) with SES example
// Full example - https://aws.amazon.com/blogs/architecture/create-dynamic-contact-forms-for-s3-static-websites-using-aws-lambda-amazon-api-gateway-and-amazon-ses/
import { SESClient, SendEmailCommand, SendTemplatedEmailCommand } from "@aws-sdk/client-ses"; // need package.json with "type": "module" for import
// Set the region (example - us-east-2)
const ses = new SESClient({ region: "us-east-2" });
// https://docs.aws.amazon.com/ses/latest/DeveloperGuide/verify-email-addresses.html
const SENDER = 'Your verified SES email address';
export const handler = async(event) => {
const sendCommand = new SendEmailCommand({
Destination: {
ToAddresses: [SENDER],
},
Message: {
Body: {
Text: { Data: 'Name: ' + event.name + '\nEmail: ' + event.email + '\nMessage: ' + event.message }
},
Subject: { Data: 'Contact Form: ' + event.name, },
},
Source: SENDER,
});
const sendTemplateCommand = new SendTemplatedEmailCommand({
Destination: {
ToAddresses: [
`${event.email}`
]
},
Source: SENDER,
Template: 'ContactForm',
TemplateData: '{\"message\":\"Thank you!\"}',
ConfigurationSetName: 'ContactForm'
})
try {
const mainResponse = await ses.send(sendCommand);
const replyResponse = await ses.send(sendTemplateCommand)
console.log(mainResponse, replyResponse)
}
catch (error) {
console.log(error)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment