-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.go
77 lines (62 loc) · 1.51 KB
/
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
package mail
import (
"log"
"net"
"strings"
"github.com/spf13/viper"
)
/*
CheckDomainMX takes a domain name as a string, and returns email information
containing the domain name, whether the domain has MX records, whether the domain
has an SPF record, whether the domain has a DMARC record, the SPF record, and the
DMARC record to a specified config file.
*/
func CheckDomainMX(domain string, fileName string, fileType string, filePath string) {
var HasMX, HasSPF, HasDMARC bool
var SPRRecord, DMARCRecord string
vp := viper.New()
// setup config file
vp.SetConfigName(fileName)
vp.SetConfigType(fileType)
vp.AddConfigPath(filePath)
err := vp.ReadInConfig()
if err != nil {
log.Printf("Error: %v\n", err)
}
mxRecords, err := net.LookupMX(domain)
if err != nil {
log.Printf("Error: %v\n", err)
}
if len(mxRecords) > 0 {
HasMX = true
}
txtRecords, err := net.LookupTXT(domain)
if err != nil {
log.Printf("Error: %v\n", err)
}
for _, record := range txtRecords {
if strings.HasPrefix(record, "v=spf") {
HasSPF = true
SPRRecord = record
break
}
}
DMARCRecords, err := net.LookupTXT("_dmarc." + domain)
if err != nil {
log.Printf("Error: %v\n", err)
}
for _, record := range DMARCRecords {
if strings.HasPrefix(record, "v=DMARC1") {
HasDMARC = true
DMARCRecord = record
break
}
}
vp.Set("domain", domain)
vp.Set("HasMX", HasMX)
vp.Set("HasSPF", HasSPF)
vp.Set("HasDMARC", HasDMARC)
vp.Set("SPRecord", SPRRecord)
vp.Set("DMARCRecord", DMARCRecord)
vp.WriteConfig()
}