moved email logic to a separate method