-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsender.go
110 lines (103 loc) · 2.51 KB
/
sender.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package email
import (
"errors"
"net/smtp"
"strconv"
"sync"
)
// Sender represents the SMTP credentials along with the (optional) Address of a sender.
type Sender struct {
host string
port int
username string
password string
address *Address
}
var (
defaultSender *Sender
defaultSenderMutex sync.RWMutex
)
// NewSender creates a new Sender from the provided information.
//
// The `host` may include a port number, which defaults to 25. That is, "example.com"
// and "example.com:25" are equivalent.
// The `addr` parameters are optional and may be either an email address or a name followed by an
// email address.
func NewSender(host, user, pass string, addr ...string) (*Sender, error) {
port := 0
for i, l := 0, len(host); i < l; i++ {
if host[i] == ':' {
for _, digit := range host[i+1:] {
if digit < '0' || digit > '9' {
return nil, errors.New("NewSender: invalid port number: " + host)
}
port = port*10 + int(digit-'0')
}
host = host[:i]
break
}
}
if port == 0 {
port = 25
}
if user == "" {
return nil, errors.New("NewSender: empty username: " + user)
}
if pass == "" {
return nil, errors.New("NewSender: empty password: " + pass)
}
var (
address *Address
err error
)
switch len(addr) {
case 2:
address, err = NewAddress(addr[0], addr[1])
case 1:
address, err = NewAddress("", addr[0])
}
if err != nil {
return nil, errors.New("NewSender: " + err.Error())
}
return &Sender{host, port, user, pass, address}, nil
}
// SetDefault sets the receiver as the default sender.
func (s *Sender) SetDefault() *Sender {
defaultSenderMutex.Lock()
defaultSender = s
defaultSenderMutex.Unlock()
return s
}
// Send composes the provided message using the `data`, and sends it.
func (s *Sender) Send(msg *Message, data interface{}) error {
if msg == nil {
return errors.New("Sender.Send: no message to send")
}
body := msg.setSender(s).Compose(data)
if msg.HasErrors() {
return errors.New("Sender.Send: failed to compose message")
}
go smtp.SendMail(
s.host+":"+strconv.Itoa(s.port),
smtp.PlainAuth(
"",
s.username,
s.password,
s.host,
),
msg.FromAddr(),
msg.RecipientAddrs(),
body,
)
return nil
}
// Send composes the provided message using the `data`, and sends it using the default Sender.
func Send(msg *Message, data interface{}) error {
defaultSenderMutex.RLock()
defer defaultSenderMutex.RUnlock()
sender := defaultSender
if sender == nil {
return errors.New("Send: no default sender")
}
return sender.Send(msg, data)
}