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

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 Prefix

    Valid 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 Email

    The symbol (@) Symbol should be then Followed by the Domain of the email address.

  4. Allowed Characters

    The 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.


Examples of Invalid Email Address

    A few examples of invalid email addresses are Following.


    Method 1: Check if an Email Address is Valid with Python Regex

    Regular Expression in Python is one way that will help us find if the email address is valid or not. Creating our own Regular expression will not always be a good approach to use in this case scenario but it can help us customize our version of validation. You can learn about regular expression in python from this article which will help you how to write a good regular expression. but for our example, we will use a general regular expression that can help us to find if an email address is valid or not. 
    Below is the sample code that will return true if an email address is valid or not with python regular expression. 

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

    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. 

    validate email address picture imports

    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

    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
    #output: False

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

    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('', verify=True)

    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. 
    if you are interested in Python Programming Then Please Subscribe to My Youtube Channel as well.

Post a Comment

* Please Don't Spam Here. All the Comments are Reviewed by Admin.
  1. It is showing error:
    check = validate_email(mail_list[mail], verify=True)
    File "/usr/local/lib/python3.10/dist-packages/", line 197, in validate_email
    return check_mx_record(email, verify=verify, smtp_timeout=smtp_timeout)
    File "/usr/local/lib/python3.10/dist-packages/", line 137, in check_mx_record
    mx_hosts = get_mx_ip(hostname)
    File "/usr/local/lib/python3.10/dist-packages/", line 112, in get_mx_ip
    MX_DNS_CACHE[mx_host] = DNS.mxlookup(mx_host)
    File "/home/vamsi/.local/lib/python3.10/site-packages/DNS/", line 56, in mxlookup
    l = dnslookup(name, qtype, timeout)
    File "/home/vamsi/.local/lib/python3.10/site-packages/DNS/", line 38, in dnslookup
    result = Base.DnsRequest(name=name, qtype=qtype).req(timeout=timeout)
    File "/home/vamsi/.local/lib/python3.10/site-packages/DNS/", line 324, in req
    File "/home/vamsi/.local/lib/python3.10/site-packages/DNS/", line 377, in sendUDPRequest
    raise first_socket_error
    File "/home/vamsi/.local/lib/python3.10/site-packages/DNS/", line 352, in sendUDPRequest
    File "/home/vamsi/.local/lib/python3.10/site-packages/DNS/", line 135, in processUDPReply
    raise TimeoutError('Timeout')
    DNS.Base.TimeoutError: Timeout

    Please help me what to do

  2. Exception: For check the mx records or check if the email exists you must have installed pyDNS python package

    Having this libraries installed

    % pip list
    Package Version
    ---------------------- --------
    dnspython 2.2.1
    filelock 3.8.0
    idna 3.3
    numpy 1.23.1
    pandas 1.4.3
    pip 22.2.2
    py3-validate-email 1.0.7
    py3dns 3.2.1
    python-dateutil 2.8.2
    pytz 2022.2.1
    setuptools 49.2.1
    six 1.16.0
    validate-email-address 1

Post a Comment

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !