-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathemail-verify.py
152 lines (142 loc) · 3.94 KB
/
email-verify.py
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
#!/usr/bin/python
#################################################################
# Email Verifier v2.3 #
# By: Dennis Linuz <[email protected]> #
# Verify if email addresses are valid by checking SMTP server #
#################################################################
import smtplib, sys
#Check if pythondns libraries exist and import them
try:
import dns.resolver
except Exception as exc:
print "Email Verifier requires 'pythondns' libraries"
output(exc)
quit()
#Extract the host from the given email address
def getHost(emailAddress):
try:
return emailAddress.split("@")[1]
except Exception as exc:
print "'" + emailAddress + "' is not a valid email address"
output(exc)
return -1
#Find the mail server hostname from DNS MX records
def resolveMX(emailHost):
global hostItems
hostDict={}
try:
records = dns.resolver.query(emailHost, "MX")
except Exception as exc:
print "Could not extract records from host."
output(exc)
return -1
for r in records:
hostDict[r.exchange] = r.preference
hostItems = hostDict.items()
hostItems.sort()
#Connect to host and check if the email address is valid
def checkEmail(emailAddress):
result = True
smtp = smtplib.SMTP()
for x in hostItems:
try:
host = x[0][:-1]
host = ".".join(host)
connectLog = smtp.connect(host)
heloLog = smtp.helo("google.com")
output(connectLog)
output(heloLog)
except Exception as exc:
errorString = "Could not connect to Server: " + host
errorString += "\nTrying next record (if any)"
output(errorString)
output(exc)
continue
else:
result = False
break
if result:
print "Could not resolve any hosts for: " + emailAddress
return -1
try:
sendLog = smtp.sendmail("[email protected]",emailAddress,"IgnoreMessage")
output(sendLog)
except Exception as exc:
print "Email is not valid."
output(exc)
else:
print "Email: " + emailAddress + " is valid!"
validList.append(emailAddress)
#Print more output if verbose argument is given
def output(string):
if verbose:
print string
#Print the documentation
def printHelp():
print ""
print "Verify if email addresses are valid by checking SMTP server\n"
print "Usage:\n\tpython verify-email.py -e <email> -v\n"
print "Arguments:\n"
print " -h or --help","This help".rjust(28)
print " -v or --verbose","Increases verbosity".rjust(35)
print " -e or --email <email>","Specify one email address to check".rjust(44)
print " -f or --file <file>","Specify a file of emails delimeted by line".rjust(54)
print "\nExample:\n\tpython verify-email.py -e [email protected] -v"
print "\tpython verify-email.py --file email.txt\n"
quit()
verbose=False
suppliedInput = 0
validList = []
if not (len(sys.argv) == 3 or len(sys.argv) == 4):
printHelp()
for i in range(1,len(sys.argv)):
if sys.argv[i] == "-h" or sys.argv[i] == "--help":
printHelp()
elif sys.argv[i] == "-v" or sys.argv[i] == "--verbose":
verbose=True
continue
elif sys.argv[i] == "-e" or sys.argv[i] == "--email":
if suppliedInput == 0:
inputEmail = sys.argv[i+1]
suppliedInput = 1
else:
printHelp()
elif sys.argv[i] == "-f" or sys.argv[i] == "--file":
if suppliedInput == 0:
inputFile = sys.argv[i+1]
suppliedInput = 2
else:
printHelp()
try:
if suppliedInput == 0:
printHelp()
elif suppliedInput == 1:
host = getHost(inputEmail)
if not host == -1:
if not resolveMX(host) == -1:
checkEmail(inputEmail)
elif suppliedInput == 2:
try:
file = open(inputFile,"r")
except Exception as exc:
print "Could not open '" + inputFile + "' for reading"
output(exc)
quit()
else:
for email in file:
email = email.replace("\n","")
print "Checking: " + email
host = getHost(email)
if host == -1:
continue
elif resolveMX(host) == -1:
continue
elif checkEmail(email) == -1:
continue
except KeyboardInterrupt:
print "Keyboard Interrupt Detected."
if validList:
print "\nValid Emails: \n=========="
for item in validList:
print item
print ""