A Python library for validating and suggesting corrections for email addresses.
Project description
Email Safeguard
A comprehensive email validation library that provides domain suggestions, disposable email detection, and MX record validation with a focus on security and user experience.
Features
- 🔍 Smart validation with helpful suggestions
- 🛡️ Disposable email detection
- 📨 MX record validation
- ⚡ Fast and customizable
- 🎯 Type hints and modern Python support
- 🔧 Configurable validation rules
Installation
pip install email-safeguard
Quick Start
from email_safeguard.validator import EmailSafeguard
validator = EmailSafeguard()
result = validator.validate("user@gmial.com")
if result.is_valid:
if result.suggestions:
print(f"Email is valid but did you mean: {result.suggestions.get('domain')}?")
else:
print("Email is valid!")
else:
print(f"Error: {result.message}")
Advanced Usage
Custom Configuration
validator = EmailSafeguard(
check_mx=True, # Enable MX record validation
allow_disposable=False, # Reject disposable emails
suggest_corrections=True, # Suggest corrections for typos
max_distance=2 # Maximum edit distance for suggestions
)
Handling Results
from email_safeguard.validator import EmailSafeguard, ValidationResult
validator = EmailSafeguard()
result = validator.validate("user@tempmail.com")
# Check the validation result
if result.result == ValidationResult.VALID:
print("Email is valid!")
elif result.result == ValidationResult.DISPOSABLE:
print("Disposable emails not allowed")
elif result.result == ValidationResult.INVALID_DOMAIN:
if result.suggestions and 'domain' in result.suggestions:
print(f"Invalid domain. Did you mean: {result.suggestions['domain']}?")
elif result.result == ValidationResult.NO_MX_RECORD:
print("Domain has no mail server")
ValidationResult Types
The library provides several validation result types:
from email_safeguard.validator import ValidationResult
# Available validation results:
# ValidationResult.VALID
# ValidationResult.INVALID_FORMAT
# ValidationResult.INVALID_DOMAIN
# ValidationResult.INVALID_TLD
# ValidationResult.DISPOSABLE
# ValidationResult.NO_MX_RECORD
# ValidationResult.TIMEOUT
Validation Response
The validate() method returns a ValidationResponse object with the following attributes:
class ValidationResponse:
is_valid: bool # Whether the email is valid
result: ValidationResult # The specific validation result
message: str # A descriptive message
suggestions: Optional[Dict[str, str]] # Suggested corrections if any
Data Files
The library uses three customizable data files:
popular_domains.txt: Common email domainspopular_tlds.txt: Valid top-level domainsdisposable_domains.txt: Known disposable email providers
Custom Data Files
validator = EmailSafeguard(data_dir="path/to/data/directory")
Development
Running Tests
# Run all tests
python -m pytest
# Run with coverage
python -m pytest --cov=email_safeguard
Type Checking
mypy email_safeguard
Contributing
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Author
Chukwuka Ibejih (chukaibejih@gmail.com)
Acknowledgements
Built with:
- Django - Email validation
- python-Levenshtein - String similarity
- dnspython - DNS queries
If you find this library helpful, please give it a ⭐!
Project details
Release history Release notifications | RSS feed
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 email_safeguard-0.1.6.tar.gz.
File metadata
- Download URL: email_safeguard-0.1.6.tar.gz
- Upload date:
- Size: 969.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
86ce3a0d91d3ebb79928e14714bd06e466b93bbea6f4cc19bffd477d281534ef
|
|
| MD5 |
501997a5fd5c9bf3e0e755fe51e0339b
|
|
| BLAKE2b-256 |
329b953e53fab523cc634b28323d746cbc2ee6fa60d90f28bb93a4759b1c305b
|
File details
Details for the file email_safeguard-0.1.6-py3-none-any.whl.
File metadata
- Download URL: email_safeguard-0.1.6-py3-none-any.whl
- Upload date:
- Size: 968.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
38056e1c98c765af5d7a9c724415a1e06239a19b9c37fbb52128def60dc1e2c2
|
|
| MD5 |
93b571cb7dd370f6605ed4363bdb0584
|
|
| BLAKE2b-256 |
e6134abd65ff0ff1f6dd3b5f25c00b9de3ed0f8cce09f1d8635dae28ec5e7120
|