2014-10-22 23:47:24 +08:00
|
|
|
package gomail
|
|
|
|
|
|
|
|
import (
|
2015-06-30 17:55:03 +08:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2014-10-22 23:47:24 +08:00
|
|
|
"io"
|
2015-06-30 17:55:03 +08:00
|
|
|
"net/mail"
|
2014-10-22 23:47:24 +08:00
|
|
|
)
|
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
// Sender is the interface that wraps the Send method.
|
|
|
|
//
|
|
|
|
// Send sends an email to the given addresses.
|
|
|
|
type Sender interface {
|
2015-07-03 05:08:18 +08:00
|
|
|
Send(from string, to []string, msg io.WriterTo) error
|
2015-06-30 17:55:03 +08:00
|
|
|
}
|
2014-10-22 23:47:24 +08:00
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
// SendCloser is the interface that groups the Send and Close methods.
|
|
|
|
type SendCloser interface {
|
|
|
|
Sender
|
|
|
|
Close() error
|
|
|
|
}
|
2014-10-22 23:47:24 +08:00
|
|
|
|
2016-04-12 05:29:32 +08:00
|
|
|
// A SendFunc is a function that sends emails to the given addresses.
|
2015-08-24 00:55:53 +08:00
|
|
|
//
|
2015-06-30 17:55:03 +08:00
|
|
|
// The SendFunc type is an adapter to allow the use of ordinary functions as
|
|
|
|
// email senders. If f is a function with the appropriate signature, SendFunc(f)
|
|
|
|
// is a Sender object that calls f.
|
2015-07-03 05:08:18 +08:00
|
|
|
type SendFunc func(from string, to []string, msg io.WriterTo) error
|
2014-10-22 23:47:24 +08:00
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
// Send calls f(from, to, msg).
|
2015-07-03 05:08:18 +08:00
|
|
|
func (f SendFunc) Send(from string, to []string, msg io.WriterTo) error {
|
2015-06-30 17:55:03 +08:00
|
|
|
return f(from, to, msg)
|
|
|
|
}
|
2014-10-22 23:47:24 +08:00
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
// Send sends emails using the given Sender.
|
|
|
|
func Send(s Sender, msg ...*Message) error {
|
|
|
|
for i, m := range msg {
|
|
|
|
if err := send(s, m); err != nil {
|
|
|
|
return fmt.Errorf("gomail: could not send email %d: %v", i+1, err)
|
2014-10-22 23:47:24 +08:00
|
|
|
}
|
|
|
|
}
|
2015-06-30 17:55:03 +08:00
|
|
|
|
|
|
|
return nil
|
2014-10-22 23:47:24 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
func send(s Sender, m *Message) error {
|
|
|
|
from, err := m.getFrom()
|
2015-06-30 17:55:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-10-22 23:55:36 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
to, err := m.getRecipients()
|
2014-10-22 23:55:36 +08:00
|
|
|
if err != nil {
|
2015-06-30 17:55:03 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
if err := s.Send(from, to, m); err != nil {
|
2015-06-30 17:55:03 +08:00
|
|
|
return err
|
2014-10-22 23:55:36 +08:00
|
|
|
}
|
|
|
|
|
2015-06-30 17:55:03 +08:00
|
|
|
return nil
|
2014-10-22 23:55:36 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) getFrom() (string, error) {
|
|
|
|
from := m.header["Sender"]
|
2015-07-03 05:08:18 +08:00
|
|
|
if len(from) == 0 {
|
2015-07-15 05:34:21 +08:00
|
|
|
from = m.header["From"]
|
2015-07-03 05:08:18 +08:00
|
|
|
if len(from) == 0 {
|
|
|
|
return "", errors.New(`gomail: invalid message, "From" field is absent`)
|
2015-06-30 17:55:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 05:08:18 +08:00
|
|
|
return parseAddress(from[0])
|
2014-10-22 23:55:36 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 05:34:21 +08:00
|
|
|
func (m *Message) getRecipients() ([]string, error) {
|
2015-07-26 00:52:52 +08:00
|
|
|
n := 0
|
|
|
|
for _, field := range []string{"To", "Cc", "Bcc"} {
|
|
|
|
if addresses, ok := m.header[field]; ok {
|
|
|
|
n += len(addresses)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
list := make([]string, 0, n)
|
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
for _, field := range []string{"To", "Cc", "Bcc"} {
|
2015-07-15 05:34:21 +08:00
|
|
|
if addresses, ok := m.header[field]; ok {
|
2015-07-03 03:10:45 +08:00
|
|
|
for _, a := range addresses {
|
|
|
|
addr, err := parseAddress(a)
|
2015-06-30 17:55:03 +08:00
|
|
|
if err != nil {
|
2015-07-03 03:10:45 +08:00
|
|
|
return nil, err
|
2015-06-30 17:55:03 +08:00
|
|
|
}
|
2015-07-15 05:25:44 +08:00
|
|
|
list = addAddress(list, addr)
|
2015-06-30 17:55:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
return list, nil
|
2014-10-22 23:55:36 +08:00
|
|
|
}
|
|
|
|
|
2015-07-15 05:25:44 +08:00
|
|
|
func addAddress(list []string, addr string) []string {
|
2015-06-30 17:55:03 +08:00
|
|
|
for _, a := range list {
|
|
|
|
if addr == a {
|
2015-07-03 03:10:45 +08:00
|
|
|
return list
|
2015-06-30 17:55:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-03 03:10:45 +08:00
|
|
|
return append(list, addr)
|
2015-06-30 17:55:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseAddress(field string) (string, error) {
|
2016-04-12 05:18:11 +08:00
|
|
|
addr, err := mail.ParseAddress(field)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("gomail: invalid address %q: %v", field, err)
|
2015-06-30 17:55:03 +08:00
|
|
|
}
|
2016-04-12 05:18:11 +08:00
|
|
|
return addr.Address, nil
|
2014-10-22 23:47:24 +08:00
|
|
|
}
|