#!/bin/sh

# This is a shell script to emulate the standard mail(1) aka mailx(1)
# aka Mail(1) tool usage for sending emails. This shell script is not
# a client for checking or reading mail. Also, this version of mail
# does not touch any mail boxes.

# Copyright (c) 2003 by Jeremy C. Reed
# This software is provided "as is" without any warranties. You may
# redistribute and use this source, with or without modification,
# as long as you include this copyright and disclaimer.

# To do: some switches to consider:
# -v  display details of delivery
# -E  do not send empty messages
# -c  send carbon copies to users
# -b  send blind carbon copies

# todo: test if read can read in long enough lines
# some shells may have limit of 256 characters per read?
 
SENDMAIL=sendmail  # set to path of your sendmail(8) command
SENDMAIL_ARGS="-t -i" # -i is so dot doesn't terminate message
                      # -t is to get recipients from headers

unset subject
unset to_addr
dontsendempty=0

about ()
{
  echo "This is mailx.sh version 0.03. This is simple, shell script"
  echo "to implement mailx(1) functionality for sending mail. It does"
  echo "not provide an interface for checking or reading email messages."
  echo

  # maybe do this without forking a command
  mailname=$( basename $0 )

  if [ -n "$1" ] ; then
    echo "$mailname: $1" 1>&2
  fi

  echo "Usage: $mailname [-s subject] to-addr ..." 1>&2

  if [ -n "$1" ] ; then
    if [ -n "$2" ] ; then 
      exit $2
    fi
    exit 100
  fi

  exit 0
}

if [ $# -eq 0 ] ; then
  about "Sorry, this is not a mail reader." 1
fi

while [ -n "$1" ] ; do
  case "$1" in
  -*)
      if [ "$1" = "-s" ] ; then
        if [ -z "$2" ] ; then
          about "The \"-s\" option requires an argument." 4
        fi
        subject="$2"
        shift
      elif [ "$1" = "-u" -o "$1" = "-f" ] ; then
        about "Sorry, this is not a mail reader." 2
#      elif [ "$1" = "-E" ] ; then
# this probably needs to have entire message saved to temp file first
#        dontsendempty=1
      else
#        about "unknown option -- $1" 3
        about "Switch \"$1\" is not implemented." 3
      fi
      ;;
  *)  if [ -n "$to_addr" ] ; then
        to_addr="${to_addr}, $1"
      else
        to_addr="$1"
      fi
      ;;
  esac
  shift
done


if [ -t 0 ] ; then   # no piped in standard input
  if [ -z "$subject" ] ; then
    echo -n "Subject: "
    OLD_IFS=$IFS
    IFS=""
    read -r subject
    IFS=$OLD_IFS
  fi
fi

if [ -z "$to_addr" ] ; then
  about "No recipients specified." 6
#  about "You must specify recipients." 6
fi

# generate SMTP headers

# mail(1) doesn't seem to set date
#date +"Date: %a, %e %b %G %T %z"
## maybe pipe though sed to get rid of extra space
## before single digit day of month?

{
# if [ -n "$to_addr" ] ; then
echo "To: ${to_addr}"
# fi

if [ -n "$subject" ] ; then
  echo "Subject: $subject"
fi

# end of headers
echo ""

# output
line_count=0
OLD_IFS=$IFS
IFS=""
while read -r line ; do
  # finish if manual, non-piped input is a period on a line by itself
  if [ "$line" = "." -a -t 0 ] ; then
      break
  fi
  echo "$line"
  line_count=$(( line_count + 1 ))
done

IFS=$OLD_IFS

if [ -t 0 ] ; then   # no piped in standard input
  echo "EOT" 1>&2
fi

# later add switch to not send mail by exiting
if [ "$line_count" -eq 0 ] ; then
#  if [ "$dontsendempty" -eq 1 ] ; then
# todo this, have it save to temp file and then send to sendmail as needed
#    # No message, so just exit cleanly
#    exit 0
#  fi
  if [ -z "$subject" ] ; then
    echo "No message, no subject; hope that's ok" 1>&2
  else
    echo "Null message body; hope that's ok" 1>&2
  fi
fi

} | $SENDMAIL $SENDMAIL_ARGS

