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
- SMTP over TLS support, with automatic STARTTLS upgrades for plaintext
connections
Installation
If you're using go mod
:
go get -v github.com/xenking/mailyak/v3
Or with GOPATH
:
go get -v github.com/xenking/mailyak
Usage
package main
import (
"net/smtp"
"github.com/xenking/mailyak/v3"
)
func main() {
my := mailyak.New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com"))
mail := my.NewMail()
mail.To("dom@itsallbroken.com")
mail.From("jsmith@example.com")
mail.FromName("Bananas for Friends")
mail.Subject("Business proposition")
if err := t.ExecuteTemplate(mail.HTML(), "htmlEmail", data); err != nil {
panic(" 💣 ")
}
mail.Plain().SetString("Get a real email client")
if err := my.Send(mail); err != nil {
panic(" 💣 ")
}
}
To send an attachment:
package main
import (
"net/smtp"
"github.com/xenking/mailyak/v3"
)
func main() {
my := mailyak.New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com"))
mail := my.NewMail()
mail.To("dom@itsallbroken.com")
mail.From("oops@itsallbroken.com")
mail.Subject("I am a teapot")
mail.HTML().SetString("Don't panic")
mail.Attach("filename.txt", &input)
if err := my.Send(mail); 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.