Skip to main content

Email validation with syntax checking and normalization.

Project description

philiprehberger-email-validate

Tests PyPI version Last updated

Email validation with syntax checking and normalization.

Installation

pip install philiprehberger-email-validate

Usage

from philiprehberger_email_validate import validate_email

result = validate_email("user@example.com")
print(result.valid)       # True
print(result.normalized)  # "user@example.com"
print(result.domain)      # "example.com"

Quick Syntax Check

from philiprehberger_email_validate import is_valid

is_valid("user@example.com")   # True
is_valid("not-an-email")       # False

Normalization

from philiprehberger_email_validate import normalize

normalize("  User@Example.COM  ")          # "user@example.com"
normalize("first.last+tag@gmail.com")      # "firstlast@gmail.com"
normalize("user+promo@example.com")        # "user@example.com"

MX Lookup

from philiprehberger_email_validate import validate_email

result = validate_email("user@example.com", check_mx=True)
if not result.valid:
    print(result.error)  # "MX lookup failed for domain: example.com"

Disposable Email Detection

from philiprehberger_email_validate import validate_email

result = validate_email("user@mailinator.com")
print(result.is_disposable)  # True

Custom Disposable Domains

from philiprehberger_email_validate import validate_email, set_disposable_domains

# Per-call extra domains
result = validate_email("user@tempmail.xyz", extra_disposable=["tempmail.xyz"])
print(result.is_disposable)  # True

# Global merge with built-in list
set_disposable_domains({"tempmail.xyz", "fakeemail.org"})

Role-Based Email Detection

from philiprehberger_email_validate import validate_email, is_role_based

result = validate_email("info@example.com")
print(result.is_role_based)  # True

is_role_based("admin@example.com")    # True
is_role_based("john@example.com")     # False

Email Domain Suggestions

from philiprehberger_email_validate import validate_email, suggest_domain

result = validate_email("user@gmial.com")
print(result.suggested_domain)  # "gmail.com"

suggest_domain("hotmial.com")   # "hotmail.com"
suggest_domain("gmail.com")     # "" (no suggestion needed)

RFC 5321 Strict Mode

from philiprehberger_email_validate import validate_email

result = validate_email("user@example.com", strict=True)
print(result.valid)  # True

result = validate_email(".user@example.com", strict=True)
print(result.valid)  # False
print(result.error)  # "Local part starts or ends with a dot (RFC 5321)"

Bulk Validation

from philiprehberger_email_validate import validate_many

results = validate_many(["user@example.com", "bad@@email", "test@gmail.com"])
for r in results:
    print(r.normalized, r.valid)

# With concurrent MX lookups and strict mode
results = validate_many(emails, check_mx=True, concurrent=True, strict=True)

API

Function / Class Description
EmailResult Dataclass with valid, normalized, domain, error, is_disposable, is_role_based, and suggested_domain fields
normalize(email) Normalize an email: lowercase, strip whitespace, Gmail dot-insensitivity, plus-addressing cleanup
is_valid(email) Quick boolean syntax check
validate_email(email, check_mx, extra_disposable, strict) Full validation returning an EmailResult
validate_many(emails, check_mx, concurrent, extra_disposable, strict) Validate multiple emails with optional parallel MX lookups
is_role_based(email) Check if an email uses a role-based local part (info@, admin@, etc.)
suggest_domain(domain) Suggest a corrected domain for common typos
set_disposable_domains(domains) Merge additional domains into the global disposable domains set
DISPOSABLE_DOMAINS Mutable set of known disposable email domains
ROLE_PREFIXES Frozen set of known role-based email prefixes
COMMON_DOMAINS Frozen set of common email provider domains used for suggestions

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this project useful:

Star the repo

🐛 Report issues

💡 Suggest features

❤️ Sponsor development

🌐 All Open Source Projects

💻 GitHub Profile

🔗 LinkedIn Profile

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

philiprehberger_email_validate-0.4.0.tar.gz (10.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file philiprehberger_email_validate-0.4.0.tar.gz.

File metadata

File hashes

Hashes for philiprehberger_email_validate-0.4.0.tar.gz
Algorithm Hash digest
SHA256 2e8cb34250fea1b5c9037c90602d3936642e655e7b97036a1f3693284f395e6a
MD5 867f644814508dea05e19ff5b3b7b322
BLAKE2b-256 d28c8d6bae8733e148deef3af0ec4a10baa62eeff7c072b6ceafa638a2e9edce

See more details on using hashes here.

File details

Details for the file philiprehberger_email_validate-0.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_email_validate-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aeb8307470e4215819a3473fbd84012be99471b71c924a3244daf12e0a4b1cc8
MD5 83536798967663ea039c62fcaccc08f7
BLAKE2b-256 f3a774a5edf35eb13b149dff0b4aeab6e2faacb5d71d4827551cb7afa2b8756e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page