1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
| # -*- Mode: Python; coding: utf-8; indent-tabs-mode: nil; tab-width: 4 -*-
import threading
from email.header import decode_header
from email.utils import parsedate_tz, formatdate, mktime_tz, parseaddr
import poplib
from email import message_from_string
def get_input_connection(email, pwd, srv):
session = poplib.POP3_SSL(srv)
session.user(email)
session.pass_(pwd)
return session
class Emails(threading.Thread):
def __init__(self, server, user, pwd):
threading.Thread.__init__(self)
self.server = server
self.user = user
self.pwd = pwd
def run(self):
session = get_input_connection(self.user, self.pwd, self.server)
if session <> False:
mail_count = 0
numMessages = session.stat()[0]
for i in range(1, numMessages+1):
message = '\n'.join(session.top(i, 0)[1])
message = message_from_string(message)
_from = []
for item in parseaddr(message['from']):
_from.append(self.decode_string(item))
sender = "%s <%s>" % (_from[0], _from[1])
timestamp = mktime_tz(parsedate_tz(message['date']))
date = formatdate(timestamp)
subject = self.decode_string(message['Subject'])
#uidl = session.uidl(i)
#id = self.user + uidl.split(' ')[2]
#print sender, date, subject, timestamp
mail_count += 1
print "le thread "+self.user+" s'est termine proprement "+ str(mail_count)
else:
print "Ko"
def decode_string(self, string):
value, charset = decode_header(string)[0]
try:
return value.decode(charset or 'utf-8')
except:
return value
# __ Main __
if __name__ == "__main__":
a = Emails('pop.citron.fr', 'toto.titi@citron.fr','bleu')
b = Emails('pop.citron.fr', 'tata.popo@citron.fr','rouge')
c = Emails('pop.citron.fr', 'bob.patrick@citron.fr','vert')
a.start()
b.start()
c.start() |
Partager