如何提取邮件.eml文件的全部附件呢?

可以使用下面这段python代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45

import os
import email
from email import policy
from email.parser import BytesParser

def save_attachments_from_eml(eml_file_path, output_dir, file_name):
# 读取 .eml 文件内容
with open(eml_file_path, 'rb') as f:
eml_content = f.read()

# 解析 .eml 文件
msg = BytesParser(policy=policy.default).parsebytes(eml_content)

# 检查并保存附件
for part in msg.iter_attachments():
filename = part.get_filename()
if filename:
file_data = part.get_payload(decode=True)

# 创建输出文件路径
output_file_path = os.path.join(output_dir, file_name + '-' + filename)

# 将附件数据写入文件
with open(output_file_path, 'wb') as f:
f.write(file_data)

print(f"Saved attachment: {output_file_path}")

eml_file_path = '/Users/wangmingjie/Desktop/NET'
output_dir = '/Users/wangmingjie/Desktop/NET-attachment'
os.makedirs(output_dir, exist_ok=True)

# 获取该路径下全部文件
files = os.listdir(eml_file_path)

for file in files:
if file.endswith('.eml'):
# 将邮件名称作为file_name
file_name = file.split('.')[0]
save_attachments_from_eml(os.path.join(eml_file_path, file), output_dir, file_name)


print('存储完成,共存储了{}个文件'.format(len(files)))