Moved sending email to a separate function