mail functino refactoring