六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 39|回复: 0

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

[复制链接]

升级  38%

27

主题

27

主题

27

主题

秀才

Rank: 2

积分
107
 楼主| 发表于 2013-1-27 04:55:18 | 显示全部楼层 |阅读模式
#!/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[0])       emailServer.pass_(userinfo[1])       mailinfo=[]    # 遍历邮件    for i in range(emailMsgNum):           message=emailServer.retr(i+1)[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'])[0][0]            if frommail.find('.com'):                posstart=frommail.find('<')                posend=frommail.find('>')                if posstart>-1 and posend>-1:                    frommail=frommail[posstart+1:posend]            if not keymap.has_key(mail['received']):                keymap[mail['received']]=frommail                mailinfo.append((frommail,decode_header(mail['subject'])[0][0]))                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[userinfo[0]]    content=getMails(keymap=map,userinfo=userinfo[0])    allmap[userinfo[0]]=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'
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表