Sending mails from HTML forms with Go
Hugo is a great static page generator and fast and I enjoy working with it. It was the right decision. Now, the page is responsive, quick, and easy to maintain.
Yet, one thing I couldn’t easily accomplish with Hugo was sending mails from contact forms – a form mailer. With WordPress, this is pretty easy since it’s PHP under the hood on the server side. But with Hugo, the pages are static HTML.
What started as a quick and dirty “I want to send HTML forms as mail” service quickly transformed into a fully-fledged web service. At the time of this post, the service supports the following features:
- Single-binary Webservice
- Multi-form support
- Multiple recipients per form
- Only display form-fields that are configured in for the form in the resulting mail
- Check for required form fields
- Anti-SPAM functionality via honeypot fields
- Limit form access to specific domains
- Per-form mail server configuration
- hCaptcha support
- reCaptcha v2 support
- Anti-SPAM functionality via built-in, auto-expiring and single-use security token feature
- Form field type validation (text, email, number, bool)
- Confirmation mail to poster
- Custom Reply-To header based on sending mail address
It is FOSS and can be found on Github. Maybe it can help you with a similar problem.
In the first iterations of
js-mailer I used the popular go-gomail/gomail package for handling the mail-sending part. Unfortunately, the project isn’t maintained anymore and uses some pretty outdated opinions on handling the mail sending.