Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question
    
"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 Answers 3

up vote 13 down vote accepted

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.

share|improve this answer
4  
munpack works like a charm! –  elhoim Dec 20 '11 at 10:25

The best program for this purpose is ripmime.

It extracts the text and all attachments:

http://www.pldaniels.com/ripmime/

share|improve this answer
  • 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|improve this answer
    
If anyone wants me to paste the Perl script here then just leave a comment –  subanki Sep 10 '10 at 18:03
1  
if you would be so kind (obviously leaving attribution of where you found it :) ) –  warren Sep 10 '10 at 18:18
1  
just read the script ... that's a heckuva lot of perl! –  warren Sep 10 '10 at 18:24
    
so you are sure you want me to include that on my answer , it will become very long. –  subanki Sep 10 '10 at 18:40
    
don't paste here, copying from the original source would be better in this case :) –  warren Sep 28 '10 at 21:16

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.