-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.py
135 lines (109 loc) · 3.49 KB
/
main.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
import re
import smtplib
import dns.resolver
import pandas as pd
from tkinter import Tk
from tkinter.filedialog import askopenfilename, asksaveasfilename
def main():
print("Program: Email Ping")
print("Release: 1.0")
print("Date: 2019-05-31")
print("Author: Brian Neely")
print()
print()
print("This program pings a email web server for a specified email address in a csv.")
print("It assumes that the csv file is encoded in UTF-8 and is comma delimited.")
print("A significant portion of the ping_email function is copied from the work of Scott Brady from his Python-Email-Verification-Script")
print("His work and this work is licensed under the Apache License 2.0")
print()
print()
print("Program Starting")
# Hide Tkinter GUI
Tk().withdraw()
# Find input file
print("Select File in")
file_in = askopenfilename(initialdir="../", title="Select input file",
filetypes=(("Comma Separated Values", "*.csv"), ("all files", "*.*")))
if not file_in:
input("Program Terminated. Press Enter to continue...")
exit()
# Set ouput file
file_out = asksaveasfilename(initialdir=file_in, title="Select output file",
filetypes=(("Comma Separated Values", "*.csv"), ("all files", "*.*")))
if not file_out:
input("Program Terminated. Press Enter to continue...")
exit()
# Read csv
data = pd.read_csv(file_in, low_memory=False)
# Find email column
email_col = column_selection(list(data))
# Test each email address
for i in data[email_col]:
print('')
print(i)
# data["Email Validity"] = ping_email(i)
try:
data.loc[data[email_col] == i, "Email Validity"] = ping_email(i)
except:
data.loc[data[email_col] == i, "Email Validity"] = "Domain Doesn't Exist"
# Write CSV
print("Writing CSV File...")
data.to_csv(file_out, index=False)
print("Wrote CSV File!")
print()
print("File written to: " + file_out)
input("Press Enter to close...")
def ping_email(inputAddress):
# Print what email is being tested
# print("Testing: " + inputAddress)
# Address used for SMTP MAIL FROM command
fromAddress = 'noresponse@gmail.com'
# Simple Regex for syntax checking
regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$'
# Email address to verify
addressToVerify = str(inputAddress)
# Lower case address
addressToVerify = addressToVerify.lower()
# Syntax check
match = re.match(regex, addressToVerify)
if match == None:
return 'Bad Syntax'
# Get domain for DNS lookup
splitAddress = addressToVerify.split('@')
domain = str(splitAddress[1])
# print('Domain:', domain)
# MX record lookup
records = dns.resolver.query(domain, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
# SMTP lib setup (use debug level for full output)
server = smtplib.SMTP()
server.set_debuglevel(0)
# SMTP Conversation
server.connect(mxRecord)
server.helo(server.local_hostname) ### server.local_hostname(Get local server hostname)
server.mail(fromAddress)
code, message = server.rcpt(str(addressToVerify))
server.quit()
#print(code)
#print(message)
# Assume SMTP response 250 is success
if code == 250:
return 'Success'
else:
return 'Failed'
def column_selection(headers):
while True:
try:
print("Select column.")
for j, i in enumerate(headers):
print(str(j) + ": to perform sentiment analysis on column [" + str(i) + "]")
column = headers[int(input("Enter Selection: "))]
except ValueError:
print("Input must be integer between 0 and " + str(len(headers)))
continue
else:
break
return column
if __name__ == '__main__':
main()