Put some unique string into the email, and write a script with a loop in it that sleeps for 10 seconds, The script should check the recipients mail box using grep for the unique string.
#!/bin/ksh
i=1
while [ $i -lt 18 ]
do
grep mystring /var/spool/mail/mailbox
if [ $? -eq 0 ]
then
echo found
exit 0
else
echo not found
fi
i=`expr $i + 1`
sleep 10
done
mail -s "not received" admin <<EOF
email not recieved in 3 minutes
EOF
Thanks for your advice. I'm a little confused as to how to specify the sending of the email in the first place. I can see in your example the sending of email if the recipient does not receive the email, but how to specify the sending of the first email with the unique string?
Sorry if this seems obvious, I'm new and just getting my head around things.
I may not be able to give the final answer but "jgt" has given the key to the solution and I may be able to help others by asking questions.
Please state what Operating System you have and what mail client you are running?
Can you send a mail by using say "mail" or "mailx" from the command line? If so, what do you type? In general if you can type it at the command line, it can be scripted.
To send an email, with a unique string (provided not more than one email per second is generated.)
#set UNIQUE to Process ID + current date and time
UNIQUE=$$`date +"%Y%m%d%H%M%S`
echo $UNIQUE >>output.log
mail -s "Test Response Time" someone@gmail.com <<EOF
Remarks:$UNIQUE
Sent at `date`
EOF
I am running Redhat Enterprise 5 and running gmail as the MTA. Yes, I can send an email via the command line using the mail command.
The real problem I'm having now is I really don't know how to tie all the bits and pieces together which I need to make this email monitoring script work
I know it's a big ask (maybe too big) for someone to provide more specific and final code which I can use.
Thanking you all for your advice and assistance. It's a steep learning curve for me!
Assuming that you want to run this test on demand, just join the two scripts together as:
#!/bin/ksh
#set UNIQUE to Process ID + current date and time
UNIQUE=$$`date +"%Y%m%d%H%M%S`
echo $UNIQUE >>output.log
mail -s "Test Response Time" someone@gmail.com <<EOF
Remarks:$UNIQUE
Sent at `date`
EOF
#test to see if received
i=1
while [ $i -lt 18 ]
do
grep $UNIQUE /var/spool/mail/mailbox
if [ $? -eq 0 ]
then
echo Received
echo $UNIQUE `date` >>output.log
exit 0
else
echo Not yet
fi
i=`expr $i + 1`
sleep 10
done
mail -s "not received" admin <<EOF
email not recieved in 3 minutes
EOF
I tried to execute the code you gave however and my Redhat 5 box reported:
emailmonitor.sh: command substitution: line 3: unexpected EOF while looking for matching `"'
emailmonitor.sh: command substitution: line 4: syntax error: unexpected end of file
grep: /var/spool/mail/mailbox: No such file or directory
Not yet
expr: syntax error