Improved mailsender function. SMTP functionality separated, so that SMTP can be mocked for unit test.