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)
}
}
Output:
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)
}
Output:
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()
}
}
Output:
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)
}
}
Output:
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 ¶
- func Send(s Sender, msg ...*Message) error
- type Dialer
- type Encoding
- type FileSetting
- type Message
- func (m *Message) AddAlternative(contentType, body string)
- func (m *Message) AddAlternativeWriter(contentType string, f func(io.Writer) error)
- func (m *Message) Attach(filename string, settings ...FileSetting)
- func (m *Message) Embed(filename string, settings ...FileSetting)
- func (m *Message) FormatAddress(address, name string) string
- func (m *Message) FormatDate(date time.Time) string
- func (m *Message) GetHeader(field string) []string
- func (m *Message) Reset()
- func (m *Message) SetAddressHeader(field, address, name string)
- func (m *Message) SetBody(contentType, body string)
- func (m *Message) SetDateHeader(field string, date time.Time)
- func (m *Message) SetHeader(field string, value ...string)
- func (m *Message) SetHeaders(h map[string][]string)
- func (m *Message) WriteTo(w io.Writer) (int64, error)
- type MessageSetting
- type SendCloser
- type SendFunc
- type Sender
