How to check if an Email Address is Valid and Exists With Python

With Python Programming Language, We can Easily Check if am Email address is Valid or not a valid email address. Once we find that an Email address is Valid then we can check if the email address is actually existing or not. Normally for validation, we can use a Normal Regular Expression to check for all the conditions if an Email Address is Valid or Not. In this article, I will show you all possible ways through which we can check if an email address is valid and Exists with Python Programming Langauge. To check if an Email address is valid in python we can write a Regular Expression but for that, we will see how a normal address looks like. 

What is the Format of a Valid Email Address?

To check if an Email Address is valid or Not we have to look at the normal format of the email address. There are some conditions and if the email address fits with these conditions then we will consider that these email addresses or Valid though they might not have existed.

Conditions for a valid email address

  1.  Valid Email PrefixValid Email address should consists of an email prefix and an email domain but both of them should be an acceptable format.
  2.  Email Prefix :The prefix of the email address should be at the left of the @ symbol.
  3.  Doamin Part of the EmailThe symbol (@) Symbol should be then Followed by the Domain of the email address.
  4.  Allowed CharactersThe Allowed Characters are letters(a-z), numbers, underscores, periods, and dashes.
  5.  The final constraint on a valid email address is that it underscores or dash must be followed by one or more letters or numbers.

Examples of Valid Email Address

A few examples of valid email addresses are Following.

  • bbc@gamil.com
  • ali@alixaprodev.com

Examples of Invalid Email Address

  • abc..@gmail.com
  • abc-@gmail.com

def isvalidEmail(email):
    pattern = "^\S+@\S+\.\S+$"
    objs = re.search(pattern, email)
    try:
        if objs.string == email:
            return True
    except:
        return False


VorNotV = isvalidEmail("haxrataligmail.com")
print(VorNotV)

The Above code will return False as the email address is not a valid Email Address. Although the above code will work most of the time sometimes it is hard to find if an email is valid or not by only using the python regular expression. If you want to create your own regular Expression for email validation then you definitely need to understand the regular expressions along with the international standard format for email addresses which can be found here in this link.

Method 2: Check if an Email Address is Valid or Not with Python Package

Python Provide a Third Party Package to find if an Email address is valid or not. With this third-party Python package, you can easily find the validity of an email address. This Python Package is named is “validate-email-address” which is maintained by an Experienced Python Programmer “xj9“. 
This package makes sure if the email address is valid, properly formatted, and if this email really exists. We will check it one by one. 

About validate-email-address Python Package

This Package contained only one function validate-email-address that takes two parameters one is the email address and the other parameter is for the type of validation. This package is using the RFC 2822 style of validation of an email address which is a well-known standard for email address validation. They claimed that it is better than the standard Python Library email.utils which they claimed can not detect the validation of some malformed email addresses. Under the hood, this package also uses the Python re module which is a standard python module for regular expressions.
All they are doing is comparing the input email address to the giganic regular expressions which are formatted according to the RFC 2822 style of email addresses. 
The Main Import of the validate-email-address Python Package can be seen in the following Picture. 

The Best Way to use the validate-email-address() Python

To check if the email address is valid we can use the validate-email-address() method which will return true if the email address is valid otherwise it will return false. 
Step 1. Install the Third Party Python Package validate-email-address
You can install it by using pip as follows.

pip install validate-email-address

Step 2. Install the Third Party Python Package py3dns
You can install DNS by using pip as follows. Remember the official documentation say that you have to install DNS which is an outdated Python Package and the updated version can be found here as py3dns any way you have to install it in the following way.

pip install py3dns

Step 3. Import the Code and use the Validate email Address Function


from validate_email_address import validate_email
isvalid=validate_email('alixaprodev@gmail.com')
print(isvalid)

The above code will print True to the console as the input email address is a valid Email address. 

from validate_email_address import validate_email
isvalid=validate_email('alixaprodev@gmail')
print(isvalid)
#output: False

Check if an Email Address Exist or Not with Python:-

Although there might be many ways you can check if a given mail address exists or not. sometimes even if a mail address seems valid but it might not exist. so to make sure to send email to those address which exists we can first check if the email address exists. The same Python package that we use for the email validation provides us the ability to check if am email exists or not. Look at the following code. it will return True if the email address exists otherwise it will return Flase. 


from validate_email_address import validate_email
isExists = validate_email('haxratali0@gmail.com', verify=True)
print(isExists)

By just passing another optional argument to the function it will now use the Python DNS package to make sure if the email address actually exists or not. It will return True if the email address exists otherwise it will return False.

Summary and Conclusion:-

It is highly recommended to use the third-party Python Package to make sure is the email address is valid but if you want to create your own validation rules then you have to go through the regular expression and understand them will in the context of the python programming language. If you have any Questions Regarding this Article please put them in the comment section I will be happy to answer them all. 

Leave a Comment

Scroll to Top