The print "\n";
line isn't absolutely necessary. Yeah, it'll make your HTML markup look nicer when people use view-source, but it won't affect the rendering of the page at all and any developer tool will reformat the markup anyway.
That said, this is pretty much the exact same code I use to look through attachments as well ...