[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index] [Thread Index]

Re: how to extract attachments from an email?



Adam Galant wrote:
> > > I need a script to extract all attachments from emails and do an
> > > action an each of them (scp to a different machine). How to do
> > > this?
> Yes, messages come directly from MTA. I know procmail is my friend here
> ;-) but I still don't know how to handle attachments here. A quick recipee
> will be of great help.

As it happens I was needing to do something along those lines myself
just recently.  (Running scripts on spam messages quarantined as a
MIME attachment.)

  apt-cache show mpack

  munpack -t < /tmp/message
  munpack: reading from standard input
  part1 (text/plain)
  part2 (text/html)

In a script you probably want -q to keep the noise down and possibly
-C to work in a temporary directory.  Something like this should get
you going.  This is UNTESTED!  And too terse for general use.

  #!/bin/sh
  trap 'rm -rf $TMPDIR' 0
  TMPDIR=$(mktemp -d -t yourscriptname.XXXXXXXXXX) || exit 1
  munpack -q -t -C $TMPDIR || exit 1
  for file in $TMPDIR/*; do
      : some action on $file
  done
  exit 0

Bob

Attachment: pgpSrT6bfAyfq.pgp
Description: PGP signature


Reply to: