updated version with support for sending emails