Skip to content
An elegant MIME/SMTP email library with support for attachments
Go
Branch: master
Clone or download
domodwyer ci: GitHub actions (#42)
* Add go.mod

* ci: add go test workflow

* readme: remove govendor reference

Bad advice is bad.

* ci: add golangci-lint
Latest commit a05347a Oct 22, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github/workflows
.gitignore Initial commit Jun 23, 2016
.travis.yml ci: test against latest Go versions Nov 7, 2018
LICENSE
README.md
attachments.go
attachments_test.go
examples_test.go
go.mod ci: GitHub actions (#42) Oct 22, 2019
mailyak.go New AddHeader() to specify arbitrary headers (#27) Jul 26, 2018
mailyak_test.go
mime.go Email body should be encoded as 'quoted-printable' (#31) Nov 7, 2018
mime_test.go
package.go
setters.go New AddHeader() to specify arbitrary headers (#27) Jul 26, 2018
setters_test.go
splitter.go
splitter_test.go Chunked splitter writes (#34) Dec 12, 2018
writer.go Documentation updates May 21, 2017
writer_test.go ci: GitHub actions (#42) Oct 22, 2019

README.md

Build Status GoDoc

An elegant MIME mail library with support for attachments




A simple, easy to use email library for Go (golang).
  • Full attachment support (attach anything that implements io.Reader)
  • Send to multiple addresses at the same time, including BCC addresses.
  • Supports composing multi-part messages (HTML and plain text emails for older clients)
  • Write templates directly to the email body (implements io.Writer for convenience)
  • Production ready - several million emails sent in a production environment
  • Comprehensive unit tests

Installation

go get -v github.com/domodwyer/mailyak

Usage

// Create a new email - specify the SMTP host and auth
mail := mailyak.New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com"))

mail.To("dom@itsallbroken.com")
mail.From("jsmith@example.com")
mail.FromName("Prince Fournineteen")

mail.Subject("Business proposition")

// mail.HTML() and mail.Plain() implement io.Writer, so you can do handy things like
// parse a template directly into the email body
if err := t.ExecuteTemplate(mail.HTML(), "htmlEmail", data); err != nil {
	panic(" 💣 ")
}

// Or set the body using a string setter
mail.Plain().Set("Get a real email client")

// And you're done! 
if err := mail.Send(); err != nil {
	panic(" 💣 ")
}

To send an attachment:

mail := mailyak.New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com"))

mail.To("dom@itsallbroken.com")
mail.From("oops@itsallbroken.com")
mail.Subject("I am a teapot")
mail.HTML().Set("Don't panic")

// input can be a bytes.Buffer, os.File, os.Stdin, etc.
// call multiple times to attach multiple files
mail.Attach("filename.txt", &input)

if err := mail.Send(); err != nil {
	panic(" 💣 ")
}

Notes

  • Why "MailYak"? Because "MailyMcMailFace" is annoyingly long to type.
  • You can use a single instance of mailyak to send multiple emails after changing the to/body/whatever fields, avoiding unnecessary allocation/GC pressure.
  • Attachments are read when you call Send() to prevent holding onto multiple copies of the attachment in memory (source and email) - this means changing the attachment data between calling Attach() and Send() will change what's emailed out!
  • For your own sanity you should vendor this, and any other libraries when going into production.
You can’t perform that action at this time.