-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidate_emails.py
More file actions
115 lines (95 loc) · 4.01 KB
/
validate_emails.py
File metadata and controls
115 lines (95 loc) · 4.01 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
#!/usr/bin/env python3
import csv
import re
import dns.resolver
import smtplib
import socket
from email.mime.text import MIMEText
def validate_email_syntax(email):
"""Basic email syntax validation"""
pattern = r'^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$'
return re.match(pattern, email) is not None
def check_mx_record(domain):
"""Check if domain has valid MX record"""
try:
answers = dns.resolver.resolve(domain, 'MX')
return len(answers) > 0
except (dns.resolver.NoAnswer, dns.resolver.NXDOMAIN, dns.resolver.NoNameservers):
return False
def check_smtp_connection(email):
"""Check if SMTP server accepts the email (without sending)"""
try:
domain = email.split('@')[1]
mx_records = dns.resolver.resolve(domain, 'MX')
mx_record = str(mx_records[0].exchange)
# Connect to SMTP server
server = smtplib.SMTP(mx_record, 25, timeout=10)
server.quit()
return True
except:
return False
def is_well_known_company(email):
"""Check if it's a well-known company domain"""
well_known_domains = [
'tcs.com', 'infosys.com', 'wipro.com', 'hcl.com', 'techmahindra.com',
'capgemini.com', 'accenture.com', 'cognizant.com', 'lntinfotech.com',
'mphasis.com', 'hexaware.com', 'virtusa.com', 'ust-global.com',
'genpact.com', 'exlservice.com', 'wns.com', 'nttdata.com',
'prolifics.com', 'cyient.com', 'valuelabs.com', 'neudesic.com',
'bahwancybertek.com', 'peoplestrong.com', 'adp.com'
]
domain = email.split('@')[1].lower()
return domain in well_known_domains
def validate_emails(input_csv, output_csv):
"""Validate emails and create a clean list"""
valid_emails = []
invalid_emails = []
print("🔍 Validating email addresses...")
with open(input_csv, 'r', encoding='utf-8') as f:
reader = csv.DictReader(f)
for i, row in enumerate(reader, 1):
email = row['email'].strip().lower()
first_name = row['first_name']
company = row['company']
print(f"Checking {i}: {email}")
# Step 1: Basic syntax check
if not validate_email_syntax(email):
print(f" ❌ Invalid syntax: {email}")
invalid_emails.append({'email': email, 'reason': 'Invalid syntax'})
continue
# Step 2: Check if well-known company
if not is_well_known_company(email):
print(f" ⚠️ Unknown company domain: {email}")
invalid_emails.append({'email': email, 'reason': 'Unknown company domain'})
continue
# Step 3: Check MX record
domain = email.split('@')[1]
if not check_mx_record(domain):
print(f" ❌ No MX record: {email}")
invalid_emails.append({'email': email, 'reason': 'No MX record'})
continue
# If all checks pass
valid_emails.append({
'email': email,
'first_name': first_name,
'company': company
})
print(f" ✅ Valid: {email}")
# Write valid emails
with open(output_csv, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=['email', 'first_name', 'company'])
writer.writeheader()
writer.writerows(valid_emails)
print(f"\n📊 Results:")
print(f"✅ Valid emails: {len(valid_emails)}")
print(f"❌ Invalid emails: {len(invalid_emails)}")
print(f"📁 Saved valid emails to: {output_csv}")
return len(valid_emails)
if __name__ == "__main__":
input_file = "cleaned_new_contacts.csv"
output_file = "validated_contacts.csv"
valid_count = validate_emails(input_file, output_file)
if valid_count > 0:
print(f"\n🎯 Ready to send to {valid_count} validated contacts!")
else:
print("\n❌ No valid contacts found!")