How to validate email with Python
There are dozens of ways and types of email verification, and you know that. To validate email address all you need is a basic knowledge of the SMTP protocol and command of one of the programming languages. Take Python, for example, a popular, functional and object-oriented language. It offers great opportunities in web development (Django, Pyramid, Flask), protocols (HTML, XML, JSON), software development, business apps, games, and more. We can use it, too, within iLead verifier to validate email with Python. For this, we can write a simple Python script, as well as you or your IT team can do for your projects/website/software.
That’s what we’re going to show you in a moment, but few disclaimers first:
- This script is strictly for Python version 3, or Py3K;
- It requires “one-at-a-time” email insertion, you can do it multiple times, though there’s a monthly limit for free checks at iLead;
- Not recommended to utilize it too often due to risk of getting your IP blacklisted or flagged;
- It does not guarantee 100% accurate results, it is rather good for verifying email format.
How to validate email with Python & iLead
This Python email validation script is based on isValidEmail() function and curl command. Curl for Python requests is a tool for data transferring: for basics, here is how to use curl command. The code for iLead API to validate email addresses is, in fact, quite simple as you may see see from a snippet below. It is similar to any other task within an OOP language.
As we said, this script is mostly to validate email format, and not so much as to determine if an email actually exists or not. It will only check whether the email entered is in a valid format or not. Proper email format implies a line of characters followed by an @ symbol, again followed by characters, followed by a dot (“.”), and followed by characters.
In other words, it checks if email looks like firstname.lastname@example.org and returns OK result, if it is something like email.test.com, it returns Error response. The script does not check either DNS/MX records, or mailbox existence (mail from – rcpt to).
Within iLead verifier service, the OK response usually comes with the code 200, while error responses are marked by 400. And that’s your email verification with Python.
When format is wrong and you get error response, there is an additional step to try to inspect an email further, BUT only for Linux OS. Add two lines of code to use Python for email validation:
This way, you can get more detailed results about email validity – including format, syntax, MX records, SMTP connection. Results are displayed on a rate from 0 to 10, where 0 is invalid and 10 – 100 per cent valid. Visualized, it would look similar to online email verification tab, as shown here:
P.S. This Python script is just one of the ways to perform email validation for you business, go to Validate emails with jQuery to learn more. For info about iLead API, go to Help page. If you have questions or other inquiries regarding email validation, don’t hesitate to contact us.