python 读取邮件

#邮件数量太多,运行缓慢,因此只读取最近的邮件

#!/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)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值