optimize the logic of mail