Microservice for contact form submission on serverless services such Google Cloud Functions with CORS handling, recaptcha verification, data validation and SMTP client.
It's perfect to use for GitHub Pages, static generated sites and static sites.
This Node service has small footprint as it only require two other packages that do not add new dependencies: jsonschema for validation and nodemailer for SMTP client. Recaptcha verifier uses native modules.
This repository has no master. Read more: GitHub abandons 'master' term to avoid slavery row.
- Zip this project and upload onto the Google Cloud Function
- Set the function to be executed:
main
(on src/index.js) - Set the environment variables (example on .env.example)
ALLOWED_ORIGINS
- allowed origins to make CORS request, include protocol and port if applicable, comma separate multiple entriesSMTP_HOST
- SMTP provider addressSMTP_USER
- e-mail account to use as senderSMTP_PASS
- password for the e-mail accountSERVICE_NAME
- how the service identify itself for the SMTP serverMAIL_TO
- e-mail account to receive the contact formMAIL_SUBJECT
- subject for contact e-mailCAPTCHA_SECRET
- Google recaptcha secret
- Deploy
The local environment runs a HTTP server with express just like in GCP, it can be used to make debuging easier or to run the service on other infrastructure.
The environment variables will be read from ".env" file so copy from .env.example.
After installing dependencies execute npm run serve
to start the service.
Currently this service won't run on AWS Lambda, to add support keep in mind:
- dependencies must be included in the uploaded ZIP (they don't install it for you - Boo AWS!!)
- the property "main" of package.json is not read, Lambda will look for index.js in root directory
- the function to be called currently is
main
but Lambda require it to behandler
- main method call signature does not match - this project uses the express signature
(request, response) => {}
and Lambda has its own signature(event, context, callback) => {}
which impacts how request's data are retrieved and HTTP headers/status code are returned