How to Validate Email Address in Python

Check if an email address is correct and really exists using the Python and Real Email


Email Validation in a Nutshell

An Email Address can look right but still be wrong and bounce. Real Email uses in depth email address validation to check if emails really exist without sending any messages.

Checking Formatting

You can use a Regular Expression or the email_validator library to check if an Email Address is formatted correctly. For example using in Python.

import re
email_regex = re.compile(r"[^@]+@[^@]+\.[^@]+")

# "not an email" is invalid so its false.
>>> email_regex.match("not an email") != None
False
# "foo@a" looks like an email, so it passes even though its not real.
>>> email_regex.match("foo@a") != None
False
# "foo@gmail.com" passes, gmail is a valid email server,
# but gmail require more than 3 letters for the address.
>>> email_regex.match("foo@gmail.com") != None
True

You could use it in your code like..

import re
email_regex = re.compile(r"[^@]+@[^@]+\.[^@]+")

if email_regex.match("foo@bar.com"):
print("address is valid")
else:
print("not valid")

But these the Email accounts might not actually exist and if you send an Email to them it might bounce.

Real Email API Validation

To check if an address really exists you can use the Real Email API which does in depth Email Address inspection on the email server. In this example we use the Python Requests library.

import requests

email_address = "foo@bar.com"
response = requests.get(
"https://isitarealemail.com/api/email/validate",
params = {'email': email_address})

status = response.json()['status']
if status == "valid":
print("email is valid")
elif status == "invalid":
print("email is invalid")
else:
print("email was unknown")

You will be able to test 100 emails per day for free. If you have more that that you will need to signup and get an API key. An Email Address might have the unknown status if the email server is unresponsive.

Email Validation Using an API Key

With an API key you wont be limited by how many addresses you can check or how fast you can check them. In Python the example changes to add the API key.

import requests

api_key = "" // todo put your api key here
email_address = "foo@bar.com"
response = requests.get(
"https://isitarealemail.com/api/email/validate",
params = {'email': email_address},
headers = {'Authorization': "Bearer " + api_key })

status = response.json()['status']
if status == "valid":
print("email is valid")
elif status == "invalid":
print("email is invalid")
else:
print("email was unknown")

Bulk Email Address Validation with CSV file

Depending on your use case you may like to use bulk csv file validation and read the CSV file with Python. This is better if you have one big list of emails to check rather than an on going process. First upload your CSV file to Real Email, when it is validated you can read the result file with python like below.

import csv
with open('emails-validated.csv') as csvfile:
reader = csv.DictReader(csvfile)
for row in reader:
print(row)

# {'email': 'foo@bar.com', 'status': 'valid'}

Get started with Real Email validations today.