gomail

package module
v0.0.0-...-b1e5552 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2015 License: MIT Imports: 15 Imported by: 1

README

Gomail

Build Status Code Coverage Documentation

Introduction

Gomail is a simple and efficient package to send emails. It is well tested and documented.

Gomail can only send emails using an SMTP server. But the API is flexible and it is easy to implement other methods for sending emails using a local Postfix, an API, etc.

It is versioned using gopkg.in so I promise they will never be backward incompatible changes within each version.

It requires Go 1.2 or newer. With Go 1.5, no external dependencies are used.

Features

Gomail supports:

  • Attachments
  • Embedded images
  • HTML and text templates
  • Automatic encoding of special characters
  • SSL and TLS
  • Sending multiple emails with the same SMTP connection

Documentation

https://godoc.org/gopkg.in/gomail.v2

Download

go get gopkg.in/gomail.v2

Examples

See the examples in the documentation.

FAQ

x509: certificate signed by unknown authority

If you get this error it means the certificate used by the SMTP server is not considered valid by the client running Gomail. As a quick workaround you can bypass the verification of the server's certificate chain and host name by using SetTLSConfig:

d := gomail.NewPlainDialer("smtp.example.com", "user", "123456", 587)
d.TLSConfig = &tls.Config{InsecureSkipVerify: true}

Note, however, that this is insecure and should not be used in production.

Contribute

Contributions are more than welcome! See CONTRIBUTING.md for more info.

Change log

See CHANGELOG.md.

License

MIT

Contact

You can ask questions on the Gomail thread in the Go mailing-list.

Support

If you want to support the development of Gomail, I gladly accept donations.

I will give 100% of the money I receive to Enfants, Espoir Du Monde. EEDM is a French NGO which helps children in Bangladesh, Cameroun, Haiti, India and Madagascar.

All its members are volunteers so its operating costs are only 1.9%. So your money will directly help children of these countries.

As an added bonus, your donations will also tip me by lowering my taxes 😄

I will send an email with the receipt of the donation to EEDM annually to all donors.

Donate

Documentation

Overview

Package gomail provides a simple interface to compose emails and to mail them efficiently.

More info on Github: https://github.com/go-gomail/gomail

Example
package main

import (
	"gopkg.in/gomail.v2"
)

func main() {
	m := gomail.NewMessage()
	m.SetHeader("From", "alex@example.com")
	m.SetHeader("To", "bob@example.com", "cora@example.com")
	m.SetAddressHeader("Cc", "dan@example.com", "Dan")
	m.SetHeader("Subject", "Hello!")
	m.SetBody("text/html", "Hello <b>Bob</b> and <i>Cora</i>!")
	m.Attach("/home/Alex/lolcat.jpg")

	d := gomail.NewPlainDialer("smtp.example.com", 587, "user", "123456")

	// Send the email to Bob, Cora and Dan.
	if err := d.DialAndSend(m); err != nil {
		panic(err)
	}
}
Example (Daemon)

A daemon that listens to a channel and sends all incoming messages.

package main

import (
	"log"
	"time"

	"gopkg.in/gomail.v2"
)

func main() {
	ch := make(chan *gomail.Message)

	go func() {
		d := gomail.NewPlainDialer("smtp.example.com", 587, "user", "123456")

		var s gomail.SendCloser
		var err error
		open := false
		for {
			select {
			case m, ok := <-ch:
				if !ok {
					return
				}
				if !open {
					if s, err = d.Dial(); err != nil {
						panic(err)
					}
					open = true
				}
				if err := gomail.Send(s, m); err != nil {
					log.Print(err)
				}
			// Close the connection to the SMTP server if no email was sent in
			// the last 30 seconds.
			case <-time.After(30 * time.Second):
				if open {
					if err := s.Close(); err != nil {
						panic(err)
					}
					open = false
				}
			}
		}
	}()

	// Use the channel in your program to send emails.

	// Close the channel to stop the mail daemon.
	close(ch)
}
Example (Newsletter)

Efficiently send a customized newsletter to a list of recipients.

package main

import (
	"fmt"
	"log"

	"gopkg.in/gomail.v2"
)

func main() {
	// The list of recipients.
	var list []struct {
		Name    string
		Address string
	}

	d := gomail.NewPlainDialer("smtp.example.com", 587, "user", "123456")
	s, err := d.Dial()
	if err != nil {
		panic(err)
	}

	m := gomail.NewMessage()
	for _, r := range list {
		m.SetHeader("From", "no-reply@example.com")
		m.SetAddressHeader("To", r.Address, r.Name)
		m.SetHeader("Subject", "Newsletter #1")
		m.SetBody("text/html", fmt.Sprintf("Hello %s!", r.Name))

		if err := gomail.Send(s, m); err != nil {
			log.Printf("Could not send email to %q: %v", r.Address, err)
		}
		m.Reset()
	}
}
Example (NoAuth)

Send an email using a local SMTP server.

package main

import (
	"gopkg.in/gomail.v2"
)

func main() {
	m := gomail.NewMessage()
	m.SetHeader("From", "from@example.com")
	m.SetHeader("To", "to@example.com")
	m.SetHeader("Subject", "Hello!")
	m.SetBody("text/plain", "Hello!")

	d := gomail.Dialer{Host: "localhost", Port: 587}
	if err := d.DialAndSend(m); err != nil {
		panic(err)
	}
}
Example (NoSMTP)

Send an email using an API or postfix.

package main

import (
	"fmt"
	"io"

	"gopkg.in/gomail.v2"
)

func main() {
	m := gomail.NewMessage()
	m.SetHeader("From", "from@example.com")
	m.SetHeader("To", "to@example.com")
	m.SetHeader("Subject", "Hello!")
	m.SetBody("text/plain", "Hello!")

	s := gomail.SendFunc(func(from string, to []string, msg io.WriterTo) error {
		// Implements you email-sending function, for example by calling
		// an API, or running postfix, etc.
		fmt.Println("From:", from)
		fmt.Println("To:", to)
		return nil
	})

	if err := gomail.Send(s, m); err != nil {
		panic(err)
	}
}
Output:
From: from@example.com
To: [to@example.com]

Index

Examples