How to Verify Email Addresses Using Python
Tags: python, emailverification, automation, programming
Published: 2023-02-17 19:59:25

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](https://pypi.org/project/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](https://pypi.org/project/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:

```python
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')

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

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

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

```python
# calling the email_verifier function
email_verifier(my_email)
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mr2x4mognpwonyby8uzf.png)


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

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bqz84jsstj8nsmsevg8k.png)

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

```python
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')
```
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aqhw1avtqr31hb41r74o.png)


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