Advertisement
Nickpips

Email Checker

Apr 14th, 2016 (edited)
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.91 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. import sys
  4. import imaplib
  5. import getpass
  6. import email
  7. import datetime
  8. import random
  9. import urllib2
  10. import socket
  11. import ssl
  12. import re
  13.  
  14. password = open('password','r').read().strip()
  15.  
  16. urlregex = re.compile(ur'(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:\'".,<>?\xab\xbb\u201c\u201d\u2018\u2019]))')
  17.  
  18. def process_mailbox(M,rate,linkrate):
  19.   endrate = 0
  20.   rv, data = M.search(None, "ALL")
  21.   if rv != 'OK':
  22.       print "No messages found!"
  23.       return
  24.   count = 0
  25.   numEmails = len(data[0].split())
  26.   for num in data[0].split()[::-1]:
  27.       if random.random() < endrate:
  28.           break
  29.       if not random.random() < rate:
  30.           continue
  31.      
  32.       count += 1
  33.      
  34.       rv, data = M.fetch(num, '(BODY[1])')
  35.       if rv != 'OK':
  36.           print "ERROR getting message", num
  37.           return
  38.  
  39.       msg = email.message_from_string(data[0][1])
  40.       msgstr = str(msg).decode('quopri').replace('\n',' ').replace('\r',' ')
  41.       links = urlregex.findall(msgstr)
  42.      
  43.       visited = []
  44.       for a in links:
  45.         if random.random() < linkrate:
  46.             try:
  47.                 site = urllib2.urlopen(a[0], timeout=1)
  48.                 if not 'html' in site.info().getheaders("Content-Type")[0]:
  49.                     raise (ValueError('Not HTML'))
  50.                 html = site.read()
  51.                 visited.append(a[0])
  52.             except (urllib2.HTTPError, ValueError, urllib2.URLError, socket.timeout, ssl.SSLError, IndexError):
  53.                 donothing = 0
  54.  
  55.       print 'MESSAGE: ' + msgstr[0:100]
  56.       print 'LINKS: ' + str(len(links))        
  57.       print 'VISITED: ' + str(len(visited))
  58.      
  59.       rate -= 0.02
  60.       endrate += 0.005
  61.   return numEmails
  62.  
  63. MAIL_SERVERS = {'gmail': 'imap.gmail.com',
  64.                 'yahoo': 'imap.mail.yahoo.com',
  65.                 'aol': 'imap.aol.com',
  66.                 'hotmail' : 'imap-mail.outlook.com'}
  67.  
  68. emails = open('emails','r').read().strip().split('\n')
  69. passwords = open('passwords','r').read().strip().split('\n')
  70.  
  71.  
  72. for i in range(0,len(emails)):
  73.     M = imaplib.IMAP4_SSL(MAIL_SERVERS['yahoo'], 993)
  74.     try:
  75.         M.login(emails[i], passwords[i])
  76.     except imaplib.IMAP4.error:
  77.         print "> LOGIN FAILED"
  78.        
  79.     rv, mailboxes = M.list()
  80.    
  81.     if rv != 'OK':
  82.         print "> LOGIN FAILED"
  83.         continue
  84.        
  85.     rv, data = M.select("INBOX")
  86.    
  87.     if rv == 'OK':
  88.         print '> User: ' + str(i) + ' ' + emails[i]
  89.         print '> Processing mailbox...'
  90.         numEmails = process_mailbox(M,0.7,0.5)
  91.         print '> Finished Processing'
  92.         print '> ' + str(numEmails) + ' email' + ('s' if numEmails != 1 else '') + ' processed'
  93.         print ''
  94.         print ''
  95.         M.close()
  96.     else:
  97.         print "> LOGIN FAILED"
  98.         continue
  99.     M.logout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement