#!/bin/sh
# 
# Command to send to multiple email addreses simultaneously, separated by commas.
# However, if any email address has a colon in it, then the second email address overrides the first
#
# Ex:
#	user1@example.com,user2@example.com:user3@example.com,user4@example.com
#
# This will send to user1, user3 (overriding user2), and user4.
#
# Command line definition from Nagios:
# /usr/bin/printf "%b" "***** Nagios Monitor XI Alert *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" | \
# $USER1$/pu-notify-all-emails -s "** $NOTIFICATIONTYPE$ Service Alert: $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ **" \
#	-t $CONTACTEMAIL$:$_HOSTALT_CONTACT$,$CONTACTADDRESS1$

PATH=/usr/bin:/bin

subject=""
sendto=""

# Get params for subject and receiver
while [ -n "$1" ]; do
  case "$1" in
    -s) subject="$2"; shift 2;;
    -t) sendto="$2"; shift 2;;
    *) shift 1;;
  esac
done

# If we have a blank subject or a blank receiver, then exit
[ -z "$subject" -o -z "$sendto" ] && exit

output=""
output=`cat`

OLD_FS=$IFS
IFS=,
for receiver in $sendto; do
  [ -z "$receiver" ] && continue
  override=`echo $receiver | cut -d ":" -f 2`
  [ -n "$override" ] && receiver=$override
  echo "$output" | /bin/mail -s "$subject" $receiver
done
IFS=$OLD_FS
