| 1 | [](https://travis-ci.org/domodwyer/mailyak) |
| 3 | [](https://godoc.org/github.com/domodwyer/mailyak) |
| 4 | |
| 5 | <p align="center"> |
| 6 | <img src="https://s3-eu-west-1.amazonaws.com/iab-assets/mailyak-header.png" /> |
| 7 | </p> |
| 8 | <p align="center"> |
| 9 | <em>An elegant MIME mail library with support for attachments</em> |
| 10 | </p> |
| 11 | <br /><br /><br /> |
| 12 | A simple, easy to use email library for Go (golang). |
| 13 | |
| 14 | - Full attachment support (attach anything that implements `io.Reader`) |
| 15 | - Send to multiple addresses at the same time, including BCC addresses. |
| 16 | - Supports composing multi-part messages (HTML and plain text emails for older |
| 17 | clients) |
| 18 | - Write templates directly to the email body (implements `io.Writer` for |
| 19 | convenience) |
| 20 | - Production ready - several million emails sent in a production environment |
| 21 | - SMTP over TLS support, with automatic STARTTLS upgrades for plaintext |
| 22 | connections |
| 23 | |
| 24 | # Installation |
| 25 | |
| 26 | If you're using `go mod`: |
| 27 | |
| 28 | ```bash |
| 29 | go get -v github.com/domodwyer/mailyak/v3 |
| 30 | ``` |
| 31 | |
| 32 | Or with `GOPATH`: |
| 33 | |
| 34 | ```bash |
| 35 | go get -v github.com/domodwyer/mailyak |
| 36 | ``` |
| 37 | |
| 38 | # Usage |
| 39 | |
| 40 | ```Go |
| 41 | // Create a new email - specify the SMTP host:port and auth (if needed) |
| 42 | mail := mailyak.New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) |
| 43 | |
| 44 | mail.To("[email protected]") |
| 45 | mail.From("[email protected]") |
| 46 | mail.FromName("Bananas for Friends") |
| 47 | |
| 48 | mail.Subject("Business proposition") |
| 49 | |
| 50 | // mail.HTML() and mail.Plain() implement io.Writer, so you can do handy things like |
| 51 | // parse a template directly into the email body |
| 52 | if err := t.ExecuteTemplate(mail.HTML(), "htmlEmail", data); err != nil { |
| 53 | panic(" 💣 ") |
| 54 | } |
| 55 | |
| 56 | // Or set the body using a string setter |
| 57 | mail.Plain().Set("Get a real email client") |
| 58 | |
| 59 | // And you're done! |
| 60 | if err := mail.Send(); err != nil { |
| 61 | panic(" 💣 ") |
| 62 | } |
| 63 | ``` |
| 64 | |
| 65 | To send an attachment: |
| 66 | ```Go |
| 67 | mail := mailyak.New("mail.host.com:25", smtp.PlainAuth("", "user", "pass", "mail.host.com")) |
| 68 | |
| 69 | mail.To("[email protected]") |
| 70 | mail.From("[email protected]") |
| 71 | mail.Subject("I am a teapot") |
| 72 | mail.HTML().Set("Don't panic") |
| 73 | |
| 74 | // input can be a bytes.Buffer, os.File, os.Stdin, etc. |
| 75 | // call multiple times to attach multiple files |
| 76 | mail.Attach("filename.txt", &input) |
| 77 | |
| 78 | if err := mail.Send(); err != nil { |
| 79 | panic(" 💣 ") |
| 80 | } |
| 81 | ``` |
| 82 | |
| 83 | # Notes |
| 84 | |
| 85 | - Why "MailYak"? Because "MailyMcMailFace" is annoyingly long to type. |
| 86 | - You can use a single instance of mailyak to send multiple emails after |
| 87 | changing the to/body/whatever fields, avoiding unnecessary allocation/GC |
| 88 | pressure. |
| 89 | - Attachments are read when you call `Send()` to prevent holding onto multiple |
| 90 | copies of the attachment in memory (source and email) - this means changing |
| 91 | the attachment data between calling `Attach()` and `Send()` will change what's |
| 92 | emailed out! |
| 93 | - For your own sanity you should vendor this, and any other libraries when going |
| 94 | into production. |
| 95 | |
| 96 | |
| 97 | ### Maintenance Status |
| 98 | |
| 99 | This library is fully maintained. |
| 100 | |
| 101 | The (relatively) small API/scope and many years spent maturing means it doesn't |
| 102 | receive frequent code changes any more. Bug fixes are definitely accepted (and |
| 103 | appreciated!), and you can consider this a stable and maintained library. |