Reputation: 15
I have a huge list of email ids and I have to filter out all valid email ids , valid means really exist or not .
I am trying to implement smtplib
for this but I am not sure about the right way. It gives random result for same email id, which is not exist (I have checked manually ) but it's showing sometimes exist and sometimes does not exit.
Is there any better or working method available ?
import socket
import smtplib
import dns.resolver
def get_connection_handler():
try:
# Get local server hostname
# host = socket.gethostname()
host = socket.getfqdn()
username = '[email protected]' #used with working email_id
password = 'password' #password
# SMTP lib setup (use debug level for full output)
server = smtplib.SMTP('smtp.gmail.com', 587, timeout=30)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(username, password)
return host, server
except Exception as e:
return None, None
def validate_email_id(server, host, email_list):
for each_email_id in email_list:
records = dns.resolver.query(each_email_id.split('@')[-1], 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
# SMTP Conversation
server.connect(mxRecord)
server.helo(host)
server.mail('[email protected]')
addressToVerify = '[email protected]'
code, message = server.rcpt(str(addressToVerify))
email_list = ['[email protected]','[email protected]','[email protected]','[email protected]','[email protected]',]
server,host = get_connection_handler()
validate_email_id(server,host,email_list)
It gives random result for same email_id
, sometimes exist and sometimes does not.
Is there any other better way to do this ?
Upvotes: 1
Views: 347
Reputation: 898
The validate email python package could help you in this situation. According to the documentation, it is used to check whether an email is "valid, properly formatted and really exists."
After installing using pip install validate_email
, you could use it like this:
from validate_email import validate_email
is_valid = validate_email('[email protected]')
Which returns true
if the email is valid, false
if not.
Upvotes: 1
Reputation: 1375
You can check if syntax is correct, just lookup the RFC, read it and run! The only sane check is really [email protected]. So vaildate with a package like @Nick Predey suggested or only check for @ and ..
The second part of your question is interesting. There is no method anymore to check if the e-mailaddress actually exists. In the early days there was finger to test if a user existed on a host. Nowadays that protocol isn't used anymore in the fight against spam (https://en.wikipedia.org/wiki/Finger_protocol)
Upvotes: 1