Extract/save a mail attachment using bash

32

9

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?

warren

Posted 2010-09-10T17:35:30.870

Reputation: 8 599

"normal bash tools" -- do you mean using only the functions built into bash (i.e. what you'll find in the bash man page) or do you mean, more generally, command line tools which you could add to a bash shell script? – Doug Harris – 2010-09-10T17:46:56.210

@Doug Harris - either.. if I can call mail and do this, or save the attachment elsewise, that's fine too :) – warren – 2010-09-10T17:47:49.477

Answers

30

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.

Gilles 'SO- stop being evil'

Posted 2010-09-10T17:35:30.870

Reputation: 58 319

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 – Pierz – 2017-10-13T10:07:25.803

Thanks for the info about metamail. Can you please help me up with this question?

– george – 2018-03-02T12:59:08.527

14

The best program for this purpose is ripMIME.

It extracts the text and all attachments:

https://pldaniels.com/ripmime/

GitHub: inflex/ripMIME

Andreas Rehm

Posted 2010-09-10T17:35:30.870

Reputation: 339

4sudo apt install ripmime works though. See also https://linux.die.net/man/1/ripmime . I use this program; works great, no dependencies except libc6. – unhammer – 2018-04-25T09:16:17.380

good stuff, metamail and munpack not available on Ubuntu but ripmime is and does exactly what expected. – Diego – 2018-07-28T14:51:34.287

2

  • 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

subanki

Posted 2010-09-10T17:35:30.870

Reputation: 6 702

If anyone wants me to paste the Perl script here then just leave a comment – subanki – 2010-09-10T18:03:30.457

1if you would be so kind (obviously leaving attribution of where you found it :) ) – warren – 2010-09-10T18:18:48.013

1just read the script ... that's a heckuva lot of perl! – warren – 2010-09-10T18:24:55.053

so you are sure you want me to include that on my answer , it will become very long. – subanki – 2010-09-10T18:40:44.403

don't paste here, copying from the original source would be better in this case :) – warren – 2010-09-28T21:16:44.227

btw - shared the bash script I ended up using here: http://antipaucity.com/2014/04/24/automatically-extract-email-attachments-with-common-linux-tools/#.U1lFauZdWtY

– warren – 2014-04-24T17:10:19.150