How to do it...

Listing 5.8 shows us the code that finds the domain name from the given email address and verifies it:

#!/usr/bin/env python 
# Python Network Programming Cookbook, Second Edition -- Chapter - 5 
# This program is optimized for Python 2.7.12 and Python 3.5.2. 
# It may run on any other version with/without modifications. 
 
import re 
import smtplib 
import dns.resolver 
import argparse 
 
 
def mail_checker(fromAddress, toAddress): 
 
    regex = '^[a-z0-9][a-z0-9._%+-]{0,63}@[a-z0-9-]+
(.[a-z0-9-]+)*(.[a-z]{2,})$' addressToVerify = str(toAddress) match = re.match(regex, addressToVerify) if match == None: print('Bad Syntax in the address to verify.
Re-enter the correct value') raise ValueError('Bad Syntax') splitAddress = addressToVerify.split('@') domain = str(splitAddress[1]) records = dns.resolver.query(domain, 'MX') mxRecord = records[0].exchange mxRecord = str(mxRecord) server = smtplib.SMTP() server.set_debuglevel(1) try: server.connect(mxRecord) except Exception as e: print ("Mail Check Failed Due to Error: %s" %str(e)) return server.helo(server.local_hostname) server.mail(fromAddress) code, message = server.rcpt(str(addressToVerify)) server.quit() if code == 250: print('Successfully verified the email: %s', fromAddress) else: print('Failed to verify the email: %s', fromAddress) if __name__ == '__main__': parser = argparse.ArgumentParser(description='Mail Server Example') parser.add_argument('--fromAddress', action="store",
dest="fromAddress", type=str, required=True) parser.add_argument('--toAddress', action="store",
dest="toAddress", type=str, required=True) given_args = parser.parse_args() mail_checker(given_args.fromAddress, given_args.toAddress)

If you run this recipe, you will see the following output:

$ python 5_8_verify_email.py [email protected] 
[email protected]
connect: ('alt2.gmail-smtp-in.l.google.com.', 25) connect: ('alt2.gmail-smtp-in.l.google.com.', 25) Successfully verified the email: [email protected]

The preceding output is printed when 250 is returned from the mail server. The program may also produce the following output, since mail servers such as Gmail block outside access to port 25, which is the port of Simple Mail Transfer Protocol (SMTP) that we are checking:

$ python 5_8_verify_email.py [email protected] [email protected]
connect: ('alt2.gmail-smtp-in.l.google.com.', 25)
connect: ('alt2.gmail-smtp-in.l.google.com.', 25)
Mail Check Failed Due to Error: [Errno 101] Network is unreachable
  

If you use an invalid domain name as follows, there will be an error message:

$ python 5_8_verify_email.py [email protected] [email protected]
Traceback (most recent call last):
  File "5_8_verify_email.py", line 57, in <module>
    mail_checker(given_args.fromAddress, given_args.toAddress)
  File "5_8_verify_email.py", line 26, in mail_checker
    records = dns.resolver.query(domain, 'MX')
  File "/usr/local/lib/python2.7/dist-packages/dns/resolver.py", line 1132, in query
    raise_on_no_answer, source_port)
 File "/usr/local/lib/python2.7/dist-packages/dns/resolver.py", line 1051, in query
   raise NXDOMAIN(qnames=qnames_to_try, responses=nxdomain_responses)
dns.resolver.NXDOMAIN: None of DNS query names exist: slt.lxx., slt.lxx.
  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.101.81