Take the mail from the POP3 server, analyze the mail title, to cc bc from, and save it as a. eml file
1 import poplib 2 Import stringio, email 3 4 def dcode (STR): 5 H = Email. header. header (STR) 6 DH = Email. header. decode_header (h) 7 return DH [0] [0] 8 9 popsrv = "pop.xxxxxxxx.com" # connect to login server 10 username = "hsbk@xxxxxx.com" 11 passwd = "123456" 12 13 14 subject = "15 f_addr = "" 16 f_name = "" 17 to = [] 18 cc = [] 19 BC = [] 20 21 Pop = poplib. POP3 (popsrv) 22 # Pop. set_debuglevel (1) 23 pop. user (username) 24 Po P. pass _ (passwd) 25 26 27 num, total_size = pop. stat () 28 HDR, text, octet = pop. RETR (Num) # obtain the last email 29 text = '\ n '. join (text) # concatenate the list into a string of 30 31 amail = Email. message_from_string (text) 32 subject = dcode (amail. get ("subject") 33 34 35 36 f_addr = Email. utils. parseaddr (amail. get ("from") [1] 37 f_name = dcode (email. utils. parseaddr (amail. get ("from") [0]) 38 39 40 41 42 Tol = [] 43 tostr = MSG. get ('to') 44 45 I F tostr = none: 46 tostr = ''47 48 tostr = tostr. replace ('\ n ',''). replace ('\ t ',''). replace ('"',''). replace ("'", "") 49 Tol = tostr. split (',') 50 for T in tol: 51 taddr = Email. utils. parseaddr (t) [1] 52 records = taddr. find ("@") 53 if exist! =-1: 54. append (taddr) 55 56 57 Tol = [] 58 59 tostr = MSG. get ('bc') 60 61 If tostr = none: 62 tostr = ''63 64 tostr = tostr. replace ('\ n ',''). replace ('\ t ',''). replace ('"',''). replace ("'", "") 65 66 Tol = tostr. split (',') 67 for T in tol: 68 taddr = Email. utils. parseaddr (t) [1] 69 records = taddr. find ("@") 70 if exist! =-1: 71 BC. append (taddr) 72 73 74 Tol = [] 75 tostr = MSG. get ('cc') 76 77 If tostr = none: 78 tostr = ''79 80 tostr = tostr. replace ('\ n ',''). replace ('\ t ',''). replace ('"',''). replace ("'", "") 81 Tol = tostr. split (',') 82 for T in tol: 83 taddr = Email. utils. parseaddr (t) [1] 84 records = taddr. find ("@") 85 if exist! =-1: 86 CC. append (taddr) 87 88 89 90 fp = open ('e:/test3.eml', 'wb ') 91 FP. write (text) 92 FP. close () 93 # Pop. dele (Num) # Delete copies on the server 94 95 96 print "subject", subject 97 print "f_addr", f_addr 98 print "f_name", f_name 99 print "", to100 print "cc", cc101 print "BC", bc102 103 104