# go-mail - Simple and easy way to send mails in Go [![GoDoc](https://godoc.org/github.com/wneessen/go-mail?status.svg)](https://pkg.go.dev/github.com/wneessen/go-mail) [![codecov](https://codecov.io/gh/wneessen/go-mail/branch/main/graph/badge.svg?token=37KWJV03MR)](https://codecov.io/gh/wneessen/go-mail) [![Go Report Card](https://goreportcard.com/badge/github.com/wneessen/go-mail)](https://goreportcard.com/report/github.com/wneessen/go-mail) [![Build Status](https://api.cirrus-ci.com/github/wneessen/go-mail.svg)](https://cirrus-ci.com/github/wneessen/go-mail) [![Mentioned in Awesome Go](https://awesome.re/mentioned-badge-flat.svg)](https://github.com/avelino/awesome-go) [![#go-mail on Discord](https://img.shields.io/badge/Discord-%23gomail-blue.svg)](https://discord.gg/zSUeBrsFPB) [![REUSE status](https://api.reuse.software/badge/github.com/wneessen/go-mail)](https://api.reuse.software/info/github.com/wneessen/go-mail)
The main idea of this library was to provide a simple interface to sending mails for my [JS-Mailer](https://github.com/wneessen/js-mailer) project. It quickly evolved into a full-fledged mail library. go-mail follows idiomatic Go style and best practice. It's only dependency is the Go Standard Library. It combines a lot of functionality from the standard library to give easy and convenient access to mail and SMTP related tasks. Parts of this library (especially some parts of [msgwriter.go](msgwriter.go)) have been forked/ported from the [go-mail/mail](https://github.com/go-mail/mail) respectively [go-gomail/gomail](https://github.com/go-gomail/gomail) which both seems to not be maintained anymore. ## Features Some of the features of this library: * [X] Only Standard Library dependant * [X] Modern, idiomatic Go * [X] Sane and secure defaults * [X] Explicit SSL/TLS support * [X] Implicit StartTLS support with different policies * [X] Makes use of contexts for a better control flow and timeout/cancelation handling * [X] SMTP Auth support (LOGIN, PLAIN, CRAM-MD) * [X] RFC5322 compliant mail address validation * [X] Support for common mail header field generation (Message-ID, Date, Bulk-Precedence, Priority, etc.) * [X] Reusing the same SMTP connection to send multiple mails * [X] Support for attachments and inline embeds (from file system, `io.Reader` or `embed.FS`) * [X] Support for different encodings * [X] Support sending mails via a local sendmail command * [X] Support for requestng MDNs (RFC 8098) and DSNs (RFC 1891) * [X] Message object satisfies `io.WriteTo` and `io.Reader` interfaces * [X] Support for Go's `html/template` and `text/template` (as message body, alternative part or attachment/emebed) * [X] Output to file support which allows storing mail messages as e. g. `.eml` files to disk to open them in a MUA go-mail works like a programatic email client and provides lots of methods and functionalities you would consider standard in a MUA. ## Support We have a support and general discussion channel on the Gophers Discord server. Find us at: [#go-mail](https://discord.gg/zSUeBrsFPB) ## Examples The package is shipped with GoDoc example code for difference scenarios. Check them out on its [GoDoc page](https://pkg.go.dev/github.com/wneessen/go-mail#pkg-examples) For ease of use, here is a full usage example: ```go package main import ( "fmt" "github.com/wneessen/go-mail" "os" ) func main() { // Create a new mail message m := mail.NewMsg() // To set address header fields like "From", "To", "Cc" or "Bcc" you have different methods // at your hands. Some perform input validation, some ignore invalid addresses. Some perform // the formating for you. // if err := m.FromFormat("Toni Tester", "sender@example.com"); err != nil { fmt.Printf("failed to set FROM address: %s\n", err) os.Exit(1) } if err := m.To(`"Max Mastermind