Python教程

python处理eml格式的邮件

本文主要是介绍python处理eml格式的邮件,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

对于数据进行读取

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2020/2/29/029 22:03
# @Author : H
# @File : getEmailHeader.py

import os
import re
from email.parser import Parser


def read_mail(path):
    if os.path.exists(path):
        with open(path) as fp:
            email = fp.read()
            return email
    else:
        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'>

这篇关于python处理eml格式的邮件的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!