Serverless lambda to send email from HTML form
This lambda function sends email using Amazon SES (Simple Email Service).
Installation and Setup
- Install and setup serverless.
- Verify 'From' and 'To' email addresses in SES Console.
- Deploy this lambda by
sls deploy
and remember it's POST endpoint: it should be used in your HTML form JS code. - Open Lambda function configuration console and add an environment variable. The name of variable should be in lowercase, and it is passed as a query ?source=<env_variable_name> parameter to the POST URL above. The value should be in JSON and have three required parameters: from, to and subject. For example:
{"from":"from@example.com","to":"to@example.com","subject":"My Static Website Contact Form","noheaders":false}
- Use sample HTML form, CSS and JS from this repo on your static site. Use your POST URL in the code, like
https://asd123qwe.execute-api.eu-west-1.amazonaws.com/dev/send?source=your_env_variable_name
Enjoy!
Notes
- JS code sends all form inputs with name="" attribute.
- For better formatting, use name="First Name" + name="Last Name" or name="Name", and name="Email" inside your
<input>
tags.