There are several tutorials on the internet. Here's what I found...
First you need to check for the correct formatting and for this you can use regular expressions like this:
import re
addressToVerify ='info@scottbrady91.com'
match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$', addressToVerify)
if match == None:
print('Bad Syntax')
raise ValueError('Bad Syntax')
DNS
Next we need to get the MX record for the target domain, in order to start the email verification process. Note that you are allowed in the RFCs to have a mail server on your A record, but that's outside of the scope of this article and demo script.
import dns.resolver
records = dns.resolver.query('scottbrady91.com', 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
Python DNS Python doesn't have any inbuilt DNS components, so we've
pulled in the popular dnspython library. Any library that can resolve
an MX record from a domain name will work though.
Mailbox
Now that we have all the preflight information we need, we can now find out if the email address exists.
import socket
import smtplib
# Get local server hostname
host = socket.gethostname()
# SMTP lib setup (use debug level for full output)
server = smtplib.SMTP()
server.set_debuglevel(0)
# SMTP Conversation
server.connect(mxRecord)
server.helo(host)
server.mail('me@domain.com')
code, message = server.rcpt(str(addressToVerify))
server.quit()
# Assume 250 as Success
if code == 250:
print('Success')
else:
print('Bad')
What we are doing here is the first three commands of an SMTP conversation for sending an email, stopping just before we send any data.
The actual SMTP commands issued are: HELO, MAIL FROM and RCPT TO. It is the response to RCPT TO that we are interested in. If the server sends back a 250, then that means we are good to send an email (the email address exists), otherwise the server will return a different status code (usually a 550), meaning the email address does not exist on that server.
And that's email verification!
source: https://www.scottbrady91.com/Email-Verification/Python-Email-Verification-Script
Here are some other alternatives from another website...
Let’s make it more sophisticated and assume we want the following criteria to be met for an account@domain email address:
- Both consist of case-insensitive alphanumeric characters. Dashes,
periods, hyphens or underscores are also allowed
- Both can only start and end with alphanumeric characters
- Both contain no white spaces account has at least one character and
domain has at least two domain includes at least one period
- And of course, there’s an ‘@’ symbol between them
^[a-z]([w-]*[a-z]|[w-.]*[a-z]{2,}|[a-z])*@[a-z]([w-]*[a-z]|[w-.]*[a-z]{2,}|[a-z]){4,}?.[a-z]{2,}$
Validating emails with Python libraries
Another way of running sophisticated checks is with ready-to-use packages, and there are plenty to choose from. Here are several popular options:
email-validator 1.0.5
This library focuses strictly on the domain part of an email address, and checks if it’s in an x@y.com format.
As a bonus, the library also comes with a validation tool. It checks if the domain name can be resolved, thus giving you a good idea about its validity.
pylsEmail 1.3.2
This Python program to validate email addresses can also be used to validate both a domain and an email address with just one call. If a given address can’t be validated, the script will inform you of the most likely reasons.
py3-validate-email
This comprehensive library checks an email address for a proper structure, but it doesn’t just stop there.
It also verifies if the domain’s MX records exist (as in, is able to send/receive emails), whether a particular address on this domain exists, and also if it’s not blacklisted. This way, you avoid emailing blacklisted accounts, which would affect deliverability.
source: https://mailtrap.io/blog/python-validate-email/
These are just extracts from these websites, make sure to visit them and confirm this can be useful for your specific case, they also include much more methods and explain this in more detail, hope it helps.