#!/bin/sh

# OK, I have a script that sends mail notification, designed to be run from a
# .forward (or .qmail) file (in addition to whatever else you use for delivery). 
# I'll puff it here (long puff, beware).
# 
# It works with Qmail; it may not work with other systems.  It needs either a
# "$RECIPIENT" variable or a "Delivered-To:" header.  It relies on formail
# (which comes with the procmail package).

export PATH="/bin:/usr/bin:/usr/local/bin:/ofb/bin:$HOME/bin"

email_subject=""
email_from="$SENDER"
email_rcpt="$RECIPIENT"
email_content=""
formail -cz | 
(       while read one two ; do
                case "$one" in
                Delivered-To:)  [ -z "$email_rcpt" ] && email_rcpt="$two" ;;
                Subject:)       email_subject="$two" ;;
                From:)          email_from="$two" ;;
                To:)            email_to="$two" ;;
                esac

                if [ -z "$one" ]; then
                        buf=""
                        while [ ${#buf} -lt 80 ] && read line; do
                                email_content="$buf"
                                [ -z "$buf" ] && buf="$line" || 
                                buf="$buf $line"
                        done
                        break
                fi
        done

        IFS='@'
        set -- $email_rcpt
        if [ 2 != $# ]; then
                echo "Cannot decode address: $email_rcpt" >&2
                exit 1
        fi

        email_rcpt_domain="$2"
        IFS='-'
        set -- $1
        email_rcpt_user="$1"
        email_rcpt_extra="$2"

gsend -PC \
"@${email_rcpt_domain}/user/${email_rcpt_user}/mail/${email_rcpt_extra}" \
"${email_rcpt_user}@${email_rcpt_domain}" \
<< PUFF
From: $email_from
To: $email_to
Subject: $email_subject
$email_content
PUFF

)

exit 0
 
