#!/bin/bash
set -bm

###
mail_from=cron-mail-check@streamkid.domain
mail_helo_reply="250 mailgate.streamkid.domain"
mail_ok_from_reply="250 2.1.0 Ok"
mail_ok_to_reply="250 2.1.5 Ok"
mail_data_reply="354 End data with <CR><LF>.<CR><LF>"
mail_domain=gdb.gr
mail_to=streamkid@gmail.com
mail_srv=mailgate.streamkid.domain
mail_txt="New mail arrived"
###

function tx_rx() {
	echo $1 >&9
	read -r tmp <&9
	echo $tmp;
}

function send_mail()
{
	exec 9<>/dev/tcp/$mail_srv/25
	if [[ $(tx_rx "HELO $mail_domain") != $mail_EHLO_REPLY ]]; then	echo "send_mail()->ehlo_rejected! :-(";	exit 2;	fi
	if [[ $(tx_rx "Mail From: $mail_from") != $mail_FROM_REPLY ]]; then echo "send_mail()->mail_from_rejected! :-("; exit 2; fi
	if [[ $(tx_rx "Rcpt to: $mail_to") != $mail_TO_REPLY ]]; then echo "send_mail()->mail_rcpt_to_rejected! :-("; exit 2; fi
	if [[ $(tx_rx "Data") != $mail_data_reply ]]; then echo "send_mail()->data_rejected! :-("; exit 2; fi
	cat $mail_txt >&9
	tx_rx "."
	9>&-
	9<&-
}

