13

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.
Sam Rohn
  • 359
  • 2
  • 5
  • 13
  • 1
    were you able to find any solution? – r4v1 Apr 18 '19 at 09:11
  • 1
    There is no good way to do this for the very simple reason that if the world's mailservers were to respond to such queries, they would be so overwhelmed by spammers that they would have no capacity to do useful work. – BoarGules Dec 07 '20 at 21:45

5 Answers5

13

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
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
3

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)
Ali Najafi
  • 85
  • 1
  • 11
1

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")
Stephen
  • 4,228
  • 4
  • 29
  • 40
0

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")
-2

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.

Pgarr
  • 66
  • 2
  • 4
  • 4
    This package also uses the same method. In fact I tried with this package. it doesn't work at all. https://github.com/syrusakbary/validate_email/issues/89 some one has pointed out the same – Sam Rohn Nov 30 '18 at 16:41
  • 1
    This package doesn't work; it also doesn't return anything on timeout or error which means if something goes wrong there isn't any way of knowing – cuuupid Jun 18 '19 at 05:46
  • so what did you guys used? – chip Sep 18 '19 at 05:19
  • Works only for gmail – aac Jan 10 '20 at 10:34
  • This package hasn't been maintained since 2015! And is a no-go for 2020! and it wasn't working in the first place either – Mohammad Moallemi Dec 28 '20 at 23:16