Advertisement
Nickpips

E-mail Checker

Mar 26th, 2016
1,109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 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. print ''
  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. print ''
  59.  
  60. rate -= 0.02
  61. endrate += 0.005
  62.  
  63. MAIL_SERVERS = {'gmail': 'imap.gmail.com',
  64. 'yahoo': 'imap.mail.yahoo.com',
  65. 'aol': 'imap.aol.com'}
  66.  
  67. M = imaplib.IMAP4_SSL(MAIL_SERVERS['yahoo'])
  68. try:
  69. M.login('npipitone1234@yahoo.com', password)
  70. except imaplib.IMAP4.error:
  71. print "> LOGIN FAILED"
  72.  
  73. rv, mailboxes = M.list()
  74. if rv == 'OK':
  75. print "Mailboxes:"
  76. print mailboxes
  77.  
  78. rv, data = M.select("INBOX")
  79. if rv == 'OK':
  80. print ''
  81. print '> Processing mailbox...'
  82. process_mailbox(M,0.7,0.5)
  83. print '> Finished Processing'
  84. M.close()
  85. M.logout()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement