DEV Community

Cover image for How Do You Verify Email Addresses in Python (Syntax, MX, and SMTP Checks)?
Muhammad Afzal Kalwar
Muhammad Afzal Kalwar

Posted on

How Do You Verify Email Addresses in Python (Syntax, MX, and SMTP Checks)?

Email verification works by running a series of checks in order: syntax, domain and MX records, and then (where the server allows it) an SMTP-level mailbox check. Each stage rules out more bad addresses, and none of them can guarantee delivery on its own. This guide shows how to build the first stages in Python and explains where the limits are.

What does email verification actually check?

  1. Syntax: does the address have a valid local part and domain?
  2. Domain and MX lookup: does the domain exist and publish mail exchangers?
  3. Disposable and role detection: is it a throwaway domain, or something like info@ or admin@?
  4. SMTP probe: does the receiving server accept RCPT TO for this mailbox?

Many servers accept all recipients (catch-all) or throttle probes, so the SMTP result is often "unknown" and should be treated that way.

Step 1: syntax and MX check

import re
import dns.resolver  # pip install dnspython

EMAIL_RE = re.compile(r"^[A-Za-z0-9._%+-]+@([A-Za-z0-9-]+\.)+[A-Za-z]{2,}$")

def check_syntax(email: str) -> bool:
    return bool(EMAIL_RE.match(email))

def get_mx(domain: str) -> list[str]:
    try:
        answers = dns.resolver.resolve(domain, "MX", lifetime=5)
        return [str(r.exchange).rstrip(".") for r in sorted(answers, key=lambda r: r.preference)]
    except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer, dns.resolver.LifetimeTimeout):
        return []
Enter fullscreen mode Exit fullscreen mode

Step 2: classify instead of returning true/false

ROLE = {"info", "admin", "support", "sales", "contact"}

def verify(email: str) -> dict:
    if not check_syntax(email):
        return {"email": email, "status": "invalid", "reason": "syntax"}
    local, domain = email.rsplit("@", 1)
    mx = get_mx(domain)
    if not mx:
        return {"email": email, "status": "invalid", "reason": "no_mx"}
    status = "risky" if local.lower() in ROLE else "deliverable_domain"
    return {"email": email, "status": status, "mx": mx[0]}
Enter fullscreen mode Exit fullscreen mode

Step 3: a polite SMTP probe

import smtplib

def smtp_probe(email: str, mx_host: str, sender: str = "verify@example.com") -> str:
    try:
        with smtplib.SMTP(mx_host, 25, timeout=10) as s:
            s.helo("example.com")
            s.mail(sender)
            code, _ = s.rcpt(email)
            if code == 250:
                return "accepted"
            if 500 <= code < 600:
                return "rejected"
            return "unknown"
    except (smtplib.SMTPException, OSError):
        return "unknown"
Enter fullscreen mode Exit fullscreen mode

Use your real sending domain, probe at a low rate, cache results per domain, and never treat "accepted" as proof the mailbox is real, because catch-all servers accept everything. Many cloud hosts also block outbound port 25.

Practical tips

  • Run domain-level checks once per domain, not once per address.
  • Keep "unknown" as its own status and send those to a small test batch.
  • Remove hard bounces from your list right after each campaign.
  • Verify at sign-up time to keep bad data out.

FAQ

Can verification guarantee an email will be delivered? No. It lowers bounce risk; inbox placement also depends on reputation and content.

What is a catch-all domain? One that accepts mail for any address, so the SMTP check cannot confirm a specific mailbox.

Is SMTP probing safe to run at scale? Only at low, rate-limited volumes from a properly configured sender; aggressive probing can get your IP blocked.

Should I build or buy? Building the syntax and MX stages is easy; reliable SMTP probing at volume needs infrastructure and monitoring.


Written by Muhammad Afzal, Python developer at INDUS Web Agency.

Top comments (0)