-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathemail_verification.py
66 lines (52 loc) · 1.77 KB
/
email_verification.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
from dns import resolver
import smtplib
import socket
import re
# FIRST CHECK
def check_syntax(email):
regex = r'^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
if (re.search(regex, email)):
print("Check 1 (Syntax) Passed")
else:
print("Check 1 FAILED! Bad Syntax, Invalid Email!")
exit()
# SECOND CHECK
def check_dns(email, domain):
try:
records = resolver.resolve(domain, 'MX')
mxRecord = str(records[0].exchange)
print("Check 2 (DNS -", mxRecord+") Passed")
return mxRecord
except:
print("Check 2 FAILED! The domain", domain,
"does not exist, Invalid Email!")
exit()
# THIRD CHECK
def check_response(email, domain, mxRecord):
try:
# Get local server hostname
host = socket.gethostname()
# SMTP lib setup (use debug level for full output)
server = smtplib.SMTP()
server.set_debuglevel(0)
# SMTP Conversation
server.connect(mxRecord)
server.helo(host)
server.mail(email)
code, message = server.rcpt(str(email))
server.quit()
# Assume 250 as Success
if code == 250:
print("Check 3 (SMTP response) Passed")
print(email, "is a VALID email address!")
else:
print("Check 3 FAILED! The user", email.split(
"@")[0], "does not exist, Invalid Email!")
except socket.error as socketerror:
print("Check 3 HALTED! The domain", domain,
", either does not have an SMTP or have restricted access through external scripts")
email = input("Enter your Email id :")
domain = email.split("@")[-1]
check_syntax(email) # CHECK1
mxRecord = check_dns(email, domain) # CHECK2
check_response(email, domain, mxRecord) # CHECK3