#!/usr/bin/bash
#
#
# sending mail with attachments
# requires: 
#  mailx 
#  perl (with -MMIME::Base64)
#  awk, cat, grep, wc, sed
#  /tmp for temporary files
#
#


#set -x 


# maximum filesize of single attachment in bytes
# 10.000.000 bytes
MAX_FILESIZE=10000000
# maximum total size of mail
# 12.000.000 bytes
MAX_MAILSIZE=12000000

show_usage()
{
    echo "usage $0: "
    echo "$0 -t mail@recipient.org [-s subject] [-b File] File1 File2 ..."
}

# Parameter 1 File
get_filetype()
{
   m_CONTENTTYPE="text/plain"
   is_compressed=$(file "$1" | grep "compress" | wc -l | sed 's/ //g')
   if [[ "$is_compressed" = "1" ]]; then
       m_CONTENTTYPE="application/zip"
   fi
}

# Parameter 1 File
check_filesize()
{
    filesize=$(ls -lart $1 | awk '{ print $5 }')
    if (($filesize > $MAX_FILESIZE)); then
        echo "ERROR: File $1 is too big (size=$filesize, maximum allowed=$MAX_FILESIZE)" >&2
        return 1
    fi
}

# prueft die groesse der Mail insgesamt
check_mailsize()
{
    filesize=$(ls -lart $TMPFILE | awk '{ print $5 }')
    if (($filesize > $MAX_MAILSIZE)); then
        echo "ERROR: eMail is too big (size=$filesize, maximum allowed=$MAX_MAILSIZE)" >&2
        exit 2
    fi
}


TMPFILE=/tmp/mymail.$$
declare -a ATTACHMENT

if [ -e $TMPFILE ]; then
    rm -f $TMPFILE
fi

echo "Parameters: $@ "

cmdpar=""
for param in "$@"
do
    if [ "$cmdpar" = "SUBJECT" ]; then
        echo "writing subject: $param"
        m_SUBJECT="$param"
        cmdpar=""
        continue
    fi
    if [ "$cmdpar" = "TO" ]; then
        echo "recipients: $param"
        m_TO="$param"
        cmdpar=""
        continue
    fi
    if [ "$cmdpar" = "BODY" ]; then
        echo "body: FILE=$param (if FILE = - then stdin is read)"
        cmdpar=""
        m_BODY="$param"
        continue
    fi

    case "$param" in
        "-s") cmdpar="SUBJECT";;
        "-t") cmdpar="TO";;
        "-b") cmdpar="BODY";;
        *)    cmdpar=""
              if [ ! -r "$param" ]; then
                  echo "File $param does not exist or is not readable" 
              else 
                  att_idx=${#ATTACHMENT[*]}
                  ATTACHMENT[$att_idx]="$param"
                  echo "adding file $param as ATTACHMENT NR. $att_idx = ${ATTACHMENT[$att_idx]}"
              fi
              ;;
    esac

done

# Parameter check
if [ -z $m_TO ]; then
    echo "Parameter -t is required." >&2
    show_usage $*
    exit 1
fi

# creating the mail
cat /dev/null >$TMPFILE

#############
# Mail header
{
    #echo "FROM: "
    echo "TO: $m_TO
SUBJECT: $m_SUBJECT
MIME-version: 1.0
Content-type: multipart/mixed; boundary=\"fron_$$_tier\"
  
This is a message with multiple parts in MIME format.
--fron_$$_tier
Content-type: text/plain
  
"
} >>$TMPFILE


#############
# Mail body  
{
    if [[ "$m_BODY" = "-" ]] || [[ -r "$m_BODY" ]]; then
        cat "$m_BODY"
    fi
    echo 
} >>$TMPFILE

#############
# Mail Attachments 
for i in "${ATTACHMENT[@]}"
do
   echo "working on attachment \"${i}\"" 
   check_filesize "$i"
   if [[ "$?" = "1" ]]; then
       continue
   fi
   get_filetype "$i"
   {
       echo "--fron_$$_tier"
       echo "Content-type: $m_CONTENTTYPE
Content-Disposition: attachment; filename=$(basename $i);
Content-transfer-encoding: base64

"
   } >>$TMPFILE
   perl -MMIME::Base64 -e' open(FILE, $ARGV[0]) or die "$!";while (read(FILE, $buf, 60*57)) {print encode_base64($buf);}' "$i" >>$TMPFILE

   echo  >>$TMPFILE

   check_mailsize
done


#############
# Mail footer
{
    echo "--fron_$$_tier--

"

} >>$TMPFILE

   check_mailsize

# perl -MMIME::Base64 -e' open(FILE, $ARGV[0]) or die "$!";while (read(FILE, $buf, 60*57)) {print encode_base64($buf);}'
# sending the eMail
cat $TMPFILE | mailx -t
rm -f $TMPFILE


