Extract/save a mail attachment using bash
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/
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
- Here is the Perl Script to do it
Related videos on Youtube
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, 2022Comments
-
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 almost 14 yearsIf anyone wants me to paste the Perl script here then just leave a comment
-
David Yates almost 14 yearsif you would be so kind (obviously leaving attribution of where you found it :) )
-
David Yates almost 14 yearsjust read the script ... that's a heckuva lot of perl!
-
subanki almost 14 yearsso you are sure you want me to include that on my answer , it will become very long.
-
David Yates over 13 yearsdon't paste here, copying from the original source would be better in this case :)
-
David Yates about 10 yearsbtw - shared the bash script I ended up using here: antipaucity.com/2014/04/24/…
-
Pierz over 6 yearsAlso 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 over 6 yearsThanks for the info about
metamail
. Can you please help me up with this question? -
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 almost 6 yearsgood stuff, metamail and munpack not available on Ubuntu but ripmime is and does exactly what expected.
-
FelixJN over 4 yearsNotably
munpack
works on single mails only. When dealing with a whole mailbox file, it needs to be split first. E.g. usingformail < /val/mail/usersmailbox -s munpack
. reference -
Greg Sadetsky about 3 yearsThank you!
brew install ripmime
on macOS works well, and thenripmime -i <input file
. Cheers -
lepe about 2 yearscomparing with munpack, it was able to handle better (not perfectly though) encoded file names