Skip to content

Instantly share code, notes, and snippets.

@raz0229
Last active October 17, 2025 16:23
Show Gist options
  • Select an option

  • Save raz0229/bd08cd96ef0d22fe8e134fecbc0e1b4f to your computer and use it in GitHub Desktop.

Select an option

Save raz0229/bd08cd96ef0d22fe8e134fecbc0e1b4f to your computer and use it in GitHub Desktop.
Lookup and save all valid emails under domain ucp.edu.pk (University of Central Punjab) from batches 2022-2025, spring and fall terms, and CS, AI, AF, SE departments in csv file
# pip install dnspython
import smtplib
import dns.resolver
import csv
from itertools import product
def verify_email_address(email):
domain = email.split('@')[-1]
try:
# Step 1: Get MX record for the domain
mx_records = dns.resolver.resolve(domain, 'MX')
mx_record = str(mx_records[0].exchange)
# Step 2: Connect to the mail server
server = smtplib.SMTP(timeout=5)
server.connect(mx_record)
server.helo(server.local_hostname) # Identify ourselves
server.mail('[email protected]') # Dummy sender
code, message = server.rcpt(email) # RCPT TO command
server.quit()
# Step 3: Interpret the SMTP response
return code == 250
except Exception as e:
print(f"Error verifying {email}: {e}")
return False
# Define available values
terms = ["f", "s"]
batches = ["22", "23", "24", "25"]
depts = ["af", "cs", "se", "af"]
regNos = [f"{i:04d}" for i in range(1, 1360)] # 0001 to 1360
# Output CSV file
output_file = "verified_ucp_emails_list.csv"
# Create CSV file with header (once)
with open(output_file, mode="w", newline="") as file:
writer = csv.writer(file)
writer.writerow(["Email"])
# Iterate through all combinations
for term, batch, dept, regNo in product(terms, batches, depts, regNos):
email = f"l1{term}{batch}bs{dept}{regNo}@ucp.edu.pk"
print(f"Checking: {email}")
if verify_email_address(email):
print(f"✅ Valid: {email}")
# Append valid email to CSV
with open(output_file, mode="a", newline="") as file:
writer = csv.writer(file)
writer.writerow([email])
else:
print(f"❌ Invalid: {email}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment