I get a \"Premature end of script headers: contactform.cgi\" error message when running the below script. What frustrates me is that I ran this as a .php on another server
Check your line endings! If you see an error about the file not being found, followed by this "premature of end headers" error in your Apache log - it may be that you have Windows line endings in your script in instead of Unix style. I ran into that problem / solution.