Refactor Email sending function