-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
128 lines (107 loc) · 2.35 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
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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"net"
"net/smtp"
"os"
"strings"
"time"
)
var (
sender string
email string
)
/**
* Initialize the command line arguments
*/
func init() {
flag.StringVar(&sender, "sender", "[email protected]", "the sender email address")
flag.StringVar(&email, "email", "[email protected]", "the email address to verify")
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}
flag.Parse()
}
/**
* Main entry point for our program
*/
func main() {
// Make sure we have something to work with
if len(os.Args) == 1 {
flag.Usage()
os.Exit(-1)
}
// Extract the domain from the email addy
domain := extractDomain(email)
// Determine the MX server for the extracted domain
records := getMXRecords(domain)
if len(records) == 0 {
log.Fatalf("No MX records found for %s\n", domain)
}
// Perform the email check
checkEmail(records[0], email)
}
/**
* Method for extracting the domain from an email address
*/
func extractDomain(email string) string {
if index := strings.Index(email, "@"); index == -1 {
log.Fatal("Malformed email address")
}
return email[strings.Index(email, "@")+1:]
}
/**
* Method for fetching the MX record(s)
*/
func getMXRecords(domain string) []string {
var records []string
mxs, _ := net.LookupMX(domain)
for _, mx := range mxs {
host := mx.Host
// Remove trailing dot
if last := len(host) - 1; last >= 0 && host[last] == '.' {
host = host[:last]
}
records = append(records, host)
}
return records
}
/**
* Method to check for a valid email address
*/
func checkEmail(server string, email string) {
var buffer bytes.Buffer
fmt.Fprintf(&buffer, "%s:%d", server, 25)
// Dial the tcp connection (10 second timeout)
conn, err := net.DialTimeout("tcp", buffer.String(), 10*time.Second)
if err != nil {
log.Fatal(err)
}
// Connect to the SMTP server
c, err := smtp.NewClient(conn, server)
if err != nil {
log.Fatal(err)
}
defer c.Quit()
// Set the sender and recipient first
if err := c.Mail(sender); err != nil {
log.Fatal(err)
}
// Test email
log.Printf("Checking email %s\n", email)
status := "VALID"
if err := c.Rcpt(email); err != nil {
status = "INVALID"
}
fmt.Printf("%s|%s\n", email, status)
// Quit gracefully
//log.Printf("Closing gracefully\n")
err = c.Quit()
if err != nil {
log.Fatal(err)
}
}