0

The given script is not verifying some email addresses correctly.

for example

himanshu.jain@traveldglobe.com

jobbuzz@timesjobs.com

are not valid according to my script but I verified these on leopathu.com/verify-email which verifying them correctly.

import re
import dns.resolver
import socket
import smtplib
def email_verifier(email):

    match = re.match(r'([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5})$', email)
    if(match == None):
        return "Invalid Email."
    else:
        splitEmail = email.split('@')
        domain = str(splitEmail[1])


        records = dns.resolver.query(domain, 'MX')
        mxRecord = records[0].exchange
        mxRecord = str(mxRecord)
        # 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('sender@gmail.com')
        code, message = server.rcpt(str(email))
        server.quit()

       #returns 250 if valid
        if code == 250:
            return ('Valid Email Address')
        else:
            return ('invalid Email Address')

email = input("Enter email address : ")
print(email_verifier(email))

What to do to make it more accurate?

brainLoop
  • 3,810
  • 2
  • 10
  • 13

1 Answers1

0

Both addresses fail the script on my machine:

[cam@tindall ~]$ python3 verify-email.py
Enter email address : jobbuzz@timesjobs.com
invalid Email Address. Error: 552
b'5.2.2 The email account that you tried to reach is over quota. Please direct the recipient to 5.2.2  https://support.google.com/mail/?p=OverQuotaPerm d3-v6si12074514edj.156 - gsmtp'              

[cam@tindall ~]$ python3 verify-email.py
Enter email address : himanshi.jain@traveldglobe.com
invalid Email Address. Error: 550
b'5.1.1 <himanshi.jain@traveldglobe.com> User unknown'

The first error looks like a real error (that user has a full mailbox), but the second failure likely means that the test "mail" from my machine has been blocked based on some kind of spam heuristic. This will vary from connection to connection, and will fail on most nondescript cloud boxes (i.e. ec2 instances or DO droplets you spun up an hour ago) and residential connections.

Not all nodes on the global email graph are created equally, and mail that is deliverable from a trustworthy mail host who has sent lots of reputable mail in the past, is connecting over SSL and has all its DMARC ducks in order, etc will be treated much differently by real-world mail servers than a barely-compliant Python script calling up via a Comcast connection and claiming to be "sender@gmail.com".

If, as I suspsect, you are trying to "clean" an email list that you intend to use for "bulk mail" (wink wink), then you are best off doing these kind of checks using a setup as close to your ultimate sending setup as possible. In other words, test with the actual sender address, using the actual machine, IP address, etc. that you intend to send the campaign with. This of course won't be possible if you are trying to clean the list for use with a service like Mailchimp or Convertkit. Unfortunately, that's life for someone operating at the edges of polite society and doing things of questionable legality.

If you're interested in going half-straight, you can send a verification link to your link, and drop anybody who doesn't follow it. This will of course decimate the list, but you will probably end up with a more focused audience and a list that is safe to use on Mailchimp et al.

If you're simply trying to verify that a given user actually controls a given address (seems unlikely given the context here), then of course you need to send that user an email with a verification token. Only by actually sending mail to someone can you verify in totality whether an address is 100% legit. If that's your plan, I have a simple service to let you add such functionality to your site without maintaining any of this tedious mail infrastructure. More details can be found at http://clicktoverify.net/.

C. Tindall
  • 413
  • 3
  • 8