I am trying to verify if an email actually exists by first resolving its dns, then check if the email is valid using the below code:
email = [email protected]
domain = email.split("@")[-1]
records = dns.resolver.query(domain, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
server.connect(mxRecord)
server.helo(host)
server.mail('[email protected]')
code, message = server.rcpt(str(email))
server.quit()
if code == 250:
print('valid email', message)
else:
print('invalid email', message)
This works few times, but when I send multiple request I get a message like:
"5.7.1 Service unavailable, Client host [122.166.xxx.xxx] blocked using Spamhaus. To request removal from this list see http://www.spamhaus.org/lookup.lasso (AS160312312) [BL2NAM02FT12312.eop-nam02.prod.protection.outlook.com]'"
I understand that they are trying to block my ip address as it thinks its spammy.
Here are my questions:
server.mail('[email protected]')
Double opt-in method The most popular technique used by companies to test email validity is probably double opt-in.
It's time to test out the app. Make sure that Flask is running on your terminal with flask run . Visit http://localhost:5000/ and enter your personal email address. Check your email to see an email from "[email protected]" and find the verification code provided by Twilio Verify.
As of 2021, the most updated python3 package I could find was py3-validate-email
Basic Usage:
from validate_email import validate_email
is_valid = validate_email(email_address='[email protected]', check_regex=True, check_mx=True, from_address='[email protected]', helo_host='my.host.name', smtp_timeout=10, dns_timeout=10, use_blacklist=True, debug=False)
Installation:
pip3 install py3-validate-email
yes you will quite likely get marked as spam. If you want to avoid that you'll need to put in a bit of work fighting the anti-spam measures.
You could consider using a service like Real Email to do the validations. eg
import requests
api_key = "" // todo put your api key here
email_address = "[email protected]"
response = requests.get(
"https://isitarealemail.com/api/email/validate",
params = {'email': email_address},
headers = {'Authorization': "Bearer " + api_key })
status = response.json()['status']
if status == "valid":
print("email is valid")
elif status == "invalid":
print("email is invalid")
else:
print("email was unknown")
This method in dnslib
is not suitable for bulk email validation.
because smtp server blocks you if you send a lot of email validation request.
then you should use proxy via pysocks
library. You can also see this post on medium:
import socket
import socks # PySocks
from smtplib import SMTP
class SocksSMTP(SMTP):
def __init__(self,
host='',
port=0,
local_hostname=None,
timeout=socket._GLOBAL_DEFAULT_TIMEOUT,
source_address=None,
proxy_type=None,
proxy_addr=None,
proxy_port=None,
proxy_rdns=True,
proxy_username=None,
proxy_password=None,
socket_options=None):
self.proxy_type=proxy_type
self.proxy_addr=proxy_addr
self.proxy_port=proxy_port
self.proxy_rdns=proxy_rdns
self.proxy_username=proxy_username
self.proxy_password=proxy_password
self.socket_options=socket_options
# if proxy_type is provided then change the socket to socksocket
# else behave like a normal SMTP class.
if self.proxy_type:
self._get_socket = self.socks_get_socket
super(SocksSMTP, self).__init__(host, port, local_hostname, timeout, source_address)
def socks_get_socket(self, host, port, timeout):
if self.debuglevel>0:
self._print_debug('connect: to', (host, port), self.source_address)
return socks.create_connection((host, port),
timeout=timeout,
source_address=self.source_address,
proxy_type=self.proxy_type,
proxy_addr=self.proxy_addr,
proxy_port=self.proxy_port,
proxy_rdns=self.proxy_rdns,
proxy_username=self.proxy_username,
proxy_password=self.proxy_password,
socket_options=self.socket_options)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With