python实现收邮件判断模块poplib,email
一、代码
# 输入邮件地址, 口令和POP3服务器地址: import datetime import email import poplib import email.policy from email.parser import Parser flag=False emailaddress = '******' # 注意使用开通POP,SMTP等的授权码 password = '*******' pop3_server = '*******' # 连接到POP3服务器: server = poplib.POP3(pop3_server) # 可以打开或关闭调试信息: # server.set_debuglevel(1) # POP3服务器的欢迎文字: print(server.getwelcome()) # 身份认证: server.user(self.emailaddress) server.pass_(self.password) # stat()返回邮件数量和占用空间: messagesCount, messagesSize = server.stat() print('messagesCount:', messagesCount) print('messagesSize:', messagesSize) # list()返回所有邮件的编号: resp, mails, octets = server.list() print("resp:", resp) print("mails:", mails) print("octets:", octets) length = len(mails) print(length) for i in range(length): i=length-i resp, lines, octets = server.retr(i) msg_content=b'\r\n'.join(lines) msg = email.parser.BytesParser(policy=email.policy.default).parsebytes(msg_content) print('发件人', msg['from']) print('收件人', msg['to']) print('主题', msg['subject']) print('第一个收件人用户名', msg['to'].addresses[0].username) print('第一个发件人用户名', msg['from'].addresses[0].username) theme= msg['subject'] email_content=[part.get_content() for part in msg.walk() if part.get_content_maintype() == 'text'][0] if theme=="启动查库任务分派机器人" and email_content==datetime.datetime.now().strftime("%Y%m%d"): flag=True break server.quit() print(flag)
二、解决编码问题
import chardet #继承重写解析类 class BytesParser_new(email.parser.BytesParser): def parsebytes(self, text, headersonly=False): import chardet encoding=chardet.detect(text).get("encoding") text = text.decode(encoding, errors='surrogateescape') return self.parser.parsestr(text, headersonly) msg = BytesParser_new(policy=email.policy.default).parsebytes(msg_content)
三、代码封装
import email import poplib import email.policy class ReadEmail(object): def __init__(self, emailaddress, password, pop3_server): self.emailaddress = emailaddress self.password = password self.pop3_server = pop3_server self.server = poplib.POP3(pop3_server) def login(self): # 可以打开或关闭调试信息: self.server.set_debuglevel(1) # POP3服务器的欢迎文字: print(self.server.getwelcome()) # 身份认证: self.server.user(emailaddress) self.server.pass_(password) def read(self):
self.login() # stat()返回邮件数量和占用空间: messagesCount, messagesSize = self.server.stat() print('messagesCount:', messagesCount) print('messagesSize:', messagesSize) # list()返回所有邮件的编号: resp, mails, octets = self.server.list() print("resp:", resp) print("mails:", mails) print("octets:", octets) length = len(mails) print(length) for i in range(1, length): resp, lines, octets = self.server.retr(i) msg_content = b'\r\n'.join(lines) msg = email.parser.BytesParser(policy=email.policy.default).parsebytes(msg_content) # print('发件人', msg['from']) # print('收件人', msg['to']) print('主题', msg['subject']) # print('第一个收件人用户名', msg['to'].addresses[0].username) # print('第一个发件人用户名', msg['from'].addresses[0].username) theme = msg['subject'] email_content = [part.get_content() for part in msg.walk() if part.get_content_maintype() == 'text'][0] if theme == "系统退信/The email is returned": self.server.dele(i) print("删除邮件成功:", msg['subject']) self.server.quit() if __name__ == '__main__': emailaddress = '********' # 注意使用开通POP,SMTP等的授权码 password = '********' pop3_server = 'imap.qq.com' rm=ReadEmail(emailaddress,password ,pop3_server ) rm.read()
四、github最新smtplib代码
进一步对发送模块smtplib进行了封装,欢迎大家提意见
https://github.com/fengfumin/EasyMail