Updated the script. Using functions to format the mail etc.