From: jblane8 on
Hi, all,

This seemed like a basic task when I first searched for a solution,
but I'll be danged if I didn't have a hard time finding a direct
answer, so I'm posting this to make it easier (I hope) for others to
find:

So the idea in a nutshell is to use formail to split and feed the
messages from the spool into procmail, which reads a specified recipe
file for the search criteria:

prompt> cat /var/spool/mail/someuserzspool | formail -s procmail -pm /
root/extracter

/root/extracter should contain something like:

LOGFILE= /root/extracter.log
VERBOSE=yes
LOGABSTRACT=all
COMSAT=off

:0:
* Subject:.*Enough text to match a single message
/root/recovered

:0
/dev/null


It's important to send all unmatched messages to /dev/null, else
they'll be delivered to root's mail spool. Using verbose logging makes
a log entry for every message, and will let you know what went wrong
if your recipe didn't get the results you wanted.

Then you can use something like this to send the extracted message to
someone else:

prompt> cat /root/recovered | formail -rk -I "To:
recipient_alias_or_address" | sendmail -t

In the above example the message will appear to come from root. Also,
any binary attachments will get corrupted and possibly appear in the
body of the message, but I think you can play with the formail command
to fix that.

Good luck!

JBlane