-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (61 loc) · 1.42 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
package emailchecker
import (
"encoding/json"
"net"
"strings"
)
type DomainCheckResult struct {
Domain string `json:"domain"`
MXPresent bool `json:"mx_present"`
SPFPresent bool `json:"spf_present"`
DMARCPresent bool `json:"dmarc_present"`
SPFRecord string `json:"spf_record,omitempty"`
DMARCRecord string `json:"dmarc_record,omitempty"`
}
func CheckDomain(email string) (string, error) {
domain := extractDomain(email)
var result DomainCheckResult
result.Domain = domain
// Check for MX records
mxRecords, err := net.LookupMX(domain)
if err != nil {
return "", err
}
result.MXPresent = len(mxRecords) > 0
// Check for SPF records
txtRecords, err := net.LookupTXT(domain)
if err != nil {
return "", err
}
for _, record := range txtRecords {
if strings.HasPrefix(record, "v=spf1") {
result.SPFPresent = true
result.SPFRecord = record
break
}
}
// Check for DMARC records
dmarcRecords, err := net.LookupTXT("_dmarc." + domain)
if err != nil {
return "", err
}
for _, record := range dmarcRecords {
if strings.HasPrefix(record, "v=DMARC1") {
result.DMARCPresent = true
result.DMARCRecord = record
break
}
}
jsonData, err := json.Marshal(result)
if err != nil {
return "", err
}
return string(jsonData), nil
}
func extractDomain(email string) string {
parts := strings.Split(email, "@")
if len(parts) != 2 {
return "Invalid email format"
}
return parts[1]
}