write log and send mail