#!/bin/sh

###################################################
# Author: Michael Simmer
# Date: 23.10.2010
# Version: 0.2
# Contact: http://www.zeux.at/
###################################################


netcat="/vmfs/volumes/primary/custom/netcat"
tmp="/tmp/mail"
hostname=`hostname`

if [ -f $netcat ]
then

	if [ $# -eq 5 ] 
	then
		echo -e "HELO $hostname\r" > $tmp
		echo -e "MAIL FROM: $2\r" >> $tmp
		echo -e "RCPT TO: $3\r" >> $tmp
		echo -e "DATA\r" >> $tmp
		echo -e "From: $2\r" >> $tmp
		echo -e "To: $3\r" >> $tmp
		echo -e "Subject: $4\r" >> $tmp
		echo -e "\r" >> $tmp
		if [ -f $5 ]
		then
			awk '{printf("%s\r\n", $0);}' < $5 >> $tmp
			#cat $5 >> $tmp
			#echo -e "\r" >> $tmp
		else
			echo "Error: File not found!"
			rm $tmp
			exit 1
		fi
		echo -e ".\r" >> $tmp
		echo -e "quit\r" >> $tmp
	
		$netcat -i 1 $1 25 < $tmp
	
		rm $tmp
	else
		echo "Usage: ./sendmail.sh <server> <from> <to> <subject> <file>"
		exit 1
	fi
else
	echo "Error: netcat Binary not found!"
	exit 1
fi
