-
Notifications
You must be signed in to change notification settings - Fork 0
/
mail_stripper
executable file
·44 lines (33 loc) · 1.24 KB
/
mail_stripper
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
#!env python3
#
# This is a script to strip email attachments from all the files found in the
# arguments.
import email
import sys
REPLACE_TEXT = '''The attachment "{filename}" ({type}, {size:.0f} KiB) has been
deleted by `mail_stripper`. To recover it, on the next blood moon, place the
corpse of a virgin unicorn where the rainbows meet the Earth. '''
def strip_attachments(e):
for i in e.get_payload():
if i.get_content_type() != 'text/plain':
i.set_payload(REPLACE_TEXT.format(type=i.get_content_type(),
size=len(bytes(i)) / 1024 ,
filename=i.get_filename()))
del i['Content-Disposition']
i.set_type('text/plain')
email.encoders.encode_7or8bit(e)
return e.as_bytes()
def main():
for message in args:
with open(message, mode='rb') as f:
e = email.message_from_binary_file(f)
if e.is_multipart():
e = strip_attachments(e)
with open(message, mode='w+b') as f:
f.write(e)
if __name__ == '__main__':
args = sys.argv[1:]
if len(args) < 1:
sys.exit('USAGE: mail_stripper <msg_file> [msg_files]')
else:
main()