refactored sending email, added logging