Releases: torrayne/formailer
Back to Go 1.15
Go 1.16 embeds aren't well supported. So I've gone back to go generate
for now.
Complex submission data
In the interest of supporting more complex submissions, we've changed Submission.Values
from map[string]string
to map[string]interface{}
. I'm not a huge fan of interface{}
but I think it's the only way to do this.
The default template now supports arrays along with the first template function: IsSlice
. I will be adding functions as I need them in my own work or if requested.
The customization update
This is a large update with breaking changes. Up until now Formailer has been very rigid. Simple but rigid. This update hopes to solve that with configuration options.
Forms
Previously you could only set form options in the ENV. Now you set them in your code before your handler runs.
// Vercel Example
package main
import (
"net/http"
"github.com/djatwood/formailer"
)
// Formailer handles all form submissions
func Formailer(w http.ResponseWriter, r *http.Request) {
cfg := make(formailer.Config)
cfg.Set(
&formailer.Form{
To: "[email protected]",
From: `"Company" <[email protected]>`,
Subject: "New Submission",
Redirect: "/success",
}, &formailer.Form{
Name: "Contact",
To: "[email protected]",
From: `"Company" <[email protected]>`,
Subject: "New Contact Submission",
Redirect: "https://domin.com/thankyou",
},
)
cfg.Vercel(w, r)
}
SMTP
SMTP config is still all done through the ENV but now you can add an SMTP config per form.
# Default
SMTP_HOST=mail.example.com
SMTP_PORT=587
SMTP_USER=[email protected]
SMTP_PASS=mysupersecretpassword
# Overrides
SMTP_CONTACT_USER=[email protected]
SMTP_CONTACT_PASS=youcantguessthispassword
We check first for SMTP_FORM-NAME_FIELD
and if that isn't set we use SMTP_FIELD
. You can override all four SMTP vars.
Templates
Forms have a new optional variable for Go HTML templates.
cfg := make(formailer.Config)
cfg.Set(&formailer.Form{
To: "[email protected]",
From: `"Company" <[email protected]>`,
Subject: "New Submission",
Redirect: "/success",
Template: defaultTemplate,
}
defaultTemplate := `
<html>
<head>
<style>
h3 {
color: #000;
margin-bottom: 5px;
}
p {
color: #333;
margin-bottom: 2rem;
}
</style>
</head>
<body>
{{ range $name, $value := .Values }}
<h3>{{$name}}</h3>
<p>{{$value}}</p>
{{ end }}
</body>
</html>
`
Custom Handlers
I've provided pre-built serverless functions for both Netlify and Vercel. But now you can build you own.
func Handler(w http.ResponseWriter, r *http.Request) {
// pre-processing, check HTTP method
// parse body
submission, err := c.Parse(r.Header.Get("Content-Type"), body.String())
// check for EOF when submitting as multipart/form-data
if err != nil && err != io.EOF {
// handle error
return
}
// manipulate data, check honey pot fields
// Get SMTP server from env
server, err := submission.Form.SMTPServer()
if err != nil {
// handle error
return
}
// Send email
err = submission.Send(server)
if err != nil {
// handle error
return
}
// handle success
}
Added Vercel Support
v0.2.0 Updated README
Pre-release 3
I still can't fix the whole every email is attached to the same thread. But I think that's the last thing.
Pre-release 2
All the content types are supported. Redirects are applied as soon as possible.
Redirects
Pre-release 1
v0.1.7 Refactor and better error handling