refactoring manage mails.

This is a refactoring PR, purpose is to:
-  use private / public for methods
- move the database queries from the controller to related subs file.

Please review carefully, I don't have a proper test system set up in order to test the mail queue functionality.

Note: This is just a sample, If you like it I can pick up and redisign other files too:)