简体   繁体   中英

python: correct method verify if email exists

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 = test@cisco.com
    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('myemail@gmail.com')
    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:

  • Is there a right way to do this type of email validation, without getting marked as spam? Is it getting marked as spam as I am running the code on my system and just giving a dummy value for email like

server.mail('myemail@gmail.com')

  • Is it possible to use some proxy to do this? My usecase require 100's of email addresses to be verified. I see some commercial api available for email validation, but it is not feasible for me at the moment.

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='example@example.com', check_regex=True, check_mx=True, from_address='my@from.addr.ess', helo_host='my.host.name', smtp_timeout=10, dns_timeout=10, use_blacklist=True, debug=False)

Installation:

pip3 install py3-validate-email

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)

Try this.

pip install validate_email

from validate_email import validate_email
is_valid = validate_email('example@example.com', verify=True)

Visit https://pypi.org/project/validate_email/ for more info.

Why don't you send an actual email? Que up 100 or so unique emails from different ips and send an actual email. Vary the headers, subject line and message using spintax and rate limit to under 8 an hour per email and you should get scale without problems. Spamhaus will automatically flag you for sending from a fake account and being over the limit ... which is around 8 / hour for unseasoned email accounts. Don't put sales related messages in the emails you generate either.

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 = "foo@bar.com"
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")

https://github.com/Gardener-gg/email-verifier比 py3-validate-email 更具体

I found a way to check if the email exists. I use the Real Email API. I offer you a simple script on the basis of which you can continue to act

import requests

email_address = str(input('Email: '))
response = requests.get(
    "https://isitarealemail.com/api/email/validate",
    params = {'email': email_address})

status = response.json()['status']
if status == "valid":
  print("email is valid")
elif status == "invalid":
  print("email is invalid")
else:
  print("email was unknown")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM