email insert/update logic