Pip email_validator
email-validator
is a Python package that provides a simple way to validate email addresses. It's available on PyPI, and you can install it using pip:
pip install email-validator
Here's an example of how you can use it:
from email_validator import validate_email, EmailNotValidError
def validate_email_address(email):
try:
v = validate_email(email)
return v.email # returns email address
except EmailNotValidError as e:
return str(e)
email = "invalid_email"
print(validate_email_address(email)) # Output: 'Invalid email: invalid_email'
In this example, the validate_email
function takes an email address as input and returns the validated email address if it's valid. If the email address is invalid, it raises an EmailNotValidError
exception, which is caught and returned as a string.
The email-validator
package provides a more comprehensive email validation than the built-in email
module in Python, as it checks for more complex email address formats and also checks for the existence of the email address.