DEV Community

Cover image for How to Verify Email Addresses Using Python
Khumbo Klein Chilamwa
Khumbo Klein Chilamwa

Posted on • Edited on

How to Verify Email Addresses Using Python

Email verification is a process that involves the confirmation of the authenticity or legitimacy of an email address. Nowadays businesses are integrating email verification into their day-to-day operations and this has proved to be more effective as it helps them keep only customers' email addresses that are valid and reachable. The email verifying tools that are available out there are cool and sophisticated but they come with a price tag, for a developer that should never be a huddle because you can build your own tool to verify a single email or bulk email. In this article, I will show you how you can build your own email verification tool using the Python library called verify-email.

Here is the Table of Contents:

  • Installing the Required Package

  • Verifying a Single Email Address

  • Verifying Bulk Email Addresses

  • Final Thoughts

Installing the Required Package

First things first, you need to have the verify-email package installed. Ensure that pip is working on your computer, in your terminal run the below command to install the package:
$ pip install verify-email
The verify-email package verifies email addresses by checking the domain name and pinging the handler or username for its existence.

Verifying a Single Email Address

Firstly, open a new Python file, call it email-verifier-script.py, and on top of the file do the following import:
from verify_email import verify_email
After doing the import, you need to create an email verifying handler, this is a function that will handle the email verification process. Call the function email_verifier() and make it look like this:



def email_verifier(email):
    # verifying email using verify_email function
    verify = verify_email(email)
    # checking if the verify value is True
    if verify == True:
        print(f'{email} is a valid email address')
    # checking if the verify value is False
    elif verify == False:
        print(f'{email} is not a valid email address')



Enter fullscreen mode Exit fullscreen mode

The email_verifier() function is taking an argument email, this will be provided by the user, so do the following:



# getting email from user
my_email = input('Enter email address:')


Enter fullscreen mode Exit fullscreen mode

After the user has provided the email address, it needs to be verified, to do that do a function call as below:



# calling the email_verifier function
email_verifier(my_email)


Enter fullscreen mode Exit fullscreen mode

Now you are set to verify your first email address, open the terminal and navigate to the directory where the script is located. Run this script using this command:
python email-verifier-script.py

You will be prompted to enter an email address, if the email address is valid, the output will look like this:

Image description

If you enter an invalid email address, this is what you get:

Image description

Verifying Bulk Email Addresses

In this section, you will get to verify a list of email addresses, so tweak the email-verifier-script.py file so that it looks like this:



from verify_email import verify_email

# a list of email addresses to be verified
email_addresses = ['khumboklein@gmail.com', 'muo@gmail.com', 
                   'admin@gmail.com', 'kchilamwa@hackbits.tech',
                   'trainings@updates.internshala.com', 'noreply@medium.com',
                   'maryellen.m@valnetinc.com']

for email in email_addresses:
    # verify individual email address
    verify = verify_email(email)

    # checking if verify is True
    if verify == True:
        print(f'{email} is a valid email address')

    # checking if verify is False
    elif verify == False:
        print(f'{email} is not a valid email address')


Enter fullscreen mode Exit fullscreen mode

In the code snippet, there is a list of email addresses. The for loop is looping through all the email addresses in the list. Inside the for loop, an email is being verified individually.

Running the script, the output will be:

Image description

Final Thoughts

With Python’s versatility, you can build your free email address verifier with a few lines of code, this comes in handy and it’s cheaper than using a premium email verifying service.

Top comments (12)

Collapse
 
cipharius profile image
Valts Liepiņš

Want to point out that fully compliant email syntax validation is too complex to efficiently solve in regexp. Here is example on why it's so:
fightingforalostcause.net/content/...

Collapse
 
sidunn profile image
Si Dunn

Nice project! This will be useful for me. Thanks!

Collapse
 
khumbolamulungu profile image
Khumbo Klein Chilamwa

you are welcome!!!!

Collapse
 
theinfosecguy profile image
Keshav Malik

This is good! Thanks for sharing.

Collapse
 
khumbolamulungu profile image
Khumbo Klein Chilamwa

yo welcome!

Collapse
 
vulcanwm profile image
Medea

this is great! i will definitely use this in my code

Collapse
 
khumbolamulungu profile image
Khumbo Klein Chilamwa

You are welcome, you definetly do that please!!!!!

Collapse
 
devsecbbs_dev profile image
DevSecBBS

that's amazing. the domain example.com exit on ping but the hello username is may wrong & it's been detected as invalid email address. that's seems working exact. will play with that soon...

Collapse
 
marissab profile image
Marissa B

That's pretty cool!

Collapse
 
khumbolamulungu profile image
Khumbo Klein Chilamwa

Thanks!!

Collapse
 
akash_sethi_0c36c30dd4dff profile image
Akash Sethi

This is working fine for me but my aws ec2 ip is getting blocked by spamhawk after verifying 50 emails. I searched the internet and found that I have to use Socks5 proxies but I couldn't find a service provider who is giving SOCKS5 proxies with SMTP enabled.

Can you please help?

Collapse
 
riteshpatel profile image
RITESH PATEL

this code how to use in Django project in email validation?