views:

338

answers:

2

I'm writing a shell script to extract mail attachments from an mbox file

At the moment I use this command: cat mboxfile|formail -des munpack -qf

But I'd like to embed the sender email address in the filename, something like:

user@host_filename.extension

Can you suggest me some tool?

+1  A: 

Why not use a scripting language with mbox libraries ? e.g. Perl and the Mail::MBox module. Using a ready made library will likely save you a lot of grief.

Brian Agnew
'cause I don't know perl ;)First I'd like to find a shell solution...
atrent
I'd recommend learning a more powerful scripting language. Doesn't have to be Perl - perhaps Ruby/Python etc.? But I appreciate that doesn't help you right now.
Brian Agnew
I think I'll go for python...thanks
atrent
A: 

SOLVED:

cat $MBOX|formail -des ../dumpFile.sh # split the mbox in many messages

for mail in * # cycle on every message do echo =========================== FROM=$(../extractFrom.sh $mail |tr -d "<"|tr -d ">"|tr -d "/" |sed 's/@/-AT-/'|tr "." "-"|sort|uniq) # get address for file in $(munpack $mail |cut -f1 -d" "|tr -s "_" "-") # extract attachments and prepend address do echo ln $file utente:${FROM}_tipo:$file # whatever done done

where dumpFile.sh is just: cat >$(mktemp -p .)

and extractFrom.sh is an awk script to get the email address

atrent