Skip to content
/ go-mail Public

Render and send emails with Go

License

Notifications You must be signed in to change notification settings

mattes/go-mail

Repository files navigation

go-mail GoDoc

Usage

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", "[email protected]")
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)

Templates

Structure

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

Embed templates into Go binary

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.

Nice templates

There is a couple of tested email templates available, please have a look at: