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


#!/usr/bin/python
#coding=utf8
import poplib
import email
import cPickle as pickle
from email.Header import decode_header
import urllib

def 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 mailinfo


if __name__ == "__main__":
userinfo=[('[email protected]','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'

你可能感兴趣的:(python)