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()
python实现收邮件判断模块poplib,email的更多相关文章
- python之模块poplib之常见用法
# -*- coding: cp936 -*- #python 27 #xiaodeng #python之模块poplib之常见用法 ''' 所以,收取邮件分两步: 第一步:用poplib把邮件的原始 ...
- Python使用SMTP模块、email模块发送邮件
一.smtplib模块: 主要通过SMTP类与邮件系统进行交互.使用方法如下: 1.实例化一个SMTP对象: s = smtplib.SMTP(邮件服务地址,端口号) s = smtplib.SMTP ...
- Python自动发邮件——smtplib和email库和yagmail库
''' 一.先导入smtplib模块 导入MIMEText库用来做纯文本的邮件模板 二.发邮件几个相关的参数,每个邮箱的发件服务器不一样,以163为例子百度搜索服务器是 smtp.163.com 三. ...
- Python通过yagmail和smtplib模块发送简单邮件
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件.python发邮件需要掌握两个模块的用法,smtplib和email,这俩模块是pytho ...
- 使用Python内置的smtplib包和email包来实现邮件的构造和发送。
此文章github地址:https://github.com/GhostCNZ/Python_sendEmail Python_sendEmail 使用Python内置的smtplib包和email包 ...
- python发送各类邮件的主要方法
更多详见: http://www.w3cschool.cc/python/python-email.html python中email模块使得处理邮件变得比较简单,今天着重学习了一下发送邮件的具体做法 ...
- 九、Python发送QQ邮件(SMTP)
看了廖雪峰老师的教程: 一封电子邮件的旅程就是 发件人 -> MUA -> MTA -> MTA -> 若干个MTA -> MDA <- MUA <- 收件人 ...
- Python之日志处理 logging模块
Python之日志处理(logging模块) 本节内容 日志相关概念 logging模块简介 使用logging提供的模块级别的函数记录日志 logging模块日志流处理流程 使用logging四 ...
- Python 【收发邮件】
发邮件 smtplib模块主要负责发送邮件 email模块主要负责构造邮件.这两个都是Python内置模块 smtplib.SMTP.方法 #按住Ctrl键并点击SMTP ,会看到对SMTP的解释(v ...
随机推荐
- sql 实现分页+分组并取出分组内的前n条数据
一.建表 if exists (select * from sysobjects where id = OBJECT_ID('[test]') and OBJECTPROPERTY(id, 'IsUs ...
- nucleus plus学习总结
前言: 最近一直都在看nucleus plus,之前看过一些linux内核的一些东西,但都是停留在文字上,代码看的很少,这个nucleus plus内核的代码量不大,看过source code ...
- Python基础教程(007)--Python的优缺点
前言 了解Python的优点和缺点 知识点 优点 简单易学 免费,开源 面相对象 丰富的库 可扩展性 缺点 运行速度慢 国内市场较小 中文资料匮乏 总结: 明白Python的优点和缺点
- ajax中回调的几个坑
在前端开发中,经常要用ajax去拿后台接口返回的数据,总结几个ajax的回调的常见问题,供大家参考爬坑. 未定义contentType,可能会造成的传入后台的数据乱码,可以加上如下代码在ajax请求中 ...
- OpenResty 技术图谱skill-map
# OpenResty 技术图谱## basic concepts- HTTP- RESTful API & API GateWay- Microservice- Domain Specifi ...
- Losing session data in ASP.NET
Losing session data in ASP.NET By default Response.Redirect terminates thread execution and there mi ...
- What is the difference between UserControl, WebControl, RenderedControl and CompositeControl?
What is the difference between UserControl, WebControl, RenderedControl and CompositeControl? UserCo ...
- 数据库SQL调优的几种方式(转)
原文地址:https://blog.csdn.net/u010520146/article/details/81161762 在项目中,SQL的调优对项目的性能来讲至关重要,所有掌握常见的SQL调优方 ...
- Centos7防火墙使用
修改时区 Centos7 #修改时区 timedatectl set-timezone Asia/Shanghai 开启防火墙 #添加一条规则 firewall-cmd --zone=public - ...
- 接口自动化测试框架-AIM2.0
跳转到3.0版本https://www.cnblogs.com/df888/p/12031649.html AIM是我用python搭建的第一款接口自动化测试框架,随着技术的提升,框架也在升级,故有了 ...