#!/bin/sh

# We want to send an email which has gone through a pipe additionally to a certain recipient
# TODO: pass pattern and recipient as argument

###### customize according to your needs ########
FILE=/tmp/mail_$$.eml
PATTERN="fortune"
ID="$(/usr/bin/mailq|grep -B 1 $PATTERN |/usr/bin/awk '{ print $1 }'|/usr/bin/head -n 1| /bin/sed -e 's/*//')"
RCPT="info@example.de"
#############

if [ -z $ID ]
then
  exit 0
fi

SENDER=$(/usr/bin/mailq|grep -B 1 "$PATTERN" |/usr/bin/head -n 1|/usr/bin/awk '{ print $7}')
/usr/sbin/postsuper -h $ID >/dev/null 2>&1
/usr/sbin/postcat -qbh $ID > "$FILE"
/usr/sbin/sendmail -f "$SENDER" "$RCPT" < "$FILE"

