-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemails.go
198 lines (177 loc) · 4.32 KB
/
emails.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package validator
import (
"fmt"
"net"
"net/mail"
"strings"
"time"
"blitiri.com.ar/go/spf"
"github.com/etkecc/go-trysmtp"
"golang.org/x/net/context"
)
var (
ErrSpamlist = fmt.Errorf("spamlist")
ErrNoMX = fmt.Errorf("no MX")
ErrSMTP = fmt.Errorf("SMTP")
ErrSPF = fmt.Errorf("SPF")
)
// Email checks if email is valid
// returnPath and optionalSenderIP are optional fields
func (v *V) Email(email, returnPath string, optionalSenderIP ...net.IP) bool {
// edge case: email may be optional
if email == "" {
return !v.cfg.Email.Enforce
}
address, err := mail.ParseAddress(email)
if err != nil {
v.cfg.Log("email %s invalid, reason: %v", email, err)
return false
}
if returnPath != "" {
rpAddress, err := mail.ParseAddress(returnPath)
if err != nil {
v.cfg.Log("return path %s invalid, reason: %v", returnPath, err)
}
if rpAddress != nil {
returnPath = rpAddress.Address
}
}
email = address.Address
return v.emailChecks(email, returnPath, optionalSenderIP...)
}
func (v *V) emailChecks(email, returnPath string, optionalSenderIP ...net.IP) bool {
maxChecks := 4
var senderIP net.IP
if len(optionalSenderIP) > 0 {
senderIP = optionalSenderIP[0]
}
errchan := make(chan error, maxChecks)
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
go v.emailSpamlist(ctx, email, errchan)
go v.emailNoMX(ctx, email, errchan)
go v.emailNoSPF(ctx, email, returnPath, senderIP, errchan)
go v.emailNoSMTP(ctx, email, errchan)
var checks int
for {
checks++
err := <-errchan
if err != nil {
v.cfg.Log("email %q is invalid, reason: %v", email, err)
return false
}
if checks >= maxChecks {
return true
}
}
}
func (v *V) emailSpamlist(ctx context.Context, email string, errchan chan error) {
select {
case <-ctx.Done():
return
default:
emailb := []byte(email)
for _, spamregex := range v.cfg.Email.spamlist {
if spamregex.Match(emailb) {
errchan <- ErrSpamlist
return
}
}
errchan <- nil
}
}
func (v *V) emailNoMX(ctx context.Context, email string, errchan chan error) {
select {
case <-ctx.Done():
return
default:
if !v.cfg.Email.MX {
errchan <- nil
return
}
at := strings.LastIndex(email, "@")
domain := email[at+1:]
if !v.MX(domain) {
v.cfg.Log("email %s domain %s invalid, reason: no MX", email, domain)
errchan <- ErrNoMX
return
}
errchan <- nil
}
}
func (v *V) emailNoSMTP(ctx context.Context, email string, errchan chan error) {
select {
case <-ctx.Done():
return
default:
if !v.cfg.Email.SMTP {
errchan <- nil
return
}
trysmtp.SMTPAddrs = []string{":25"}
client, err := trysmtp.Connect(v.cfg.Email.From, email)
if err != nil {
if strings.HasPrefix(err.Error(), "45") {
v.cfg.Log("email %s may be invalid, reason: SMTP check (%v)", email, err)
errchan <- nil
return
}
v.cfg.Log("email %s invalid, reason: SMTP check (%v)", email, err)
errchan <- ErrSMTP
return
}
client.Close()
errchan <- nil
}
}
// shouldValidateReturnPath checks if returnPath should be validated in SPF check
func (v *V) shouldValidateReturnPath(email, returnPath string) bool {
if returnPath == "" {
return false
}
if email == returnPath {
return false
}
var emailDomain, returnPathDomain string
emailParts := strings.Split(email, "@")
if len(emailParts) < 2 {
return false
}
emailDomain = v.GetBase(emailParts[1])
if returnPath != "" {
returnPathParts := strings.Split(returnPath, "@")
if len(returnPathParts) < 2 {
return false
}
returnPathDomain = v.GetBase(returnPathParts[1])
}
return emailDomain == returnPathDomain
}
func (v *V) emailNoSPF(ctx context.Context, email, returnPath string, senderIP net.IP, errchan chan error) {
select {
case <-ctx.Done():
return
default:
if !v.cfg.Email.SPF {
errchan <- nil
return
}
opts := []spf.Option{
spf.WithContext(ctx),
spf.WithTraceFunc(v.cfg.Log),
}
resultEmail, _ := spf.CheckHostWithSender(senderIP, "", email, opts...) //nolint:errcheck // not a error
if resultEmail == spf.Fail {
if v.shouldValidateReturnPath(email, returnPath) {
resultReturnPath, _ := spf.CheckHostWithSender(senderIP, "", returnPath, opts...) //nolint:errcheck // not a error
if resultReturnPath == spf.Fail {
errchan <- ErrSPF
return
}
}
errchan <- ErrSPF
return
}
errchan <- nil
}
}