41

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
  • 9,920
  • 23
  • 86
  • 147
  • "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 Sep 10 '10 at 17:46
  • @Doug Harris - either.. if I can call `mail` and do this, or save the attachment elsewise, that's fine too :) – warren Sep 10 '10 at 17:47

3 Answers3

39

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'
  • 69,786
  • 21
  • 137
  • 178
  • 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 Oct 13 '17 at 10:07
  • Thanks for the info about `metamail`. Can you please help me up with [this question](https://superuser.com/questions/1299828/how-to-use-metamail-to-extract-mail-attachments-without-interactive-input)? – george Mar 02 '18 at 12:59
  • 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](https://stackoverflow.com/questions/11281893/how-to-split-mailbox-into-single-file-per-message) – FelixJN Mar 21 '20 at 21:53
19

The best program for this purpose is ripMIME.

It extracts the text and all attachments:

https://pldaniels.com/ripmime/

GitHub: inflex/ripMIME

Greg A. Woods
  • 326
  • 3
  • 10
Andreas Rehm
  • 379
  • 1
  • 7
  • 4
    `sudo 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 Apr 25 '18 at 09:16
  • good stuff, metamail and munpack not available on Ubuntu but ripmime is and does exactly what expected. – Diego Jul 28 '18 at 14:51
  • Thank you! `brew install ripmime` on macOS works well, and then `ripmime -i – Greg Sadetsky Jun 17 '21 at 15:49
  • comparing with munpack, it was able to handle better (not perfectly though) encoded file names – lepe Mar 31 '22 at 06:44
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
  • 7,566
  • 8
  • 44
  • 50