Automating Email Domain Verification with Python
When managing a list of email addresses, it’s often useful to know which ones are using Google Workspace (formerly G Suite) for their email services. This can be valuable information for various purposes, such as targeting specific email providers for marketing campaigns or ensuring email deliverability.
In this article, we’ll walk you through how to use Python to automate the process of checking whether email addresses in a CSV file are hosted on Google Workspace. We’ll then add this information to a new column in the CSV file and save the results in a new CSV file.
Prerequisites
Before we begin, make sure you have the following:
- Python installed on your system.
- The
dnspython
library installed, which is used for DNS lookups. You can install it using pip:
pip install dnspython
The Python Script
Let’s break down the Python script step by step:
import csv
import dns.resolver
# Function to check if the domain is using Google Workspace
def is_google_workspace_domain(domain):
try:
mx_records = dns.resolver.query(domain, 'MX')
for mx in mx_records:
if "google.com" in str(mx.exchange):
return "Yes"
return "No"
except dns.resolver.NXDOMAIN:
return "No"
In this part of the script, we define a function is_google_workspace_domain
that takes a domain name as input and checks whether it uses Google Workspace. It does this by querying the DNS MX (Mail Exchanger) records of the domain and looking for any records pointing to Google's servers.
# Create argument parser
parser = argparse.ArgumentParser(description="Check if email addresses are hosted on Google Workspace.")
# Add input and output file arguments
parser.add_argument("input_file", help="Path to the input CSV file")
parser.add_argument("output_file", help="Path to the output CSV file")
# Parse command-line arguments
args = parser.parse_args()
This part of the script sets up command-line argument parsing using the argparse
library. It allows you to specify the input and output CSV file names when running the script from the command line.
# Open the input CSV file for reading and the output CSV file for writing
with open(args.input_file, 'r', newline='') as input_file, \
open(args.output_file, 'w', newline='') as output_file:
Here, we open the specified input and output CSV files using context managers. This ensures that the files are properly closed after we’re done with them.
# Create CSV reader and writer objects
csv_reader = csv.reader(input_file)
csv_writer = csv.writer(output_file)
# Read the header row from the input file
header = next(csv_reader)
# Add a new column header for Google Workspace status
header.append("Google Workspace")
# Write the updated header to the output file
csv_writer.writerow(header)
These lines create CSV reader and writer objects, read the header row from the input CSV file, and add a new column header for the Google Workspace status. The updated header is then written to the output CSV file.
# Iterate through the rows in the input CSV
for row in csv_reader:
email = row[0] # Assuming the email address is in the first column
domain = email.split('@')[1]
google_workspace_status = is_google_workspace_domain(domain)
# Add the Google Workspace status to the row
row.append(google_workspace_status)
# Write the updated row to the output file
csv_writer.writerow(row)
print(f"Results written to {args.output_file}")
Finally, we iterate through the rows in the input CSV, extract the email domain, check if it uses Google Workspace, add this information to the row, and write the updated row to the output CSV file. The script concludes by printing a message indicating where the results have been written.
Complete Script
import argparse
import csv
import dns.resolver
# Function to check if the domain is using Google Workspace
def is_google_workspace_domain(domain):
try:
mx_records = dns.resolver.query(domain, 'MX')
for mx in mx_records:
if "google.com" in str(mx.exchange):
return "Yes"
return "No"
except dns.resolver.NXDOMAIN:
return "No"
# Create argument parser
parser = argparse.ArgumentParser(description="Check if email addresses are hosted on Google Workspace.")
# Add input and output file arguments
parser.add_argument("input_file", help="Path to the input CSV file")
parser.add_argument("output_file", help="Path to the output CSV file")
# Parse command-line arguments
args = parser.parse_args()
# Open the input CSV file for reading and the output CSV file for writing
with open(args.input_file, 'r', newline='') as input_file, \
open(args.output_file, 'w', newline='') as output_file:
# Create CSV reader and writer objects
csv_reader = csv.reader(input_file)
csv_writer = csv.writer(output_file)
# Read the header row from the input file
header = next(csv_reader)
# Add a new column header for Google Workspace status
header.append("Google Workspace")
# Write the updated header to the output file
csv_writer.writerow(header)
# Iterate through the rows in the input CSV
for row in csv_reader:
email = row[0] # Assuming the email address is in the first column
domain = email.split('@')[1]
google_workspace_status = is_google_workspace_domain(domain)
# Add the Google Workspace status to the row
row.append(google_workspace_status)
# Write the updated row to the output file
csv_writer.writerow(row)
print(f"Results written to {args.output_file}")
Running the Script
To run the script, open your terminal or command prompt and use the following command:
python script.py input.csv output.csv
Replace script.py
with the name of your Python script and provide the input CSV file and output CSV file as command-line arguments.
Conclusion
Automating email domain verification using Python can save you time and help you make informed decisions when dealing with email lists. This script is a practical example of how Python can be used for such tasks. You can further customize it to suit your specific needs.
Remember to handle email addresses and data in compliance with privacy and data protection regulations, and only use this script for legitimate and ethical purposes.
Feel free to adapt and improve upon this script to fit your requirements. Happy coding!