#邮件数量太多,运行缓慢,因此只读取最近的邮件
#!/usr/bin/python
# -*- coding: UTF-8 -*-
# 文件名:read_email.py
from email.parser import Parser
import poplib
from bs4 import BeautifulSoup
from email.header import decode_header
# 邮件的Subject或者Email中包含的名字都是经过编码后的str,要正常显示,就必须decode
def decode_str(s):
value, charset = decode_header(s)[0]
# decode_header()返回一个list,因为像Cc、Bcc这样的字段可能包含多个邮件地址,所以解析出来的会有多个元素。上面的代码我们偷了个懒,只取了第一个元素。
if charset:
value = value.decode(charset)
return value
def print_info(msg,indent=0):
if indent ==0:
for header in ["From","To","Subject","name"]:
value = msg.get(header,"")
if value:
if header == "Subject":
value = decode_str(value)
if value == "邮件主题名" :#取18封邮件中需要的邮件
for par in msg.walk():
if not par.is_multipart(): # 这里要判断是否是multipart,是的话,里面的数据是无用的,至于为什么可以了解mime相关知识。
content_type = par.get_content_type()
if content_type == 'text/plain' or content_type == 'text/html':
content = par.get_payload(decode=True)
return htmlparse(content)
def htmlparse(content):
soup = BeautifulSoup(content, "html.parser")
list = [];
for tr in soup.find('table').children:
list.append(tr)
return (len(list))
def getMail(host,user,passwd,port=110):
p = poplib.POP3(host,port)
try:
p.user(user)
p.pass_(passwd)
except (poplib.error_proto,e):
print ("Login failed:",e)
sys.exit(1)
# 获取服务器上信件信息,返回是一个列表,第一项是一共有多上封邮件,第二项是共有多少字节
status = p.stat()
resp, mails, octets = p.list()
#可以查看返回的列表类似[b'1 82923', b'2 2184', ...]
#print(mails)
#print(p.list())
for i in range(0,18): #取最近的18封邮件
# 获取最新一封邮件, 注意索引号从1开始:
index = len(mails)-i
resp, lines, octets = p.retr(index)
#print(lines)
# lines存储了邮件的原始文本的每一行,
# 可以获得整个邮件的原始文本:
msg_content = b'\r\n'.join(lines).decode('utf-8')
# 稍后解析出邮件:
msg = Parser().parsestr(msg_content)
param = print_info(msg)
if not param is None:
return(param)
p.quit()
if __name__ == '__main__':
host = "pop3.------.com" # "pop.mail_serv.com"
username = "13231@163.com"
password = "----------"
getMail(host,user,passwd)