- Load template files from disk or embedded store.
- Preview rendered template files with example data in browser with go-mail-preview tool.
- Automatically inlines CSS.
- Powered by Go's html/template and text/template engine.
- Supports markdown to HTML parsing inside template.
- Use provider to send an email.
import (
"github.com/mattes/go-mail"
"github.com/mattes/go-mail/provider/mailgun"
)
// load templates
tpl, err := mail.NewTemplates(mail.FilesFromLocalDir("./templates"))
// create mail envelope
m := mail.New()
m.Subject = "Advice to self"
m.To("Mattes", "mattes@example.com")
m.Template(tpl, "simple", mail.Vars{
"Body": "no ice cream after midnight",
})
// send email with mailgun (or any other provider)
p, err := mailgun.New("mailgun-domain", "mailgun-key")
err = p.Send(m)
Emails can have a HTML and/or text body. Templates are recognized by their file extension.
my-template.html -> used for html body, processed with Go's html/template engine
my-template.txt -> used for text body, processed with Go's text/template engine
my-template.example.yaml -> used for preview
To embed templates into your Go binary, you can use a tool like go.rice.
Install go.rice first:
go get github.com/GeertJohan/go.rice
go get github.com/GeertJohan/go.rice/rice
Update your go code:
//go:generate rice embed-go
var MyTemplates = rice.MustFindBox("./path/to/templates")
Run go generate
to generate the embedded file. See files.go and
files_test.go for an example.
There is a couple of tested email templates available, please have a look at: