python提取eml邮件内容及附件

发布时间 2023-09-01 09:26:34作者: Rong_Z
import email
import base64
from email.header import decode_header, Header

fp = r"C:\Users\rongge\Downloads\每周文摘 _ Linux搭建GitLab私有仓库,并内网穿透实现公网访问.eml"
fp = r"C:\Users\rongge\Downloads\需求文档.eml"
# 读取eml文件
with open(fp, "r") as file:
    eml_content = file.read()

# 将eml文件转换为Message对象
# msg = email.message_from_string(eml_content)
msg = email.message_from_string(eml_content)
# print(msg)
# 输出邮件的主题和发件人
subject_bytes, subject_encode = decode_header(msg["Subject"])[0]
if len(decode_header(msg["from"])) == 2:
    from_bytes, from_encode = decode_header(msg["from"])[0]
    fromip_bytes, fromip_encode = decode_header(msg["from"])[1]
else:
    fromip_bytes, fromip_encode = None, None
from_bytes, from_encode = decode_header(msg["from"])[0]
if from_encode:
    print("发件人:", from_bytes.decode(from_encode))
else:
    print("发件人:", from_bytes)
if fromip_encode:
    print("发件人ip:", fromip_bytes.decode(fromip_encode))
else:
    print("发件人ip:", fromip_bytes)
print("主题:", subject_bytes.decode(subject_encode))


for par in msg.walk():
    if not par.is_multipart():  # 这里要判断是否是multipart,是的话,里面的数据是无用的,至于为什么可以了解mime相关知识。
        name = par.get_param("name")  # 如果是附件,这里就会取出附件的文件名
        if name:
            # 有附件
            # 下面的三行代码只是为了解码象=?gbk?Q?=CF=E0=C6=AC.rar?=这样的文件名
            dh = decode_header(name)
            fname = dh[0]
            print("附件名:", fname[0].decode(fname[1]))
            data = par.get_payload(decode=True)  #  解码出附件数据,然后存储到文件中
            try:
                f = open(fname[0].decode(fname[1]), "wb")  # 注意一定要用wb来打开文件,因为附件一般都是二进制文件
            except:
                print("附件名解码失败,自动储存为 附件")
                f = open("附件", "wb")
            f.write(data)
            f.close()
        else:
            # 不是附件,是文本内容
            print(par.get_payload(decode=True).decode('utf-8'))  # 解码出文本内容,直接输出来就可以了。
        print("+" * 60)  # 用来区别各个部分的输出