Added unit tests for mail templates and a lot of fixes