2

I am trying to verify emails in python, first I'm doing a simple syntax check which works fine but then I go on to check SMTP which works but my IP will get banned if i run my entire dataset through this, is there any way to check without getting my ip banned?

Here is the code:

def check_email(email):
    # Address used for SMTP MAIL FROM command
    fromAddress = 'corn@bt.com'

    # Simple Regex for syntax checking
    regex = '^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$'

    # Email address to verify
    addressToVerify = str(email)

    # Syntax check
    match = re.match(regex, addressToVerify)
    if match == None:
        print('Bad Syntax')
        raise ValueError('Bad Syntax')

    # Get domain for DNS lookup
    splitAddress = addressToVerify.split('@')
    domain = str(splitAddress[1])

    # MX record lookup
    records = dns.resolver.query(domain, 'MX')
    mxRecord = records[0].exchange
    mxRecord = str(mxRecord)
    print(mxRecord)

    # SMTP lib setup (use debug level for full output)
    server = smtplib.SMTP()
    server.set_debuglevel(0)

    # SMTP Conversation
    server.connect(mxRecord)
    server.helo(server.local_hostname)  ### server.local_hostname(Get local server hostname)
    server.mail(fromAddress)
    code, message = server.rcpt(str(addressToVerify))
    server.quit()

    # print(code)
    # print(message)

    # Assume SMTP response 250 is success
    if code == 250:
        return ("True")
    else:
        return ("False")

Thanks

Luke Prior
  • 885
  • 2
  • 11
  • 34

1 Answers1

4

Here is the reference code I've written and it successfully worked for me, you can also try this.

!python setup.py install

Importing DNS lib and getting mail exchanger query:

from dns import resolver

try:
    mx_record = resolver.query('gmail.com', 'MX')

    exchanges = [exchange.to_text().split() for exchange in mx_record]
except (resolver.NoAnswer, resolver.NXDOMAIN, resolver.NoNameservers):
    exchanges = []

Validation, whether e-mail addresses and domain name exist or not!

import re
import smtplib
import dns.resolver
fromAddress = 'user1@testmail.com'
#Simple Regex for syntax checking
regex = '^[_a-z0–9-]+(.[_a-z0–9-]+)@[a-z0–9-]+(.[a-z0–9-]+)(.[a-z]{2,})$'
# Email address to verify
inputAddress = input('Please enter the emailAddress to verify:')
addressToVerify = str(inputAddress)
# # Syntax check
match = re.match(regex, addressToVerify)
if match == None:
print('Bad Syntax')
raise ValueError('Bad Syntax')
# Get domain for DNS lookup
splitAddress = addressToVerify.split('@')
domain = str(splitAddress[1])
print('Domain:', domain)
# MX record lookup
records = dns.resolver.query(domain, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
# SMTP lib setup (use debug level for full output)
server = smtplib.SMTP()
server.set_debuglevel(0)
# SMTP Conversation
mailserver = smtplib.SMTP('mail.gmx.com',587)
server.connect(mxRecord)
server.helo(server.local_hostname) ### server.local_hostname(Get local server hostname)
server.mail(fromAddress)
code, message = server.rcpt(str(addressToVerify))
server.quit()

if code == 250:
    print('Success')
else: 
    print('Bad')
Sam
  • 86
  • 2
  • 10