Help with writing simple bash script

I want to write a bash script to:

  1. Send an email from localhost to an external gmail account. (gmail then automatically forwards the message back to a pop account on the same server.

  2. Script waits 3 minutes then checks to see if the email arrived, and if not, it sends an email to administrator.

Can anyone point me in the right direction? My knowledge of writing scripts is limited and I'm hoping someone can help.

Thanks so much,

Sallyanne

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.

Appreciate any help you can offer.

Regards,
Sallyanne

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!

Regards,
Sallyanne

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

Thank you so much for your reply. You are a star!

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

Can you please help with this?

Thank you again.

Regards,
Sallyanne.

Untested but I see a missing quote here:

UNIQUE=$$`date +"%Y%m%d%H%M%S"`

Later in the script I think that the mailbox directory search is faulty or the file has not been created (needs a Red Hat expert to comment).

Also you should replace the word mailbox with the actual user that is receiving the email, and confirm the actual location of the mailbox.