#!/bin/sh

if [ $# -ne 4 ]; then
    echo "$0 subject from bcc-addr addressfile"
    echo "f.i. $0 'How are you?' 'Some Body <from@example.com>' 'another@example.com' ./addresses "
    echo "Address file format: | Name | Mail |"
    exit 1
fi

subject=$1; shift
from=$1; shift
bcc=$1; shift
addresses_file=$1; shift


while read line; do
    name=$(echo $line | awk -F '|' '{ print $2 }' | sed -e 's/^ *//' -e 's/ *$//')
    email=$(echo $line | awk -F '|' '{ print $3 }' | sed -e 's/^ *//' -e 's/ *$//')

    sed "s/PERSON/$name/" mail | \
        mail -s "$subject" \
             -r "$from" \
             -b "$bcc" \
             "$name <$email>"
done < "$addresses_file"