abholer.py (1391B)
1 #!/usr/bin/python 2 # E-Mail-Abholer 3 # 4 # 5 import imaplib,email,datetime,os,base64,quopri 6 7 8 M = imaplib.IMAP4("mail.jdhh.de") 9 M.login("blog@jandankert.de", "") 10 M.select("INBOX") 11 typ, data = M.search(None, 'ALL') 12 13 for num in data[0].split(): 14 15 now = datetime.datetime.now() 16 dir = os.path.dirname( os.path.realpath( __file__ ) )+'/../blog/'+str(now)+'/' 17 os.mkdir(dir) 18 19 f = open(dir+'title', 'w') 20 typ, data = M.fetch(num, '(RFC822)') 21 #print 'Message #'+num; 22 src = data[0][1] 23 msg = email.message_from_string(src) 24 print "Subject: "+msg["Subject"] 25 f.write(msg["Subject"]) 26 27 f = open(dir+'sender','w') 28 #print "From: "+msg["From"] 29 f.write(msg["From"]) 30 31 32 #for pl in get_flat_parts(msg): 33 for part in msg.walk(): 34 35 if part.is_multipart(): 36 continue 37 38 39 #print " Typ:"+part.get_content_type() 40 #print " Inhalt:"+part.get_payload() 41 42 if part.get_content_type() == "text/plain": 43 f= open(dir+'text','w') 44 f.write( quopri.decodestring(part.get_payload()) ) 45 elif part.get_content_type()[:6] == "image/": 46 f = open( dir + 'attachment-'+part.get_filename(),'w' ) 47 f.write( base64.decodestring( part.get_payload() ) ) 48 else: 49 print "Unbekannter Typ: "+part.get_content_type() 50 #print "\n" 51 #print "\n\n" 52 53 M.copy(num,"Archiv") 54 M.store(num, '+FLAGS', r'(\Deleted)') 55 M.expunge 56 M.close() 57 M.logout() 58 59