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

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

Related

Why am I getting a timeout [WinError 10060] error when sending an email via smtplib?

I am sending the following email:
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.message import EmailMessage
# Open the plain text file whose name is in textfile for reading.
with open(r'C:\Users\David\Documents\Hello.txt') as fp:
# Create a text/plain message
msg = EmailMessage()
msg.set_content(fp.read())
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'Enquiry'
msg['From'] = "example1#hotmail.co.uk"
msg['To'] = "example2#hotmail.co.uk"
# Send the message via our own SMTP server.
s =server = smtplib.SMTP('smtp.live.com', 587)
s.send_message(msg)
s.quit()
When I send the message I get the following error:
TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
I am not sure why this is. Does anyone have any ideas?
I've masked the original email addresses.
No localhost had been set up. This did the trick!:
# Import smtplib for the actual sending function
import smtplib
# Import the email modules we'll need
from email.message import EmailMessage
# Open the plain text file whose name is in textfile for reading.
with open(r'C:\Users\David\Documents\Hello.txt') as fp:
# Create a text/plain message
msg = EmailMessage()
msg.set_content(fp.read())
# me == the sender's email address
# you == the recipient's email address
msg['Subject'] = 'yes'
msg['From'] = "example1#hotmail.com"
msg['To'] = "example2#hotmail.com"
# Login
s =server = smtplib.SMTP('smtp.office365.com', 587)
s.starttls()
s.login('example1#hotmail.com',"password")
# Sending the message
s.send_message(msg)
s.quit()
pip install wget
just install a library and go on. I was facing the same issue and I solve it by installing it.

Issue in Sending mail in Bulk quantity in Python

I am trying to create a bulk email sender. I want the script to open form.csv that contains multiple email addresses, and send the email to them.
Problem is after sending 75 to 80 mails its give an error, total no of mail is (1000)
Simply, I need help to send more and sends the email to those addresses.
below is the src code its working fine.enter code here
import csv, smtplib, ssl
port = 587
smtp_server = "smtp.gmail.com"
login = "example#gmail.com" # paste your login
password = "jjshajcaez" # paste your password
message = """Subject: Happy New Year
To: {recipient}
From: {sender}
Dear {name},
We wish you and your family a very Happy New Year!
With warm regards, """
sender = "example#gmail.com"
context = ssl.create_default_context()
with smtplib.SMTP(smtp_server, port) as server:
server.connect('smtp.gmail.com', '587')
server.ehlo() # Can be omitted
server.starttls(context=context)
server.ehlo() # Can be omitted
server.login(login, password)
with open("form.csv") as file:
reader = csv.reader(file)
next(reader) # it skips the header row
for name, email in reader:
server.sendmail(
sender,
email,
message.format(name=name, recipient=email, sender=sender)
)
print(f'Sent to {name}')
form.csv
#name, email
Test, test#gmail.com
is there any suggestion how to modify the code... so that it can send mail at once and store the log file on the system.

Check if an email address exists or not in 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))

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.

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

Categories

Resources