-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
96 lines (77 loc) · 1.89 KB
/
main.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
package emailverifier
import (
"fmt"
"log"
"net"
"net/smtp"
"regexp"
"strings"
)
func ValidateEmailFormat(email string) bool {
re := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`)
return re.MatchString(email)
}
func checkAndGetMXRecords(domain string) (bool, []*net.MX) {
mxRecords, err := net.LookupMX(domain)
if err != nil {
log.Printf("Error while looking up MX records: %v\n", err)
}
if len(mxRecords) > 0 {
return true, mxRecords
}
return false, mxRecords
}
func checkSPFRecords(domain string) bool {
txtRecords, err := net.LookupTXT(domain)
if err != nil {
log.Printf("Error while looking up SPF records failed: %v\n", err)
}
for _, record := range txtRecords {
if strings.HasPrefix(record, "v=spf1") {
return true
}
}
return false
}
func checkDMARCRecord(domain string) bool {
dmarcRecords, err := net.LookupTXT("_dmarc." + domain)
if err != nil {
log.Printf("Error while checking DMARC records: %v\n", err)
}
for _, record := range dmarcRecords {
if strings.HasPrefix(record, "v=DMARC1") {
return true
}
}
return false
}
func verifyEmail(email string, mx *net.MX) bool {
client, err := smtp.Dial(mx.Host + ":25")
if err != nil {
log.Printf("Error while connecting to %s: %v", mx.Host, err)
return false
}
defer client.Close()
client.Mail("[email protected]")
if err := client.Rcpt(email); err != nil {
log.Printf("Error. Failed to verify email %s: %v", email, err)
return false
}
return true
}
func CheckEmail(email string) bool {
if !ValidateEmailFormat(email) {
fmt.Printf("Invalid email format")
return false
}
domain := strings.Split(email, "@")[1]
hasMX, mxRecords := checkAndGetMXRecords(domain)
isEmailValid := false
for _, mx := range mxRecords {
if verifyEmail(email, mx) {
isEmailValid = true
break
}
}
return hasMX && checkSPFRecords(domain) && checkDMARCRecord(domain) && isEmailValid
}