How to send out emails based on conditions?