fix mail sender error and refactor code