# Description:
#   This script attempts to send an email using 'netcat'
#   It has the disadvantage that a delay interval for 'netcat' is necessary.
#   This means that the script will take quite some time to return, which
#   may be unacceptable from a web-page for example. One solution is to 
#   run the script in the background with &, but thats a bit dodge.
#
#   The better solutions are to write or find a Java SMTP mail sender or
#   to set up a 'sendmail' program on the local server.
#
# Notes:
#   The script can only send mail to the 'localhost' because it does
#   no SMTP authorization, and therefore it can only send mail to somebody
#   who has a mail box on the local computer.


if [ "$1" != "" ]
then
  sMailRecipient=$1
else
  echo "usage: $0 mailRecipient messageBody [mailSender] [mailsubject]"
  cat $0 | sed -n "/^[ ]*#/p" 
  exit 1;
fi



if [ "$2" != "" ]
then
  sMessage=$2  
else
  echo "usage: $0 mailRecipient messageBody [mailSender] "
  cat $0 | sed -n "/^[ ]*#/p" 
  exit 1;
fi

if [ "$3" != "" ]
then
  sMailSender=$3
else
  sMailSender="unknown@nowhere.com"
fi

if [ "$4" != "" ] 
then
  sMailSubject=$4
else
  sMailSubject="none"
fi

#-- Instead of echoing all of the following text into netcat
#-- I am going to try to use the -e switch to netcat with this script name
#-- Otherwise the delay of 1 second between each output line causes the email
#-- to take to long to send (one second for each line of the email plus headers).
echo "HELO ella-associates.org";
echo "MAIL FROM:<$sMailSender>";
echo "RCPT TO:<$sMailRecipient>";
echo "DATA";
sleep 1;
echo "subject: $sMailSubject "; 
echo "from: <$sMailSender> ";
echo "to: <$sMailRecipient> ";
echo "date: $date ";
echo "content-type: text/html;"
echo " ";
echo $sMessage;
#sleep 1;
printf "\r\n.\r\n";

echo "quit"
#| netcat -v -v -i1 localhost 25