Email shell script return code

Hi All,

I'm new to unix and I have a requirement to execute a shell script and get the return code/Exit code and send it in email. Meaninig we execute a script which in turn execute a shel script and fetches it\s return code and send it to email. I tried the code below as :

output_file=$(mktemp /var/tmp/TestM/Program/script.X1.out)
php /var/tmp/TestM/CATProgram/MLookup.sh >>"$output_file"
ret=$?
if [ $ret -ne 0 ]; then
  echo "MLookup.sh failed (status $ret), see the output in $output_file"
fi

I know that we can send email using mail -s "text". But the above script is not saving the return code in the file. Please help on the above requirement.
Thanks,
Midhun

output_file=$(mktemp /var/tmp/TestM/Program/script.X1.out)
php /var/tmp/TestM/CATProgram/MLookup.sh >>"$output_file"
ret=$?
echo "Return code=$ret" >> "$output_file"
if [ $ret -ne 0 ]; then
  echo "MLookup.sh failed (status $ret), see the output in $output_file"
fi

Is this what you meant?

Thanks for the help in code. I also want to email the saved return code from output file. Can you pls help on how to modify the script ?