[neomutt-users] deleting text/html from messages

Jakub Jindra jakub.jindra at jakubjindra.eu
Thu Mar 12 19:47:47 CET 2020

I was searching for some formail magic, maybe there's other tool I don't 
know about could do the job.

I came up with a python script – see attachment. Beware it can destruct 
your messages! Test it first on some copies of your messages. Sorry, I'm 
not a programmer :-(

The script accepts message from maildir as an argument:
./make_me_plain_please.py /path/to/file

It's not 100% ready to use but it doesn't look so hard to finish. I was 
able to extract headers, iterate through parts and extract text/plain 
only.  At the end it writes headers + newline + body back into the file.

You probably want to change Content-type header to text/plain, remove 
boundaries and preserve encoding. That's what's missing. I also noticed 
It strips trailing spaces in To header on my testing message. But this 
shouldn't be a blocker.

On 2020-03-12 17:52, Sven Guckes wrote:
> is there a way to tag text/html attachments for deletion?
> there is no pattern for tagging attachments, or is there?
> this would also be nice to get rid of pictures, DOCs or PDFs.
> (yes, I know i can tag messages by *number* of attachments..)
> or are there any other tools you are using for this problem?
> Sven

Jakub Jindra
Mobile: +420732114225
-------------- next part --------------
#!/usr/bin/env python3
import sys
import email

fp = open(sys.argv[1],"r")
msg = email.message_from_file(fp)

h = email.parser.HeaderParser().parsestr(msg.as_string())

for key, value in h.items():
  headers+=key+ ': '+ value + '\n'
  # add condition to change content-type and remove boundaries

# get text/plain only
if msg.is_multipart():
  for part in msg.walk():
    ctype = part.get_content_type()

    if ctype == 'text/plain':
      body = part.get_payload(decode=False)
  body = msg.get_payload(decode=False)

# output to a file, now just printing
fp = open(sys.argv[1],"w")
fp.write(headers + '\n'+body)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mailman.neomutt.org/pipermail/neomutt-users-neomutt.org/attachments/20200312/e9c4093d/attachment.sig>

More information about the neomutt-users mailing list