All Projects → ainsleyclark → go-mail

ainsleyclark / go-mail

Licence: MIT license
📧 A cross platform mail driver for GoLang. Featuring Mailgun, Postal, Postmark, SendGrid, SparkPost & SMTP.

Programming Languages

go
31211 projects - #10 most used programming language
shell
77523 projects
Makefile
30231 projects

Projects that are alternatives of or similar to go-mail

wemail
Send Affordable Bulk Email Campaign Through WordPress
Stars: ✭ 19 (-88.76%)
Mutual labels:  mailgun, smtp, postmark, sendgrid, sparkpost
SlmMail
Send mail from Laminas or Mezzio using external mail services.
Stars: ✭ 107 (-36.69%)
Mutual labels:  mail, mailgun, postmark, sendgrid, sparkpost
mailcoach-support
Questions and support for Mailcoach
Stars: ✭ 32 (-81.07%)
Mutual labels:  mailgun, smtp, sendgrid
Magento2 Gmail Smtp App
Configure Magento 2 to send email using Google App, Gmail, Amazon Simple Email Service (SES), Microsoft Office365 and many other SMTP (Simple Mail Transfer Protocol) servers
Stars: ✭ 281 (+66.27%)
Mutual labels:  mailgun, smtp, sendgrid
Slmmail
Send mail from Laminas or Mezzio using external mail services.
Stars: ✭ 106 (-37.28%)
Mutual labels:  mail, mailgun, sendgrid
Postal
✉️ A fully featured open source mail delivery platform for incoming & outgoing e-mail
Stars: ✭ 11,148 (+6496.45%)
Mutual labels:  mail, smtp, postal
Mailcore
Emailing wrapper for Vapor 3 apps
Stars: ✭ 77 (-54.44%)
Mutual labels:  mailgun, smtp, sendgrid
Airform
Functional HTML forms for Front-End Developers.
Stars: ✭ 307 (+81.66%)
Mutual labels:  mailgun, smtp, sendgrid
Mailer
A light-weight, modular, message representation and mail delivery framework for Python.
Stars: ✭ 225 (+33.14%)
Mutual labels:  mailgun, smtp, sendgrid
MailHookBundle
A bundle to catch API webhook from different mail service
Stars: ✭ 36 (-78.7%)
Mutual labels:  mailgun, sendgrid, sparkpost
postal
✉️ A fully featured open source mail delivery platform for incoming & outgoing e-mail
Stars: ✭ 12,134 (+7079.88%)
Mutual labels:  mail, smtp, postal
Fluentemail
All in one email sender for .NET. Supports popular senders (SendGrid, MailGun, etc) and Razor templates.
Stars: ✭ 1,888 (+1017.16%)
Mutual labels:  mailgun, smtp, sendgrid
go-mail
📨 Simple email interface across multiple service providers (ses, postmark, mandrill, smtp)
Stars: ✭ 39 (-76.92%)
Mutual labels:  mail, smtp, postmark
Laravel Mailbox
Catch incoming emails in your Laravel application
Stars: ✭ 783 (+363.31%)
Mutual labels:  mail, mailgun, sendgrid
Free Email Forwarding
The best free email forwarding for custom domains. Visit our website to get started (SMTP server)
Stars: ✭ 2,024 (+1097.63%)
Mutual labels:  mail, smtp
Docker Postfix
Simple SMTP server / postfix null relay host for your Docker and Kubernetes containers. Based on Alpine Linux.
Stars: ✭ 163 (-3.55%)
Mutual labels:  mail, smtp
Ptorx
📩🛡 Email privacy. Anonymously send and receive with alias forwarding.
Stars: ✭ 187 (+10.65%)
Mutual labels:  mail, smtp
Mailinabox
Mail-in-a-Box helps individuals take back control of their email by defining a one-click, easy-to-deploy SMTP+everything else server: a mail server in a box.
Stars: ✭ 10,649 (+6201.18%)
Mutual labels:  mail, smtp
Neomutt
✉️ Teaching an Old Dog New Tricks -- IRC: #neomutt on irc.libera.chat
Stars: ✭ 2,343 (+1286.39%)
Mutual labels:  mail, smtp
Maddy
✉️ Composable all-in-one mail server.
Stars: ✭ 2,800 (+1556.8%)
Mutual labels:  mail, smtp
Go Mail Logo

made-with-Go Go Report Card Test codecov go.dev reference Twitter Handle

📧 Go Mail

A cross-platform mail driver for GoLang. Featuring Mailgun, Postal, Postmark, SendGrid, SparkPost & SMTP.

Overview

  • Multiple mail drivers for your needs or even create your own custom Mailer.
  • Direct dependency free, all requests are made with the standard lib http.Client.
  • Send attachments with two struct fields, it's extremely simple.
  • Send CC & BCC messages.
  • Extremely lightweight.

Supported API's

Introduction

Go Mail aims to unify multiple popular mail APIs into a singular, easy to use interface. Email sending is seriously simple and great for allowing the developer or end user to choose what platform they use.

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

mailer, err := drivers.NewSparkPost(cfg)
if err != nil {
	log.Fatalln(err)
}

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

