changed PHP_EOL to \n, moved csv string generation inside the try/catch