Posted By

brm on 06/16/15


Tagged

email sysadmin


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

brm


Email verifying


 / Published in: Python
 

check email address whether valid or not by checking list of MX servers and send email command - will work as long as the SMTP server of target domain not be protected / the VRVY not be disabled in MX server.

  1. #!/usr/bin/python
  2. """
  3. Simple method to verify email address through MX servers
  4. need to improve the exception of error message socket
  5. """
  6.  
  7. import smtplib, os, sys, socket
  8.  
  9. if len(sys.argv) < 2:
  10. print "use: " + sys.argv[0] +" + email_address"
  11. exit(1)
  12. email = sys.argv[1]
  13. maildomain = email.split("@")[-1]
  14. nstoken = "mail exchanger = "
  15. mailserver = ""
  16. m = []
  17. print "check mx server ..."
  18. plines = os.popen("nslookup -type=mx " + maildomain).readlines()
  19. for pline in plines:
  20. if nstoken in pline:
  21. mailserver = pline.split(nstoken)[1].strip()
  22. mailserver = mailserver.split(" ")[-1] #no need this line in windows env
  23. m.append(mailserver)
  24.  
  25.  
  26. if mailserver == "":
  27. print "unable to get mx server ...", maildomain
  28. exit(1)
  29. else:
  30. print "found mx mail... ", m
  31. print "checking email address ....", email
  32. for i in m:
  33. socket.setdefaulttimeout(4)
  34. try:
  35. s = smtplib.SMTP(i)
  36. except (socket.timeout, smtplib.SMTPException), e:
  37. print "this mx server time out ", i, e
  38. pass
  39. else:
  40. rep1 = s.ehlo()
  41. if rep1[0] == 250:
  42. rep2 = s.mail("test@test.com")
  43. if rep2[0] == 250:
  44. rep3 = s.rcpt(email)
  45. if rep3[0] == 250:
  46. print email, " valid - mxserver: ", i
  47. elif rep3[0] == 550:
  48. print email, " invalid"

Report this snippet  

You need to login to post a comment.