Extract/save a mail attachment using bash

If you're aiming for portability, beware that there are several different versions of mail(1) and mailx(1). There's a POSIX mailx command, but with very few requirements. And none of the implementations I have seem to parse attachments anyway.

You might have the mpack package. Its munpack command saves all parts of a MIME message into separate files, then all you have to do is save the interesting parts and clean up the rest.

There's also metamail. An equivalent of munpack is metamail -wy.


The best program for this purpose is ripMIME.

It extracts the text and all attachments:

https://pldaniels.com/ripmime/

GitHub: inflex/ripMIME


  • YES possible

  • This is HOW (Resource from here)

find dir containing files | while read file; do

create tempdir and copy file there

run munpack on file and copy attachments elsewhere

destroy tempdir (contents)

done

  • Here is the Perl Script to do it