Email validation with syntax checking and normalization.
Project description
philiprehberger-email-validate
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)
Extract Emails from Text
from philiprehberger_email_validate import extract_emails
emails = extract_emails("Contact hello@example.com or support@test.org")
# ["hello@example.com", "support@test.org"]
Email Parts
from philiprehberger_email_validate import email_parts
parts = email_parts("user@example.com")
print(parts.local) # "user"
print(parts.domain) # "example.com"
print(parts.tld) # "com"
Mask Email
from philiprehberger_email_validate import mask_email
mask_email("john@example.com") # "j***n@example.com"
mask_email("alice@example.com") # "a***e@example.com"
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 |
extract_emails(text) |
Extract all valid email addresses from text, deduplicated |
email_parts(email) |
Split email into structured parts (local, domain, tld, normalized) |
EmailParts |
Dataclass with local, domain, tld, normalized fields |
mask_email(email, mask_char, visible) |
Mask the local part for privacy display |
Development
pip install -e .
python -m pytest tests/ -v
Support
If you find this project useful:
License
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file philiprehberger_email_validate-0.5.0.tar.gz.
File metadata
- Download URL: philiprehberger_email_validate-0.5.0.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6eba4e722a875e7c1e2df649589e0a099b51b76175745e434dae311ea57cbf3e
|
|
| MD5 |
7696bb528fa1761415ac0434d1f87972
|
|
| BLAKE2b-256 |
5f3c530ae824abe7d92388c45cd57aa8e8c9596c0b0400fb3afbf1d07dae0773
|
File details
Details for the file philiprehberger_email_validate-0.5.0-py3-none-any.whl.
File metadata
- Download URL: philiprehberger_email_validate-0.5.0-py3-none-any.whl
- Upload date:
- Size: 8.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c00073b1effadf6d634bbd00d0c32a48fae4580c3398e84ceadb35296c8c3fa8
|
|
| MD5 |
fd7ccf37804b3835a13b5253688ea694
|
|
| BLAKE2b-256 |
0635e2713641bf99815a796e5ac86c666e84db4b7bc66545900a2e73e6d1454c
|