Check if an email address exists or not in python - python

I would like to check if an email address exists or not in python 2.7.I used the SMTP server but it does not return any result.
Does anyone have an idea to check if an email address exists or not?
try:
email_address = 'me#exemple.com'
domain_name = email_address.split('#')[1]
records = dns.resolver.query(domain_name, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
#Step 3: ping email server
#check if the email address exists
# 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()
print(code)
# Assume 250 as Success
if code == 250:
print('Y')
else:
print('N')
except Exception as error:
print('Erreur:'+repr(error))

Related

python script to check if a email address is valid.?

I am trying to validate a email. What i tought of is to basically read the messages connecting to this mailbox. and i have followed this blow https://www.scottbrady91.com/email-verification/python-email-verification-script
Below is my script:
import socket
import smtplib
import dns.resolver
email_address = 'dummy#cisco.com'
#Pull domain name from email address
domain_name = email_address.split('#')[1]
#get the MX record for the domain
my_resolver = dns.resolver.Resolver()
records = my_resolver.resolve(domain_name, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
# ping email server
#check if the email address exists
# 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('test#zozo.com')
code, message = server.rcpt(str(addressToVerify))
server.quit()
if code == 250:
print('Yes')
else:
print('N')
the thing is every email is returned as valid until the domain is valid
any help is appriciated

Blocked by Spamhaus When Trying to Validate Email Using Python

I'm trying to check whether an email exists or not using Python's smtplib
This is what I did:
s = smtplib.SMTP()
s.connect(mxRecord)
s.mail('my#email.com') //Here the error shows up
The error is:
Client host [...] blocked using Spamhaus. To request removal from this list see http://www.spamhaus.org/lookup.lasso (S3130)
I tried something and it worked well.
import dns.resolver, smtplib
MyEmail = "X#hotmail.com"
MyPassword = "XXX"
EmailToValidate = "X#Y.com"
record = dns.resolver.query(str.split(EmailToValidate, "#")[1], "MX")
mx = str(record[0].exchange)
server = smtplib.SMTP("smtp.live.com", 587)
server.ehlo()
server.starttls()
server.ehlo()
server.login(MyEmail, MyPassword)
server.helo("live.com")
server.connect(mx)
server.mail(MyEmail)
code, msg = server.rcpt(EmailToValidate)
print("Code: ", str(code), " message: ", msg)
the (code, message) pair will be (250, OK) if the email exists, and (550, Address Rejected) if the email does not exists.
I wrote this on hurry, so there might be some unnecessary steps.

Python 3 Mass Email Verification Connection Refused

Im having trouble verifying a mass list of email addresses. The issue is with the error code "connection refused" Every email verification returns Connection refused. Why would this be? Can you give me a solution? No issues with verifying the email syntax
The below code is only for the correct area of the programme, i.e mxrecord check. Im using intellij Idea, python 3.4.3 and tkinter for the GUI.
def handle_accuracy(self):
for email in self.emails:
# See if email string contains ampersand and period.
if (email.find('#') < 0) or (email.find('.') < 0):
print("Email not syntactically correct.")
self.inaccurate_emails.append(email)
else:
email_exists = self.check_existence(email)
if email_exists:
print("Email is accurate and exists.")
self.accurate_emails.append(email)
else:
print("Email is syntactically correct but couldn\'t be found")
self.inaccurate_emails.append(email)
def check_existence(self, email):
at_pos = email.find('#')
mx_name = email[(at_pos + 1):]
# Connect to email server and get name of SMTP server
records = dns.resolver.query(mx_name, 'MX')
for record in records:
print(record.exchange)
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
host = socket.gethostname()
# Setup an exception block to handle issues with connection.
try:
server = smtplib.SMTP(mxRecord)
except TimeoutError:
print("Timeout")
# Indicate to calling function that email cannot be found.
return False
except ConnectionRefusedError:
print("Connection Refused")
return False
server.set_debuglevel(0)
# Setup another exception block to handle further issues with connection.
try:
server.connect()
server.helo(host) #needs to have a helo rather than hello
server.mail(email)
code, message = server.rcpt()
except TimeoutError:
print("Timeout")
server.quit()
return False
except ConnectionRefusedError:
print("Connection Refused")
server.quit()
return False
server.quit()
if code == 250:
return True
else:
return False
Thanks in advance.

How to Verify an Email Address in Python Using smtplib and MX record

I am using this code to validate email. It runs at the first few validation, but soon it shows an error OSError: [Errno 101] Network is unreachable when I am running it frequently. Please help! Thanks
def validate_email(email):
import dns.resolver
import socket
import smtplib
splitAddress = email.split('#')
domain = str(splitAddress[1])
records = dns.resolver.query(domain, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
host = socket.gethostname()
server = smtplib.SMTP()
server.set_debuglevel(0)
server.connect(mxRecord)
server.helo(host)
server.mail('me#domain.com')
code, message = server.rcpt(str(email))
server.quit()
time.sleep(1)
return code

sending email from localhost works but fails when run on remote linux server

Following is a simple python script to send emails using gmail smtp server. This code works from my localhost in my windows machine but when i run the same code from a remote linux server, "socket.error: [Errno 97] Address family not supported by protocol" is thrown and the exception happens at this line " server = smtplib.SMTP('smtp.gmail.com:587')". I appreciate any help on what is causing this error. Thanks!
fromaddr = ''
toaddrs = ''
# Credentials (if needed)
username = ''
password = ''
server = smtplib.SMTP('smtp.gmail.com:587')
server.starttls()
server.login(username,password)
msg = MIMEText('This is a test message')
server.sendmail(fromaddr, toaddrs, msg.as_string())
server.quit()

Categories

Resources