python 自動(dòng)監(jiān)控最新郵件并讀取的操作
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
#zmail庫(kù):可以用幾行代碼幫我們收取一封郵件 import zmail #輸入賬號(hào)和密碼 server=zmail.server('13163964546@qq.com','jie110341') #獲取最新的一封郵件 mail=server.get_latest() #讀取郵件 #zmail.show(mail) #讀取郵件的部分內(nèi)容 print(mail['subject']) ...... #讀取附件 郵件 存放路徑 如果有同名文件則覆蓋 zmail.save_acctachment(mail,target_path=None,overwrite=True)
需要在電腦上下載zmail庫(kù)
補(bǔ)充:Python郵箱實(shí)施監(jiān)控電腦
我就廢話不多說(shuō)了,大家還是直接看代碼吧~
import smtplib import poplib import email from email.mime.application import MIMEApplication from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText from email.header import decode_header def send_email(account, password, email_title, send_text="", file_names=None, file_dir="."): msg = MIMEMultipart() # msg = MIMEText(HTML, 'html') -- 只能發(fā)送文本內(nèi)容 content = MIMEText(send_text, "plain", "utf-8") msg.attach(content) # 文件類型 if isinstance(file_names, list): for file_name in file_names: send_file_path = file_dir + "/" + file_name part = MIMEApplication(open(send_file_path, 'rb').read()) part.add_header('Content-Disposition', 'attachment', filename=file_name) msg.attach(part) elif isinstance(file_names, str): send_file_path = file_dir + "/" + file_names part = MIMEApplication(open(send_file_path, 'rb').read()) part.add_header('Content-Disposition', 'attachment', filename=file_names) msg.attach(part) # msg['from'],msg['to']接收端顯示的發(fā)件人與收件人 msg['from'] = "奧巴馬@163.com" msg['to'] = account msg['subject'] = email_title try: server = smtplib.SMTP() server.connect('smtp.163.com') server.login(account, password) # from_addr:發(fā)送地址; to_addrs:接收地址(字符串列表) server.sendmail(account, msg['to'].split(), msg.as_string()) except Exception as e: print(e) # 獲取郵件標(biāo)題 def get_email_subject(addr, password): # 設(shè)置連接網(wǎng)址,獲取pop3協(xié)議的郵件讀取對(duì)象 read = poplib.POP3('pop.163.com', timeout=3600) # 輸入郵件地址與郵件登錄密碼 read.user(addr) # 163郵箱用戶名 read.pass_(password) # 163郵箱設(shè)置中的客戶端授權(quán)密碼 # allEmails = (totalNum, totalSize) # 讀取郵件信息(郵件總數(shù),郵件尺寸) total_num, total_size = read.stat() # top(which,howmuch) # 獲取最新的一封郵件(第幾封郵件,獲取多少封) top_email = read.top(total_num, 1) # print("***** start *****\n接收的數(shù)據(jù)為: {}\n***** end *****\n".format(top_email)) # # print("***** start *****\n[解碼前]獲取的初始郵件內(nèi)容: {}\n***** end *****\n".format(top_email[1])) # 解碼郵件信息,將解碼后的郵件信息存入tmp tmp = [] for s in top_email[1]: tmp.append(s.decode()) # print("***** start *****\n[解碼后]的郵件內(nèi)容為: {}\n***** end *****\n".format(tmp)) # 將解碼后的郵件內(nèi)容拼接為字符串 email_str = '\n'.join(tmp) # 將字符串類型解析為Message類型 message = email.message_from_string(email_str) # print("***** start *****\n" # "[解碼前]的郵件字符串內(nèi)容為: [數(shù)據(jù)類型]{}\n{}\n" # "--------------------------------------------\n" # "[解碼后]的郵件字符串內(nèi)容為: [數(shù)據(jù)類型]{}\n{}\n" # "***** end *****\n" # .format(type(email_str), email_str, type(message), message)) # 獲取郵件主題 subject_str = message['subject'] # print("***** start *****\n[解碼前]郵件標(biāo)題: {}\n***** end *****\n".format(subject_str)) subject = decode_header(subject_str) # print("***** start *****\n[解碼后]郵件標(biāo)題: {}\n***** end *****\n".format(subject)) content = subject[0][0] enc_type = subject[0][1] if enc_type: subject_decode = content.decode(enc_type) else: subject_decode = content return subject_decode, read, total_num
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。
- python實(shí)現(xiàn)自動(dòng)化辦公郵件合并功能
- Python利用機(jī)器學(xué)習(xí)算法實(shí)現(xiàn)垃圾郵件的識(shí)別
- Python 發(fā)送SMTP郵件的簡(jiǎn)單教程
- Python一行代碼實(shí)現(xiàn)自動(dòng)發(fā)郵件功能
- Python基礎(chǔ)詳解之郵件處理
- Python 調(diào)用API發(fā)送郵件
- Python基于SMTP發(fā)送郵件的方法
- python基于SMTP發(fā)送QQ郵件
- python實(shí)現(xiàn)發(fā)送郵件
- python 實(shí)現(xiàn)網(wǎng)易郵箱郵件閱讀和刪除的輔助小腳本
- python如何發(fā)送帶有附件、正文為HTML的郵件
- python使用Windows的wmic命令監(jiān)控文件運(yùn)行狀況,如有異常發(fā)送郵件報(bào)警
- 用python監(jiān)控服務(wù)器的cpu,磁盤空間,內(nèi)存,超過(guò)郵件報(bào)警
- python郵件中附加文字、html、圖片、附件實(shí)現(xiàn)方法
- Python用20行代碼實(shí)現(xiàn)完整郵件功能
相關(guān)文章
簡(jiǎn)單講解Python中的數(shù)字類型及基本的數(shù)學(xué)計(jì)算
這篇文章主要介紹了Python中的數(shù)字類型及基本的數(shù)學(xué)計(jì)算,與其他語(yǔ)言一樣,除法相對(duì)復(fù)雜些,不過(guò)本文并未就此深入,需要的朋友可以參考下2016-03-03簡(jiǎn)單介紹Python的輕便web框架Bottle
這篇文章主要介紹了Python的輕便web框架Bottle,因其注重輕便的設(shè)計(jì),與Flask一樣,Bottle框架的人氣同樣也非常高,需要的朋友可以參考下2015-04-04Python中break語(yǔ)句和continue語(yǔ)句的用法講解
在Python中,break語(yǔ)句和continue語(yǔ)句一般用于循環(huán)語(yǔ)句中,這篇文章主要介紹了Python中break語(yǔ)句和continue語(yǔ)句的用法小結(jié),需要的朋友可以參考下2022-12-12Python pyinstaller庫(kù)的安裝配置教程分享
pyinstaller模塊主要用于python代碼打包成exe程序直接使用,這樣在其它電腦上即使沒(méi)有python環(huán)境也是可以運(yùn)行的。本文就來(lái)和大家分享一下pyinstaller庫(kù)的安裝配置教程,希望對(duì)大家有所幫助2023-04-04TensorFlow人工智能學(xué)習(xí)數(shù)據(jù)類型信息及轉(zhuǎn)換
這篇文章主要為大家介紹了TensorFlow人工智能學(xué)習(xí)數(shù)據(jù)類型信息及轉(zhuǎn)換,2021-11-11python對(duì)指定目錄下文件進(jìn)行批量重命名的方法
這篇文章主要介紹了python對(duì)指定目錄下文件進(jìn)行批量重命名的方法,涉及Python中replace及join方法的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04