Refactoring mails, tasks

Another quick refactoring PR. Make lower level functions for mail related queries, add reduce mail queue action handler, fix leftover missing includes of tasks controller.