本篇內(nèi)容主要講解“python 如何自動監(jiān)控新郵件并讀取”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“python 如何自動監(jiān)控新郵件并讀取”吧!
我就廢話不多說了,大家還是直接看代碼吧~
#zmail庫:可以用幾行代碼幫我們收取一封郵件 import zmail #輸入賬號和密碼 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庫
補(bǔ)充:Python郵箱實(shí)施監(jiān)控電腦
我就廢話不多說了,大家還是直接看代碼吧~
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é)議的郵件讀取對象 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
到此,相信大家對“python 如何自動監(jiān)控新郵件并讀取”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)建站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
當(dāng)前題目:python如何自動監(jiān)控新郵件并讀取-創(chuàng)新互聯(lián)
文章來源:http://jinyejixie.com/article38/pshsp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、網(wǎng)站建設(shè)、虛擬主機(jī)、用戶體驗(yàn)、商城網(wǎng)站、App設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容