Awesome Open Source
Awesome Open Source

Gopher

Go Mail

A cross platform mail driver for GoLang.

Travis Software License Coverage Status Go Report Card GoDoc

Introduction

Go Mail aims to unify multiple popular mail API's (SparkPost, MailGun, SendGrid & SMTP) into a singular easy to use interface. Email sending is seriously simple and great for allowing the developer to choose what platform they use.

cfg := mail.Config{
    URL:         "https://api.eu.sparkpost.com",
    APIKey:      "my-key",
    FromAddress: "[email protected]",
    FromName:    "Gopher",
}

driver, err := mail.NewClient(mail.SparkPost, cfg)
if err != nil {
    fmt.Println(err)
    return
}

tx := &mail.Transmission{
    Recipients:  []string{"[email protected]"},
    Subject:     "My email",
    HTML:        "<h1>Hello from go mail!</h1>",
}

result, err := driver.Send(tx)
if err != nil {
    fmt.Println(err)
    return
}

fmt.Println(result)

Installation

go get -u github.com/ainsleyclark/go-mail

Supported API's

Currently, Sparkpost, MailGun and SendGrid is supported, if you want to see more, just submit a feature request or create a new Driver and submit a pull request.

API Dependency Examples
SparkPost github.com/SparkPost/gosparkpost Here
MailGun github.com/mailgun/mailgun-go/v4 Here
SendGrid github.com/sendgrid/sendgrid-go Here
SMTP None - only use in development. Here

Docs

Documentation can be found at the Go Docs, but we have included a kick start guide below to get you started.

Creating a new client:

The driver type is the first argument to be passed to the NewClient function, being one of the following:

  • mail.SparkPost
  • mail.MailGun
  • mail.SendGrid
  • mail.SMTP

A new configuration type is needed to create a new mailer as the second argument, each platform requires its own data, for example, MailGun requires a domain, but SparkPost doesn't. This is based of the requirements for the API. For more details see the examples above.

cfg := mail.Config{
    URL:         "https://api.eu.sparkpost.com",
    APIKey:      "my-key",
    FromAddress: "[email protected]",
    FromName:    "Gopher",
}

driver, err := mail.NewClient(mail.SparkPost, cfg)
if err != nil {
    fmt.Println(err)
    return
}

Sending Data

A transmission is required to transmit to a mailer as shown below. Once send is called, a mail.Result and error will be returned indicating if the transmission was successful.

tx := &mail.Transmission{
    Recipients: []string{"[email protected]"},
    Subject:    "My email",
    HTML:       "<h1>Hello from go mail!</h1>",
    PlainText:  "plain text",
}

result, err := driver.Send(tx)
if err != nil {
    fmt.Println(err)
    return
}

fmt.Println(result)

Adding attachments:

Adding attachments to the transmission is as simple as passing a byte slice and filename, Go Mail takes care of the rest for you.

image, err := ioutil.ReadFile("gopher.jpg")
if err != nil {
    fmt.Println(err)
    return
}

tx := &mail.Transmission{
    Recipients: []string{"[email protected]"},
    Subject:    "My email",
    HTML:       "<h1>Hello from go mail!</h1>",
    PlainText:  "plain text",
    Attachments: mail.Attachments{
        mail.Attachment{
            Filename: "gopher.jpg",
            Bytes:    image,
        },
    },
}

Todo

  • Add CC & BCC
  • Remove external dependencies.

Contributing

We welcome contributors, but please read the contributing document before making a pull request.

Licence

Code Copyright 2021 go mail. Code released under the MIT Licence.


Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Go (196,482
Golang (32,788
Mail (908
Sendgrid (336
Mailgun (186
Related Projects