result, err := mailer.Send(tx)
if err != nil {
	log.Fatalln(err)
}

fmt.Printf("%+v\n", result)

Installation

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

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:

You can create a new driver by calling the drivers package and passing in a configuration type which is required to create a new mailer. 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 below.

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

mailer, err := drivers.NewSparkpost(cfg)
if err != nil {
	log.Fatalln(err)
}

Sending Data:

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

tx := &mail.Transmission{
	Recipients: []string{"[email protected]"},
	CC:         []string{"[email protected]"},
	BCC:        []string{"[email protected]"},
	Subject:    "My email",
	HTML:       "<h1>Hello from Go Mail!</h1>",
	PlainText:  "Hello from Go Mail!",
	Headers: map[string]string{
		"X-Go-Mail": "Test",
	},
}

result, err := mailer.Send(tx)
if err != nil {
	log.Fatalln(err)
}

fmt.Printf("%+v\n", result)

Response:

The mail response is used for debugging and inspecting results of the mailer. Below is the Response type.

// Response represents the data passed back from a successful transmission.
type Response struct {
	StatusCode int         // e.g. 200
	Body       []byte      // e.g. {"result: success"}
	Headers    http.Header // e.g. map[X-Ratelimit-Limit:[600]]
	ID         string      // e.g "100"
	Message    string      // e.g "Email sent successfully"
}

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 {
	log.Fatalln(err)
}

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

Examples

Mailgun

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

mailer, err := drivers.NewMailgun(cfg)
if err != nil {
	log.Fatalln(err)
}

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

result, err := mailer.Send(tx)
if err != nil {
	log.Fatalln(err)
}

fmt.Printf("%+v\n", result)

Postal

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

mailer, err := drivers.NewPostal(cfg)
if err != nil {
	log.Fatalln(err)
}

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

result, err := mailer.Send(tx)
if err != nil {
	log.Fatalln(err)
}

fmt.Printf("%+v\n", result)

Postmark

cfg := mail.Config{
	APIKey:      "my-key",
	FromAddress: "[email protected]",
	FromName:    "Gopher",
}

mailer, err := drivers.NewPostmark(cfg)
if err != nil {
	log.Fatalln(err)
}

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

result, err := mailer.Send(tx)
if err != nil {
	log.Fatalln(err)
}

fmt.Printf("%+v\n", result)

SendGrid

cfg := mail.Config{
	APIKey:      "my-key",
	FromAddress: "[email protected]",
	FromName:    "Gopher",
}

mailer, err := drivers.NewSendGrid(cfg)
if err != nil {
	log.Fatalln(err)
}

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

result, err := mailer.Send(tx)
if err != nil {
	log.Fatalln(err)
}

fmt.Printf("%+v\n", result)

SMTP

cfg := mail.Config{
	URL:         "smtp.gmail.com",
	FromAddress: "[email protected]",
	FromName:    "Gopher",
	Password:    "my-password",
	Port:        587,
}

mailer, err := drivers.NewSMTP(cfg)
if err != nil {
	log.Fatalln(err)
}

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

result, err := mailer.Send(tx)
if err != nil {
	log.Fatalln(err)
}

fmt.Printf("%+v\n", result)

SparkPost

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

mailer, err := drivers.NewSparkPost(cfg)
if err != nil {
	log.Fatalln(err)
}

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

result, err := mailer.Send(tx)
if err != nil {
	log.Fatalln(err)
}

fmt.Printf("%+v\n", result)

Writing a Mailable

You have the ability to create your own custom Mailer by implementing the singular method interface shown below.

type Mailer interface {
	// Send accepts a mail.Transmission to send an email through a particular
	// driver/provider. Transmissions will be validated before sending.
	//
	// A mail.Response or an error will be returned. In some circumstances
	// the body and status code will be attached to the response for debugging.
	Send(t *mail.Transmission) (mail.Response, error)
}

Debugging

To debug any errors or issues you are facing with Go Mail, you are able to change the Debug variable in the mail package. This will write the HTTP requests in curl to stdout. Additional information will also be displayed in the errors such as method operations.

mail.Debug = true

Development

Setup

To get set up with Go Mail simply clone the repo and run the following:

go get github.com/vektra/mockery/v2/.../
make setup
make mocks

Env

All secrets are contained within the .env file for testing drivers. To begin with, make a copy of the .env.example file and name it .env. You can the set the environment variables to match your credentials for the mail drivers.

You can set the recipients of emails by modifying the EMAIL variables as show below.

  • EMAIL_TO: Recipients of test emails in a comma delimited list.
  • EMAIL_CC: CC recipients of test emails in a comma delimited list.
  • EMAIL_BCC: BCC recipients of test emails in a comma delimited list.

Testing

To run all driver tests, execute the following command:

make test-driver

To run a specific driver test, prepend the driver flag as show below:

make test-driver driver=sparkpost

The driver flag can be one of the following:

  • mailgun
  • postal
  • postmark
  • sendgrid
  • smtp
  • sparkpost

Contributing

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

Credits

Shout out to the incredible Maria Letta for her excellent Gopher illustrations.

Licence

Code Copyright 2022 Go Mail. Code released under the MIT Licence.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].