DNS and SMTP Email Validator is a robust Python library designed to validate email addresses at multiple levels. It checks the format, domain, and MX records, and even communicates with the mail server to confirm the existence of the recipient's email address.
- Email Format Validation: Ensure the email address matches a valid format.
- Domain Validation: Validate email domains against a customizable allowlist.
- MX Record Lookup: Check for valid mail exchange (MX) records for the domain.
- SMTP Communication: Optionally verify the existence of the recipient email address via the SMTP protocol.
- Customizable Error Handling: Choose to raise exceptions or store errors for later inspection.
Install the package via pip:
pip install dns-smtp-email-validator
from dns_smtp_email_validator import DNSSMTPEmailValidator
# Instantiate the validator
validator = DNSSMTPEmailValidator(email="[email protected]")
# Validate the email
if validator.is_valid():
print("✅ Email is valid!")
else:
print("❌ Invalid email!")
print("Errors:", validator.errors)
validator = DNSSMTPEmailValidator(
email="[email protected]",
sender_email="[email protected]",
raise_exception=True
)
if validator.is_valid():
print("✅ Email is valid!")
else:
print("❌ Email validation failed!")
You can configure allowed email domains through environment variables. Use a .env
file or system-level configuration.
ALLOWED_EMAIL_DOMAINS=gmail.com,yahoo.com,outlook.com
If ALLOWED_EMAIL_DOMAINS
is not provided, the library uses the following defaults:
ALLOWED_EMAIL_DOMAINS = [
"aol.com",
"gmail.com",
"hotmail.com",
"icloud.com",
"outlook.com",
"yahoo.com",
"zoho.com"
]
-
Email Format Check: Confirms the email follows standard formatting rules (e.g.,
[email protected]
). -
Domain Allowlist Check: Validates that the email domain is in the allowed list.
-
MX Record Lookup: Retrieves the mail exchange (MX) records for the email domain.
-
SMTP Communication (Optional): Connects to the mail server and verifies the recipient's address.
To ensure the library is working correctly, run the test suite:
pytest tests/
The library offers two approaches to handle errors:
- Raise Exceptions: Enable
raise_exception=True
to raise exceptions for validation errors. - Collect Errors: Store errors in the
errors
attribute for later inspection.
Contributions are welcome! If you'd like to enhance the functionality or fix an issue, feel free to open a pull request.
- Fork the repository.
- Create a feature branch.
- Commit your changes.
- Submit a pull request.
This project is licensed under the MIT License.
For any issues or feature requests, please open an issue.
Happy Validating! 🎉