Is there any way to verify an email whether the email actually exist or no in python? Or is thee any platform offering such services? For Example:
I have some emails
How can I be 100% sure which of the following email really exist on the internet?
CodePudding user response:
You can send a verification email with a code to the entered email to verify its presence. This can be done using any python framework. Django, flask, etc.
CodePudding user response:
You can use gmass.co, connect to your google account , go to settings , apikeys , then create an api key and use it this way :
sent = requests.get('https://verify.gmass.co/verify?email=' email '&key=' api,
headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.141 Safari/537.36'}
)
if ("valid" in sent.content):
print(email "valid")
else:
print(email "invalid")
CodePudding user response:
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 ='[email protected]' 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('[email protected]') 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:
This library focuses strictly on the domain part of an email address, and checks if it’s in an [email protected] 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.
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.
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.