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
In my case (referencing a PHP file in the top folder of a Wordpress plugin) I had to change the permissions on that folder. My test environment was fine, but when deployed the folder had 775. I changed it to 755 and it works fine.
to fix this, I had to change permissions for whole directory to 755 (777 did not work for me), and changed file owners for whole directory
chmod -R 755 public_html
chown -R nobody:nobody public_html
nobody is user that runs php in my computer.
I have this issue and resolve this by changing PHP version from 5.3.3 to 5.6.30.