Refactoring and fixes for send email functionality