Ping test sends mail when ping fails

help with bash script!

im am working on this script to make sure my server will stay online, so i made this script..

HOSTS="192.168.138.155"
COUNT=4

pingtest(){
  for myhost in "$@"
do
  ping -c "$COUNT" "$myhost" &&return 1
done
return 0
}
if pingtest $HOSTS

#100% failed
echo "server failed at $(date)" | mail -s "server is down" mymail..
echo "host ($HOSTS) is down (ping failed) at $(date)"
else
echo "everyting is fine"
fi

but its sending me a mail even when there is connection to the server, and i only want it when the ping fails..
i have made the script run every 5 min. with crontab -e 5 * * * * filelocation

can someone please help me with this?
sorry if this is the wrong place to ask..

The return code of your function is the wrong way: 0 indicates success; 1 (better: anything but 0) indicates error.
And, there's a then missing for the if construct.
And, there might be a logic error: the for loop would work on a list of hosts and return success if any of them would ping .

try:
change

if pingtest $HOSTS

to

pingtest $HOSTS
if [ $? -eq 1 ] ; then

You also are missing the "then" keyword.

thanks, but ist still not working, it keeps sending mail, even when the ping is working

Post the xtrace log (option -x ) of your script running.