forked from sathish-1804/email-validation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.py
More file actions
256 lines (214 loc) · 9.29 KB
/
temp.py
File metadata and controls
256 lines (214 loc) · 9.29 KB
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import pandas as pd
import source_code as sc
from suggestion import suggest_email_domain
import whois
from popular_domains import emailDomains
import streamlit as st
from streamlit_extras.metric_cards import style_metric_cards
from streamlit.components.v1 import html
# Set page configuration
st.set_page_config(
page_title="Email Verification Tool",
page_icon="✅",
layout="centered",
)
# Custom HTML and CSS for styling
custom_html = """
<style>
body {
font-family: 'Arial', sans-serif;
background-color: #f4f4f9;
color: #333;
}
.custom-textarea {
background-color: #fff;
border: 2px solid #ddd;
border-radius: 8px;
padding: 10px;
font-size: 16px;
width: 100%;
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
transition: border-color 0.3s ease;
}
.custom-textarea:focus {
border-color: #007bff;
outline: none;
}
.custom-button {
background-color: #007bff;
color: white;
border: none;
border-radius: 8px;
padding: 10px 20px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.3s ease;
}
.custom-button:hover {
background-color: #0056b3;
}
.tab-content {
margin-top: 20px;
}
.metric-card {
background-color: #007bff;
color: white;
padding: 10px;
border-radius: 8px;
text-align: center;
}
.metric-card h4 {
margin: 0;
}
.metric-card p {
margin: 5px 0 0;
}
</style>
"""
# Inject custom HTML and CSS
html(custom_html)
# Function to label an email
def label_email(email):
if not sc.is_valid_email(email):
return "Invalid"
if not sc.has_valid_mx_record(email.split('@')[1]):
return "Invalid"
if not sc.verify_email(email):
return "Unknown"
if sc.is_disposable(email.split('@')[1]):
return "Risky"
return "Valid"
# Function to process pasted emails and return DataFrame
def process_pasted_emails(pasted_emails):
# Split the pasted emails by newlines and commas
emails = [email.strip() for email in pasted_emails.replace(',', '\n').splitlines() if email.strip()]
# Create a list to store the results
results = []
for email in emails:
label = label_email(email)
results.append([email, label])
# Create a DataFrame for the results
result_df = pd.DataFrame(results, columns=['Email', 'Label'])
result_df.index = range(1, len(result_df) + 1) # Starting index from 1
return result_df
# Function to download DataFrame as selected file type
def download_dataframe(df, file_format):
if file_format == 'CSV':
return df.to_csv(index=False).encode('utf-8')
elif file_format == 'TXT':
return df.to_csv(index=False, sep='\t').encode('utf-8')
elif file_format == 'JSON':
return df.to_json(orient='records').encode('utf-8')
# Main function
def main():
st.title("Email Verification Tool", help="This tool verifies the validity of an email address.")
st.info("The result may not be accurate. However, it has 90% accuracy.")
t1, t2 = st.tabs(["Single Email", "Bulk Email Processing"])
with t1:
# Single email verification
email = st.text_input("Enter an email address:", key="single_email", help="Enter the email address you want to verify.")
if st.button("Verify", key="verify_single"):
with st.spinner('Verifying...'):
result = {}
# Syntax validation
result['syntaxValidation'] = sc.is_valid_email(email)
if result['syntaxValidation']:
domain_part = email.split('@')[1] if '@' in email else ''
if not domain_part:
st.error("Invalid email format. Please enter a valid email address.")
else:
# Additional validation for the domain part
if not sc.has_valid_mx_record(domain_part):
st.warning("Not valid: MX record not found.")
suggested_domains = suggest_email_domain(domain_part, emailDomains)
if suggested_domains:
st.info("Suggested Domains:")
for suggested_domain in suggested_domains:
st.write(suggested_domain)
else:
st.warning("No suggested domains found.")
else:
# MX record validation
result['MXRecord'] = sc.has_valid_mx_record(domain_part)
# SMTP validation
if result['MXRecord']:
result['smtpConnection'] = sc.verify_email(email)
else:
result['smtpConnection'] = False
# Temporary domain check
result['is Temporary'] = sc.is_disposable(domain_part)
# Determine validity status and message
is_valid = (
result['syntaxValidation']
and result['MXRecord']
and result['smtpConnection']
and not result['is Temporary']
)
st.markdown("**Result:**")
# Display metric cards with reduced text size
col1, col2, col3 = st.columns(3)
col1.markdown(f"<div class='metric-card'><h4>Syntax</h4><p>{result['syntaxValidation']}</p></div>", unsafe_allow_html=True)
col2.markdown(f"<div class='metric-card'><h4>MxRecord</h4><p>{result['MXRecord']}</p></div>", unsafe_allow_html=True)
col3.markdown(f"<div class='metric-card'><h4>Is Temporary</h4><p>{result['is Temporary']}</p></div>", unsafe_allow_html=True)
style_metric_cards()
# Show SMTP connection status as a warning
if not result['smtpConnection']:
st.warning("SMTP connection not established.")
# Show domain details in an expander
with st.expander("See Domain Information"):
try:
dm_info = whois.whois(domain_part)
st.write("Registrar:", dm_info.registrar)
st.write("Server:", dm_info.whois_server)
st.write("Country:", dm_info.country)
except:
st.error("Domain information retrieval failed.")
# Show validity message
if is_valid:
st.success(f"{email} is a Valid email")
else:
st.error(f"{email} is an Invalid email")
if result['is Temporary']:
st.text("It is a disposable email")
with t2:
# Bulk email processing
st.header("Bulk Email Processing")
# Option to upload a file
input_file = st.file_uploader("Upload a CSV, XLSX, or TXT file", type=["csv", "xlsx", "txt"])
# Option to paste emails directly
st.markdown("### Paste Emails Below")
pasted_emails = st.text_area(
"Paste emails here (one per line or separated by commas)",
height=200,
key="pasted_emails",
help="You can paste a list of emails separated by commas or newlines.",
)
# Select file format for download
file_format = st.selectbox("Select file format for download", ["CSV", "TXT", "JSON"])
# Add a custom validation button
if st.button("Validate Pasted Emails", key="validate_pasted_emails", help="Click to validate the pasted emails."):
if pasted_emails:
with st.spinner("Validating pasted emails..."):
result_df = process_pasted_emails(pasted_emails)
st.dataframe(result_df)
# Filter valid emails
valid_emails_df = result_df[result_df['Label'] == 'Valid']
# Provide download link
st.download_button(
label="Download Valid Emails",
data=download_dataframe(valid_emails_df, file_format),
file_name=f"valid_emails.{file_format.lower()}",
mime="text/csv" if file_format == 'CSV' else "text/plain" if file_format == 'TXT' else "application/json"
)
else:
st.warning("Please paste some emails to validate.")
if input_file:
st.write("Processing...")
if input_file.type == 'text/plain':
process_txt(input_file)
else:
df = process_csv(input_file)
st.success("Processing completed. Displaying results:")
st.dataframe(df)
if __name__ == "__main__":
main()