Check if an email exists in Python with our email validation api.
Email Check Python Style!
An Email Address can look right but still be wrong and bounce. Our email validation platform 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
If you did a Google Search for check if email exists python then you’ve come to the right place. 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.
API Validation
To check if an address really exists you can use the API which does in depth Email Address inspection on the email server. In this example we use the Python Requests library.
import requests
api_key = '' # Generated in your User Profile it shows at the top in a green bar once
team_slug = "" # when you sign up you have a team, its in the URL then use that
email_address = "test@test.com" # the test email
response = requests.post(
"https://app.mailvalidation.io/a/" + team_slug + "/validate/api/validate/",
json={'email': email_address},
headers={
'content-type': 'application/json',
'accept': 'application/json',
'Authorization': 'Api-Key ' + api_key,
},
)
valid = response.json()['is_valid']
if valid:
print("Valid")
else:
print("Invalid")
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 MailValidation.io via your app account, 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'}