首页 > 编程学习 > python处理eml格式的邮件

python处理eml格式的邮件

发布时间:2022/12/7 17:04:23

对于数据进行读取

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/2/29/029 22:03
# @Author : H
# @File : getEmailHeader.pyimport os
import re
from email.parser import Parserdef read_mail(path):if os.path.exists(path):with open(path) as fp:email = fp.read()return emailelse:print("file not exist!")def emailInfo(emailpath):raw_email = read_mail(emailpath)  # 将邮件读到一个字符串里面print('emailpath : ', emailpath)emailcontent = Parser().parsestr(raw_email)  # 经过parsestr处理过后生成一个字典# for k,v in emailcontent.items():#     print(k,v)From = emailcontent['From']To = emailcontent['To']Subject = emailcontent['Subject']Date = emailcontent['Date']MessageID = emailcontent['Message-ID']XOriginatingIP = emailcontent['X-Originating-IP']if "<" in From:From = re.findall(".*<(.*)>.*", From)[0]if "<" in To:To = re.findall(".*<(.*)>.*", To)[0]print("From:\t", From)print("X-Originating-IP", XOriginatingIP)print("To:\t", To)print("Subject:\t", Subject)print("Message-ID:\t", MessageID)print("Date:\t", Date)# 循环信件中的每一个mime的数据块for par in emailcontent.walk():if not par.is_multipart():  # 这里要判断是否是multipart,是的话,里面的数据是无用的content = par.get_payload(decode=True)# print(str(content,"utf-8",errors='ignore'))print("content:\t", content.decode(encoding='gbk'))  # 解码出文本内容,直接输出来就可以了。if __name__ == '__main__':email = "请求预推免系统解锁.eml"emailInfo(email)_str = 'str'
print(type(_str))
# 输出为 <class 'str'>_bytes = b'bytes'
print(type(_bytes))
# 输出为<class 'bytes'>


本文链接:https://www.ngui.cc/zz/1544525.html
Copyright © 2010-2022 ngui.cc 版权所有 |关于我们| 联系方式| 豫B2-20100000