-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdialer.go
165 lines (146 loc) · 3.66 KB
/
dialer.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package mailx
import (
"crypto/tls"
"errors"
"io"
"net"
"net/smtp"
"strconv"
"strings"
"time"
)
// @author valor.
// Dialer is a dialer to an SMTP server.
type Dialer struct {
// Host represents the host of the SMTP server.
Host string
// Port represents the port of the SMTP server.
Port int
// Username is the username to use to authenticate to the SMTP server.
Username string
// Password is the password to use to authenticate to the SMTP server.
Password string
// SSLOnConnect defines whether an SSL connection is used.
// It should be false while SMTP server use the STARTTLS extension.
SSLOnConnect bool
// TSLConfig represents the TLS configuration.
// It is used for the TLS (when the
// STARTTLS extension is used) or SSL connection.
TLSConfig *tls.Config
// Timeout is passed to net.Dialer's Timeout.
Timeout time.Duration
}
func (d *Dialer) addr() string {
return d.Host + ":" + strconv.FormatInt(int64(d.Port), 10)
}
func (d *Dialer) tlsConfig() *tls.Config {
if d.TLSConfig == nil {
return &tls.Config{ServerName: d.Host}
}
return d.TLSConfig
}
func (d *Dialer) smtpAuth(c smtpClient) (smtp.Auth, error) {
if d.Username == "" {
return nil, nil
}
ok, auths := c.Extension("AUTH")
if !ok {
return nil, errors.New("smtp server doesn't support AUTH")
}
if strings.Contains(auths, "CRAM-MD5") {
return smtp.CRAMMD5Auth(d.Username, d.Password), nil
}
if strings.Contains(auths, "PLAIN") {
return smtp.PlainAuth("", d.Username, d.Password, d.Host), nil
}
if strings.Contains(auths, "LOGIN") {
return &loginAuth{
username: d.Username,
password: d.Password,
host: d.Host,
}, nil
}
return nil, errors.New("no authentication mechanism is implemented: " + auths)
}
// Dial dials and authenticates to an SMTP server.
// The returned *Sender should be closed when done using it.
func (d *Dialer) Dial() (*Sender, error) {
var (
conn net.Conn
err error
)
netDialer := &net.Dialer{Timeout: d.Timeout}
if d.SSLOnConnect {
tlsDialer := &tls.Dialer{
NetDialer: netDialer,
Config: d.tlsConfig(),
}
conn, err = tlsDial(tlsDialer, "tcp", d.addr())
} else {
// debug: openssl s_client -starttls smtp -ign_eof -crlf -connect <host>:<port>
conn, err = netDial(netDialer, "tcp", d.addr())
}
if err != nil {
return nil, err
}
return d.dial(conn)
}
func (d *Dialer) dial(conn net.Conn) (*Sender, error) {
c, err := newSmtpClient(conn, d.Host)
if err != nil {
return nil, err
}
if !d.SSLOnConnect {
if ok, _ := c.Extension("STARTTLS"); ok {
if err = c.StartTLS(d.tlsConfig()); err != nil {
c.Close()
return nil, err
}
}
}
auth, err := d.smtpAuth(c)
if err != nil {
c.Close()
return nil, err
}
if auth != nil {
if err = c.Auth(auth); err != nil {
c.Close()
return nil, err
}
}
return &Sender{smtpClient: c, from: d.Username}, nil
}
// DialAndSend opens a connection to the SMTP server,
// sends the given emails and closes the connection.
func (d *Dialer) DialAndSend(m *Message) error {
s, err := d.Dial()
if err != nil {
return err
}
defer s.Close()
return s.Send(m)
}
// Stubbed out for tests.
var (
netDial = func(dialer *net.Dialer, network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
}
tlsDial = func(dialer *tls.Dialer, network, addr string) (net.Conn, error) {
return dialer.Dial(network, addr)
}
newSmtpClient = func(conn net.Conn, host string) (smtpClient, error) {
return smtp.NewClient(conn, host)
}
)
type smtpClient interface {
Hello(string) error
Extension(string) (bool, string)
StartTLS(*tls.Config) error
Auth(smtp.Auth) error
Mail(string) error
Rcpt(string) error
Data() (io.WriteCloser, error)
Quit() error
Close() error
}