Extract/save a mail attachment using bash

90,110

Solution 1

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.

Solution 2

The best program for this purpose is ripMIME.

It extracts the text and all attachments:

https://pldaniels.com/ripmime/

GitHub: inflex/ripMIME

Solution 3

  • 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

Share:
90,110

Related videos on Youtube

David Yates
Author by

David Yates

I'm a hobbyist programmer, part-time sysadmin, and full-time analytics, big data, data center management, automation, and cloud computing architect and delivery engineer.

Updated on September 17, 2022

Comments

  • David Yates
    David Yates almost 2 years

    Using normal bash tools (ie, built-ins or commonly-available command-line tools), is it

    • possible, and
    • how

    to extract/save attachments on emails?

    For example, say I have a nightly report which arrives via email but is a zip archive of several log files. I want to save all those zips into a backup directory. How would I accomplish that?

  • subanki
    subanki almost 14 years
    If anyone wants me to paste the Perl script here then just leave a comment
  • David Yates
    David Yates almost 14 years
    if you would be so kind (obviously leaving attribution of where you found it :) )
  • David Yates
    David Yates almost 14 years
    just read the script ... that's a heckuva lot of perl!
  • subanki
    subanki almost 14 years
    so you are sure you want me to include that on my answer , it will become very long.
  • David Yates
    David Yates over 13 years
    don't paste here, copying from the original source would be better in this case :)
  • David Yates
    David Yates about 10 years
    btw - shared the bash script I ended up using here: antipaucity.com/2014/04/24/…
  • Pierz
    Pierz over 6 years
    Also to add that if you want to also extract the text MIME parts of the email when using munpack then use the '-t' option: munpack -t email_file
  • george
    george over 6 years
    Thanks for the info about metamail. Can you please help me up with this question?
  • user3619803
    user3619803 about 6 years
    sudo apt install ripmime works though. See also linux.die.net/man/1/ripmime . I use this program; works great, no dependencies except libc6.
  • Diego
    Diego almost 6 years
    good stuff, metamail and munpack not available on Ubuntu but ripmime is and does exactly what expected.
  • FelixJN
    FelixJN over 4 years
    Notably munpack works on single mails only. When dealing with a whole mailbox file, it needs to be split first. E.g. using formail < /val/mail/usersmailbox -s munpack . reference
  • Greg Sadetsky
    Greg Sadetsky about 3 years
    Thank you! brew install ripmime on macOS works well, and then ripmime -i <input file. Cheers
  • lepe
    lepe about 2 years
    comparing with munpack, it was able to handle better (not perfectly though) encoded file names