Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bd20758

Browse files
Merge pull request avinashkranjan#532 from saaalik/email-validator
Email Validation Script avinashkranjan#297
2 parents 7a107fd + e9d5a08 commit bd20758

File tree

2 files changed

+117
-0
lines changed

2 files changed

+117
-0
lines changed

‎Email-Validator/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<h1 align="center">Email Validator</h1>
2+
A simple program which checks for Email Address Validity in three simple checks
3+
4+
---------------------------------------------------------------------
5+
## How it works
6+
7+
- Syntax check, Checks for basic email address syntax using Regex
8+
9+
- DNS check, Checks for domain validity and retrieving record names
10+
11+
- SMTP check, HELO, MAIL FROM and RCPT TO commands are implemented. In the RCPT, If the server sends back a 250, then that means we are good to send an email (the email address exists), otherwise the server will return a different status code (usually a 550), meaning the email address does not exist on that server.
12+
13+
---------------------------------------------------------------------
14+
## Requirements (Py modules used)
15+
- re (Regex)
16+
- dns
17+
- smtplib
18+
- socket
19+
20+
---------------------------------------------------------------------
21+
## TESTCASES
22+
```
23+
#INPUT
24+
mdsaaalikgmalia.com
25+
#OUTPUT
26+
Check 1 FAILED! Bad Syntax, Invalid Email!
27+
```
28+
```
29+
#INPUT
30+
mdsaaalik@gmalia.com
31+
#OUTPUT
32+
Check 1 (Syntax) Passed
33+
Check 2 FAILED! The domain gmalia.com does not exist, Invalid Email!
34+
```
35+
```
36+
#INPUT
37+
salik_invalid@gmail.com
38+
#OUTPUT
39+
Check 1 (Syntax) Passed
40+
Check 2 (DNS - gmail-smtp-in.l.google.com.) Passed
41+
Check 3 FAILED! The user salik_invalid does not exist, Invalid Email!
42+
```
43+
```
44+
#INPUT
45+
mdsaaalik@gmail.com
46+
#OUTPUT
47+
Check 1 (Syntax) Passed
48+
Check 2 (DNS - alt4.gmail-smtp-in.l.google.com.) Passed
49+
Check 3 (SMTP response) Passed
50+
mdsaaalik@gmail.com is a VALID email address!
51+
```
52+
```
53+
#INPUT
54+
mdsaaalik@yahoo.com
55+
#OUTPUT
56+
Check 1 (Syntax) Passed
57+
Check 2 (DNS - mta5.am0.yahoodns.net.) Passed
58+
Check 3 HALTED! The domain yahoo.com , either does not have an SMTP or have restricted access through external scripts
59+
```
60+
61+
#### By [Md Salik](https://github.com/saaalik)

‎Email-Validator/email_verification.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from dns import resolver
2+
import smtplib
3+
import socket
4+
import re
5+
6+
#FIRST CHECK
7+
def check_syntax(email):
8+
regex = r'^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
9+
if(re.search(regex,email)):
10+
print("Check 1 (Syntax) Passed")
11+
else:
12+
print("Check 1 FAILED! Bad Syntax, Invalid Email!")
13+
exit()
14+
15+
#SECOND CHECK
16+
def check_dns(email,domain):
17+
try:
18+
records = resolver.resolve(domain, 'MX')
19+
mxRecord = str(records[0].exchange)
20+
print("Check 2 (DNS -",mxRecord+") Passed")
21+
return mxRecord
22+
except:
23+
print("Check 2 FAILED! The domain",domain,"does not exist, Invalid Email!")
24+
exit()
25+
26+
#THIRD CHECK
27+
def check_response(email, domain, mxRecord):
28+
try:
29+
# Get local server hostname
30+
host = socket.gethostname()
31+
32+
# SMTP lib setup (use debug level for full output)
33+
server = smtplib.SMTP()
34+
server.set_debuglevel(0)
35+
36+
# SMTP Conversation
37+
server.connect(mxRecord)
38+
server.helo(host)
39+
server.mail(email)
40+
code, message = server.rcpt(str(email))
41+
server.quit()
42+
43+
# Assume 250 as Success
44+
if code == 250:
45+
print("Check 3 (SMTP response) Passed")
46+
print(email,"is a VALID email address!")
47+
else:
48+
print("Check 3 FAILED! The user",email.split("@")[0],"does not exist, Invalid Email!")
49+
except socket.error as socketerror:
50+
print("Check 3 HALTED! The domain",domain,", either does not have an SMTP or have restricted access through external scripts")
51+
52+
email = input("Enter your Email id :")
53+
domain = email.split("@")[-1]
54+
check_syntax(email)#CHECK1
55+
mxRecord = check_dns(email,domain)#CHECK2
56+
check_response(email,domain,mxRecord)#CHECK3

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /