Site icon Golang Libraries, Apps, Golang Jobs and Go Tutorials

How to send Emails from Forms with Golang

send form emails with go

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.

So I used JavaScript and some form mailer to send contact form requests. There are 3rd party services like https://formspree.io/ that are specialized in this, but since I don’t wanna invest money just to send forms and I also don’t trust the data privacy of such services (GDPR is a big thing in the EU you know 😉), I decided to write my little web service for it.

Introducing js-mailer – Golang-based Web Service

js-mailer is a straightforward, yet powerful web service written in Golang that allows static websites to send forms via JavaScript’s fetch() or XMLHttpRequest APIs.

It follows a two-step workflow. First, your JavaScript requests a token from the API using the /api/v1/token endpoint. If the request is valid and the website is authorized to request a token, the API will respond with a TokenResponseJson. This holds some data that must be included in your form as hidden inputs. It will also provide a submission URL endpoint /api/v1/send/<formid>/<token> that can be used as an action in your form. Once the form is submitted, the API will validate that all submitted data is correct and submit the form data to the configured recipients.

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:

It is FOSS and can be found on Github. Maybe it can help you with a similar problem.

Sending mails with Golang

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.

Exit mobile version