In your code where you're printing out an error message for the "format being improper", you have to backslash-escape the <body> tag.
You have:
print("<HEAD><TITLE>....
....
....
....
<body bgcolor="black" text="red"...>");
the quotes in the <body> tag are messing up the print() statement.
Escape them like this:
<body bgcolor=\"black\" text=\"red\"...> and so forth.
--Ryan