/
send_mail.go
executable file
·162 lines (133 loc) · 3.64 KB
/
send_mail.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
package evsmtp
import (
"context"
"crypto/tls"
"errors"
"io"
"net/smtp"
"sync"
"github.com/prodadidb/go-email-validator/pkg/ev/evsmtp/smtpclient"
)
// SendMailStage is stage type of SendMail
type SendMailStage uint8
// SafeSendMailStage is thread safe SendMailStage
type SafeSendMailStage struct {
SendMailStage
mu sync.RWMutex
}
// Set sets stage
func (s *SafeSendMailStage) Set(val SendMailStage) {
s.mu.Lock()
defer s.mu.Unlock()
s.SendMailStage = val
}
// Get returns stage
func (s *SafeSendMailStage) Get() SendMailStage {
s.mu.RLock()
defer s.mu.RUnlock()
return s.SendMailStage
}
// Constants of stages
const (
ClientStage SendMailStage = iota + 1
HelloStage
AuthStage
MailStage
RCPTsStage
QuitStage
CloseStage
)
// SendMail is interface of custom realization as smtp.SendMail
type SendMail interface {
Client() smtpclient.SMTPClient
Hello(helloName string) error
Auth(a smtp.Auth) error
Mail(from string) error
RCPTs(addrs []string) map[string]error
Data() (io.WriteCloser, error)
Write(w io.WriteCloser, msg []byte) error
Quit() error
Close() error
}
var testHookStartTLS func(*tls.Config)
// SendMailDialerFactory is factory for SendMail with dialing
type SendMailDialerFactory func(ctx context.Context, host string, opts Options) (SendMail, error)
// NewSendMailFactory creates SendMailDialerFactory
func NewSendMailFactory(dialFunc DialFunc, tlsConfig *tls.Config) SendMailDialerFactory {
return NewSendMailCustom(dialFunc, tlsConfig, NewSendMail)
}
// SendMailFactory is factory for SendMail
type SendMailFactory func(client smtpclient.SMTPClient, tlsConfig *tls.Config) SendMail
// NewSendMailCustom creates SendMailFactory with dialing and customization calling of SendMailFactory
func NewSendMailCustom(dialFunc DialFunc, tlsConfig *tls.Config, factory SendMailFactory) SendMailDialerFactory {
return func(ctx context.Context, host string, opts Options) (SendMail, error) {
conn, err := dialFunc(ctx, host, opts.Proxy())
if err != nil {
return nil, err
}
return factory(conn, tlsConfig), nil
}
}
// NewSendMail instantiates SendMail
func NewSendMail(client smtpclient.SMTPClient, tlsConfig *tls.Config) SendMail {
return &SendMailStruct{
SMTPClient: client,
TLSConfig: tlsConfig,
}
}
type SendMailStruct struct {
SMTPClient smtpclient.SMTPClient
TLSConfig *tls.Config
}
func (s *SendMailStruct) Client() smtpclient.SMTPClient {
return s.SMTPClient
}
func (s *SendMailStruct) Hello(helloName string) error {
return s.SMTPClient.Hello(helloName)
}
func (s *SendMailStruct) Auth(a smtp.Auth) error {
if ok, _ := s.SMTPClient.Extension("STARTTLS"); ok && s.TLSConfig != nil {
if testHookStartTLS != nil {
testHookStartTLS(s.TLSConfig)
}
if err := s.SMTPClient.StartTLS(s.TLSConfig); err != nil {
return err
}
}
if a != nil {
if ok, _ := s.SMTPClient.Extension("AUTH"); !ok {
return errors.New("smtp_checker: server doesn't support AUTH")
}
if err := s.SMTPClient.Auth(a); err != nil {
return err
}
}
return nil
}
func (s *SendMailStruct) Mail(from string) error {
return s.SMTPClient.Mail(from)
}
func (s *SendMailStruct) RCPTs(addrs []string) map[string]error {
errs := make(map[string]error)
for _, addr := range addrs {
if err := s.SMTPClient.Rcpt(addr); err != nil {
errs[addr] = err
}
}
return errs
}
func (s *SendMailStruct) Data() (io.WriteCloser, error) {
return s.SMTPClient.Data()
}
func (s *SendMailStruct) Write(w io.WriteCloser, msg []byte) error {
if _, err := w.Write(msg); err != nil {
return err
}
return w.Close()
}
func (s *SendMailStruct) Quit() error {
return s.SMTPClient.Quit()
}
func (s *SendMailStruct) Close() error {
return s.SMTPClient.Close()
}