fix bugs and add the part to send email