Python實現(xiàn)讀取郵箱中的郵件功能示例【含文本及附件】
本文實例講述了Python實現(xiàn)讀取郵箱中的郵件功能。分享給大家供大家參考,具體如下:
#-*- encoding: utf-8 -*- import sys import locale import poplib from email import parser import email import string # 確定運(yùn)行環(huán)境的encoding __g_codeset = sys.getdefaultencoding() if "ascii"==__g_codeset: __g_codeset = locale.getdefaultlocale()[1] # def object2double(obj): if(obj==None or obj==""): return 0 else: return float(obj) #end if # def utf8_to_mbs(s): return s.decode("utf-8").encode(__g_codeset) # def mbs_to_utf8(s): return s.decode(__g_codeset).encode("utf-8") # host = 'pop.exmail.qq.com' username = 'user1@xxxx.cn' password = 'password' pop_conn = poplib.POP3_SSL(host) pop_conn.user(username) pop_conn.pass_(password) #Get messages from server: # 獲得郵件 messages = [pop_conn.retr(i) for i in range(1, len(pop_conn.list()[1]) + 1)] #print messages #print "--------------------------------------------------" # Concat message pieces: messages = ["\n".join(mssg[1]) for mssg in messages] #print messages #Parse message intom an email object: # 分析 messages = [parser.Parser().parsestr(mssg) for mssg in messages] i = 0 for index in range(0,len(messages)): message = messages[index]; i = i + 1; subject = message.get('subject') h = email.Header.Header(subject) dh = email.Header.decode_header(h) subject = unicode(dh[0][0], dh[0][1]).encode('utf8') mailName = "mail%d.%s" % (i, subject) f = open('%d.log'%(i), 'w'); print >> f, "Date: ", message["Date"] print >> f, "From: ", email.utils.parseaddr(message.get('from'))[1] print >> f, "To: ", email.utils.parseaddr(message.get('to'))[1] print >> f, "Subject: ", subject print >> f, "Data: " j = 0 for part in message.walk(): j = j + 1 fileName = part.get_filename() contentType = part.get_content_type() mycode=part.get_content_charset(); # 保存附件 if fileName: data = part.get_payload(decode=True) h = email.Header.Header(fileName) dh = email.Header.decode_header(h) fname = dh[0][0] encodeStr = dh[0][1] if encodeStr != None: fname = fname.decode(encodeStr, mycode) #end if fEx = open("%s"%(fname), 'wb') fEx.write(data) fEx.close() elif contentType == 'text/plain':# or contentType == 'text/html': #保存正文 data = part.get_payload(decode=True) content=str(data); if mycode=='gb2312': content= mbs_to_utf8(content) #end if nPos = content.find('降息') print("nPos is %d"%(nPos)) print >> f, data #end if #end for f.close() #end for pop_conn.quit()
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計有所幫助。
相關(guān)文章
python+selenium實現(xiàn)自動化百度搜索關(guān)鍵詞
在本篇文章里我們給大家分享了一篇關(guān)于python+selenium實現(xiàn)自動化百度搜索關(guān)鍵詞的實例文章,需要的朋友們可以跟著操作下。2019-06-06Python實現(xiàn)的自定義多線程多進(jìn)程類示例
這篇文章主要介紹了Python實現(xiàn)的自定義多線程多進(jìn)程類,結(jié)合實例形式分析了Python多線程多進(jìn)程的相關(guān)調(diào)用與使用操作技巧,需要的朋友可以參考下2018-03-03python生成requirements.txt文件的兩種方法
requirements.txt 文件是項目的依賴包及其對應(yīng)版本號的信息列表,本文主要介紹了python生成requirements.txt文件的兩種方法,具有一定的參考價值,感興趣的可以了解一下2023-12-12Python使用logging實現(xiàn)多進(jìn)程安全的日志模塊
這篇文章主要為大家詳細(xì)介紹了Python如何使用標(biāo)準(zhǔn)庫logging實現(xiàn)多進(jìn)程安全的日志模塊,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2024-01-01