How To Validate Emails With Python
In this tutorial, I want to go over how you can use a combination of Python and Microsoft Power Bi to validate email addresses. This is a nice tool to make sure your email lists are healthy and lower your bounce rate.
Download Power BI and Python:
At the time of this article, Microsoft Power BI is only valid for Windows so this, unfortunately, will not work with Linux. Head over and download the newest version of Python and download it to your PC. After Python has completed and downloaded you’re going to want to download the newest version of Power BI.
Connect Power Bi and Python:
Within Power Bi as you can see in the image below you’re going to want to click file -> options and settings -> options
Once the options menu pops up you’re going to want to choose Python Scripting in the left hand column. Next under detected pyhton home directories choose the location to the location where Python is downloaded on your computer. Mine is c:/programs files/python38
Now we need to head over and open up our command prompt. Type in the search on your computer cmd and open up your command prompt as admin. Once your command prompt has opened up you’re going to want to type py -m pip install validate_email this will make sure that the email is valid and properly formatted and actually exists. You can learn more about validate_email
Next command we need to import the verification piece. Back in your command prompt type py -m pip install py3DNS Once you have py3DNS installed it will give you the ability to connect to an API to check DNS records on their server.
Power BI Load Emails To Validate Emails With Python
If you’re not familiar with Power BI yet its a pretty simple platform. Open up Power BI and in the top left click Get Data. For this demonstration, I have an excel file in my documents
Once you’ve chosen your selected file with your email addresses you’re going to want to choose to Transform Data not load data.
Now you should see a list of all of your emails that you’re getting ready to validate. The first thing you’re going to want to do is to change the name of the column. Right-click the column and change the name to Email
Script To Validate Emails With Python
Back in Power Bi on the top tabs choose Transform. Once you’re in the Transform tab you’ll see Run R Script and Run Python Script. Click and choose Run Python Script.
Once this opens up delete all the content that is currently in the Python Script Editor. You’re going to wan to replace the text with this simple bit of script to run Validate_Email and Py3DNS
from validate_email import validate_email
dataset[‘Valid Email’] = dataset[‘Email’].apply(lambda x:validate_email(x))
dataset[‘Verified Email’] = dataset[‘Email’].apply(lambda x:validate_email(x,verify=True))