# Description:
#   This script attempts to send an email using 'netcat'
#
#   The better solutions are to write or find a Java SMTP mail sender or
#   to set up a 'sendmail' program on the local server.
#
# Examples:
#   bashmail-better.sh matth3wbishop@yahoo.com "file=msg.txt" "mat@ella-associates.org" "testing bashmail"
#     This sends an email message to my yahoo account specifying sender as mat@ella... and subject
#     as "testing bashmail". Also, the message is got from the file 'msg.txt'
#   bashmail-better.sh matth3wbishop@yahoo.com "hello everyone" 
#     Sends an email with subject 'none' send 'not@specified.com' and sends the message 'hello everyone'
#
# 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. This is an improvement on the 
#   bashmail script which doesnt actually send the email but just constructs the 
#   protocol string.
#   
#   An alternative to this script would be to use the JavaMail Api, to send the email
#   which may in fact be much easier
#
#   The script should probably 'retry' to send the email in a few minutes if the mail
#   box or Smtp Server happens to be unavailable at a particular time
# Parameters
#   mailRecipient required: 
#    This is the person the mail is going to be sent to.
#   emailMessage  optional:
#    This is the body of the email message or, if the text starts with the text 
#    "file=" then this parameter specifies a text file which contains the body
#    of the email message
#   emailSender   optional:
#    This is the person who is sending the email
#   smtpServer    optional:
#    This is the domain name of the SMTP server where the email should be sent through
#
# Dependencies:
#   nc or netcat  
#     This program sends the protocol strings to the correct tcp socket, that is 
#     across the internet.

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

sMessageFile="none"
sMessage="none"

if [ "$2" != "" ]
then
  sMessageParam="$2"  
  if [ "$sMessageParam" != "$(echo $sMessageParam | sed 's/file=//i')" ]
  then
    sMessageFile="$(echo $sMessageParam | sed 's/file=//')"
    sMessage="none"
  else
    sMessageFile="none"
    sMessage="$sMessageParam"
  fi
else
  echo "usage: $0 mailRecipient messageBody [emailSender] [emailSubject] [smtpServer]"
  cat $0 | sed -n "/^[ ]*#/p" 
  exit 1;
fi

if [ "$3" != "" ]
then
  sMailSender="$3"
else
  sMailSender="not@specified.com"
fi

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

if [ "$5" != "" ] 
then
  sSmtpServer="$5"
else
  sSmtpServer="mx1.mail.yahoo.com"
  # mx1.mail.yahoo.com  is the yahoo one
  # findesign.org is nicks
fi

if [ "x" = "y" ] 
then
  echo "
    sMailRecipient=$sMailRecipient
    sMessageParam=$sMessageParam
    sMessage=$sMessage
    sMessageFile=$sMessageFile
    sMailSender=$sMailSender
    sMailSubject=$sMailSubject
    sSmtpServer=$sSmtpServer"
  exit 1;
fi



(\
echo "HELO ella-associates.org";
echo "MAIL FROM:<$sMailSender>";
echo "RCPT TO:<$sMailRecipient>";
echo "DATA";
#-- This sleep statement is crucial. Otherwise the Smtp Server doesn't seem to be
#-- able to keep up with all the dialog that is being spewed at it.
sleep 1;
echo "subject: $sMailSubject ";
echo "from: <$sMailSender> ";
echo "to: <$sMailRecipient> ";
echo "date: $(date) ";
#-- In some circumstances the line below will cause the body of the message 
#-- text not to be displayed
#echo "content-type: text/html;"
echo " ";
if [ "$sMessageFile" != "none" ]
then
  cat $sMessageFile
else
  echo "$sMessage";
fi

sleep 1;
printf "\r\n.\r\n";
#-- This doesnt work with yahoo
#echo ".";
#echo "quit";) 
echo "quit";) | \
nc -v -v $sSmtpServer 25