Skip to content

matthiasmullie/post-to-email

Repository files navigation

Post to email

Image by fullvector on Freepik

Contact forms for static websites

Here's a solution for those with static websites wanting a contact form, but find themselves in a situation where they can't provide a script to support it (e.g. your platform doesn't allow you to host such script, or you simply don't know how)

This project provides a simple endpoint to submit your forms to, turning those form fields into an email to you.

All you need is:

  • An SMTP server, for outgoing mails (e.g. Gmail or Outlook)
  • A platform that supports deploying Docker containers (e.g. render)
  • Your static website with a form, hosted anywhere

For your convenience, this project is wrapped into a Docker container that can easily be deployed on any Paas - some of which will allow you to deploy this for free. Read on!

Instructions

1. Deploy the container

Run the matthiasmullie/post-to-email Docker container in whatever configuration makes sense.

Below are detailed instructions to run this in a couple of different ways - pick whichever makes most sense for your use-case:

2. Build a simple form & submit it

Submit a POST request (with remaining variables - see configuration)

Check out this simple example to build a form that submits forms to the service we deployed in the first step.

And here's information about mitigating spam in case you were worried about that!

3. Receive email

You've got this! We're done here.

Configuration

Email configuration can be set globally on the server hosting this script (via environment variables), or per request on the client calling this (via POST or GET params)

If set, environment variables always get precedence over POST, which gets precedence over GET params. This can be useful to lock down certain settings (e.g. RECIPIENT or DSN) within your hosting environment to prevent abuse.

Available params:

  • ALLOW_ORIGIN [ENV only]: allowed CORS domain
  • DSN [ENV only]: DSN string for a transport (e.g. SMTP) to send emails with
  • SENDER: sender email address
  • RECIPIENT: recipient email address
  • REPLY_TO (optional, defaults to SENDER): reply-to email address
  • SUBJECT (optional, defaults to "Form to email"): email subject
  • REDIRECT (optional, defaults to referrer): location to redirect back to after handing submission, or explicitly blank for no redirect
  • HONEYPOT (optional, defaults to none): Name of a form field to use as honeypot to filter out unwanted (spam) submissions