flowercat 发表于 2013-1-27 04:55:18

python下读取邮件列表,并过滤邮件,然后调用网页发短信

#!/usr/bin/python#coding=utf8import poplib   import emailimport cPickle as picklefrom email.Header import decode_headerimport urllibdef getMails(keymap={},userinfo=[]):    emailServer = poplib.POP3('mail.domain.com')       emailServer.user(userinfo)       emailServer.pass_(userinfo)       mailinfo=[]    # 遍历邮件    for i in range(emailMsgNum):         message=emailServer.retr(i+1)      mail=email.message_from_string('\n'.join(message))         if mail['from'].find('chinamobile.com')>-1 or mail['from'].find('139.com')>-1:            frommail=decode_header(mail['from'])            if frommail.find('.com'):                posstart=frommail.find('<')                posend=frommail.find('>')                if posstart>-1 and posend>-1:                  frommail=frommail            if not keymap.has_key(mail['received']):                keymap]=frommail                mailinfo.append((frommail,decode_header(mail['subject'])))                emailServer.quit()       return mailinfoif __name__ == "__main__":    userinfo=[('username@domain.com','passwd')]    noticeurls='http://********/sms.jsp'    ff=file("/home/myhome/python-src/mapsinfo")    allmap=pickle.load(ff)      map=allmap]    content=getMails(keymap=map,userinfo=userinfo)    allmap]=map      f=file("/home/myhome/python-src/mapsinfo","w")    pickle.dump(allmap,f)    for c,d in content:      params = urllib.urlencode({'tel': phoneno(手机号), 'content': "from:"+c+"::"+d})      #urlencode很重要,在linux下需要先encode才能正确处理中文,不能直接拼url串      data = urllib.urlopen(noticeurls,params).read()   #print data    print 'over'
页: [1]
查看完整版本: python下读取邮件列表,并过滤邮件,然后调用网页发短信