Extract all attachments from unix mailbox file

Hi guys,

I wondering if someone already wrote a script for this, it looks easy for the first shot but I would say it's a bit advanced.

I want to extract all my email attachments from /var/mail/username (Unix mail format) + from my local mails (Maildir format) to a directory with the original filename, and rename it automatically if 2 attachments have the same name. Thunderbird has an extension for it but I would like to do it with shellscript.

If noone knows script for this (tried googling of course) then I write my own.
Pbly sed would be sufficient to process the whole mailbox file and find the attachments as records.

Thanks

Doing ops on e-mail is IMHO better (safer) using tools that understand e-mail (just like you don't write your MBR with a hex editor). In this case you could use "munpack":

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

Something similar here...

Perl script to save an email attachment to disk - -General UNIX discussion FAQ - Tek-Tips

Hi,

Thanks for the answers. Actually both of you give an answer which is part of the solution. I was thinking that someone must wrote a tool like munpack for it.

The only problem with munpack that it can't process a single mailbox file, eg: thunderbird's inbox. It only works if I make a test.txt which containst only a single mail which starts with:

From <sender> Mon Jun 16 20:42:11 2008
...
and ends with
--------------=CODE--

So I still have to cut this file apart to single mails in order to extract the attachments from them but I can do it from here. Thx guys.

Yeah, I read over that in your OP. You could automate separating e-mails and processing them individually with munpack using a procmail recipe.