Email is a method of exchanging messages between people using electronic devices. It is a widely-used communication medium that can also be used for signing up in the application. Keeping this in mind, lots of invalid emails can cause the use of high bandwidth resulting in an increase in cost. So, the email field should be verified earlier before it ping to the SMTP server. Python provides a couple of methods to check whether an email is valid or not.

how-to-verify-email-in-python

 

Methods to verify email in Python

In this tutorial, we will discuss three ways to verify email. They are as follows:

  • Using Python Regex
  • Using third-party libraries

 

Using Python Regex

A regular expression(Regex) is a sequence of characters defining a search pattern. It can be used to check if a particular string contains the specified search pattern.

Python provides a built-in module called re, that provides regular expression matching operations. We can simply call in our program as:

import re

 

To verify email patterns, we will use re module one of the function called search().

re.search()

It returns a Match object if there is a match in a specified pattern any part in the string else returns None.

Code:

# import regex module
import re

# finalize email regex pattern
pattern = "^[A-Za-z0-9]+[\._]?[A-Za-z0-9]+[@]\w+[.]\w{2,3}$"

def verify_email(email: str):
    """function to verify email patterns"""
    verify = re.search(pattern, email)
    
    # check if verify object returns 
    # Match object or None
    if verify:
        print("Email verified")
    else:
        print("Email not verified")


email1 = "pythonsansar@example.com"
verify_email(email1)

email2 = "psansar77.com"
verify_email(email2)

email3 = "python.sansar@com"
verify_email(email3)

email4 = "python.sansar@example.com"
verify_email(email4)

email5 = "PYTHON.SANSAR@EXAMPLE.COM"
verify_email(email4)

Output:

Email verified
Email not verified
Email not verified
Email verified
Email verified

 

Using third-party libraries

verify-email and validate_email are Python third-party libraries for checking email patterns as well as their existence.

Note: Python regex module re, we use above only check the format of email patterns, and it does not tell about the email really exists or not.

 

Library: verify-email

verify-email can verify any email address by efficiently checking the domain name and pinging the handler to verify its existence.

pip install verify-email

Code:

from verify_email import verify_email


# dummy email, not exists
email1 = "pythonsansar@example.com"
# check email1
print(email1, verify_email(email1))


# email exists
email2 = "pythonsansar@gmail.com"
# check email2
print(email2, verify_email(email2))

# email not formatted
email3 = "psansar77.com"
# check email3
print(email3, verify_email(email3))

Output:

pythonsansar@example.com False
pythonsansar@gmail.com True
psansar77.com False

 

Library: validate_email

validate_email is a package for Python that check if an email is valid, properly formatted, and really exists.

pip install validate_email

Code:

from validate_email import validate_email


# dummy email, not exists
email1 = "pythonsansar@example.com"
# check email1
print(email1, validate_email(email1))


# email exists
email2 = "pythonsansar@gmail.com"
# check email2
print(email2, validate_email(email2))

# email not formatted
email3 = "psansar77.com"
# check email3
print(email3, validate_email(email3))

Output:

pythonsansar@example.com True
pythonsansar@gmail.com True
psansar77.com False

By default, the validate_email library only checks the string matches with the email patterns or not.

Leave a Reply

Your email address will not be published.