Validation of email addresses in python is a very crucial task to maintain a valid email list. By verifying the email addresses, enhances your delivery rate and protects your sender’s reputation. So in this tutorial of the python program to check the email address is valid or not, you will learn the complete process of validation in various ways. Let’s dive into this for a better understanding of the topic.

This Tutorial covers how to validate an email address in Python:

Validate email address in Python

In python, validating the email address is very simple with python’s isValidEmail() function. Here, this function checks whether an email entered by the user is in a valid email format (a string of a certain length containing characters followed by an @ symbol followed by characters followed by a “.” followed by more characters) not as the entered email is real or fake.

The below validating email address code in python is actually pretty simple and is very similar to the way you’d do this special task using any other OOP language. Have a look at the following python snippet to see for yourself:

import re
def isValidEmail(email):
 if len(email) > 7:
 if re.match("^.+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$", email) != None:
 return True
 return False
if isValidEmail("my.email@gmail.com") == True :
 print "This is a valid email address"
else:
 print "This is not a valid email address"

This program can be used in any Python project to check whether or not an email is in the proper format. It will need to be modified to be used as a form validator, but you can definitely use it as a jumping-off point if you’re looking to validate email addresses for any form submissions.

Also Check: Using Python to Find The Largest Number Out Of Three

Check if email address valid or not in Python

An email is a string (a subset of ASCII characters) divided into two parts by @ symbol, a “personal_info” and a domain, for instance: personal_info@domain.

In the following code, we are utilizing the re module along with the search() method. Hence, you can find the valid email address using python:

# Python program to validate an Email
 
# import re module
 
# re module provides support
# for regular expressions
import re
 
# Make a regular expression
# for validating an Email
regex = r'\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}\b'
 
# Define a function for
# for validating an Email
 
 
def check(email):
 
    # pass the regular expression
    # and the string in search() method
    if(re.match(regex, email)):
        print("Valid Email")
 
    else:
        print("Invalid Email")
 
 
# Driver Code
if __name__ == '__main__':
 
    # Enter the email
    email = "ankitrai326@gmail.com"
 
    # calling run function
    check(email)
 
    email = "my.ownsite@our-earth.org"
    check(email)
 
    email = "ankitrai326.com"
    check(email)

Output:

Valid Email
Valid Email
Invalid Email

Python Program for Bulk Email Address Validation with CSV file

Based on your use case you may prefer to utilize bulk csv file validation and read the CSV file with Python. One of the best ways to validate the big list of emails rather than an ongoing process. Initially, you have to upload your CSV file to Real Email, when it is validated you can read the result file with python which is shown 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'}

Email Validation Using an 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")

Leave a Reply

Your email address will not be published. Required fields are marked *