Crobjob Script to Email if Problem Found

I have a script that runs in cron twice every hour. My question is, when this script finds an error and sends an email on that error, and the error recovers

how do i make the script only email when there's a problem and also when there's a recovery for that problem?

simply inserting a "mail -s ....." in front of the command will send out an email with EVERY output from the script. this isn't what i need.

i want the script to send an email out to whomever i tell it to, only when an occurrence of an error is found in a file, and when that error has cleared (enough time passes and that alert didn't reoccur).

makes sense?

please help.

thank you

Generate a specific file output if you want to sent mail, then

file=/tmp/hari/mailcontext.txt
if [ -f $file ] ; then
cat $file | mailx -s "ALERT!!!" -r $REPLYTO -c "$MAILCC" -b $MAILBB $MAILTO
rm $file
fi

I usually do something like this...

blah
blah
blah
[ $all_is_ok ] && exit  # exit if all is well

mailx -s oops user << EOF

message here.
do a command?

$(date)
$(cat error_file)

EOF