in reply to Premature end of script headers

Nickd_69,
Welcome to the Monastery! Since you will very likely get many responses about using CGI to do your argument parsing, let me point out a couple other things:
  • select can be used to select your default output file handle so you would not have to keep typing print MAIL
  • Invoking a sub-routine as &subroutine instead of subroutine() exposes the sub to the current @_ and is probably not what you want when debugging problems.
  • Try to get in the habit of using warnings (use warnings;) and also strict (use strict;) since it will point out lots of errors for you. Perhaps you might also like use diagnostics;
  • Take a look at Variable Scoping basics from our Tutorials section and the external Coping with Scoping by Dominus. Your use of globals will likely make debugging harder when you find that you have inadvertently used the same variable name and stepped on it elsewhere
  • Finally, reading the Perlmonks FAQ will save you a lot of headache in not getting the answers you are seeking. There is a LOT of information here.

    Cheers - L~R

  • Replies are listed 'Best First'.
    Re: Re: Premature end of script headers
    by Nickd_69 (Novice) on Aug 01, 2003 at 05:22 UTC
      I have read nearly every thing off your site that I can to do with this problem but nothing seems to fix it. I did not write the script myself but I understand it all except for the header part. This is my first crack at perl and all I want is a simple answer on how to fix it. Sometimes I can get it working but all it does is send the email with none of the data in. PLease help cause I am bashing my head against a wall to get this fixed!!!
        I just ran it on my server, as you've posted it here, and it ran just fine, no "end of headers" message, but as you say, sent an email with no content.

        That's pretty much what you'd expect running just that script by itself, because there's no form feeding it with information.

        Here's a quick fix for your problem.

        Start again.

        Start with this code:

        use CGI; use CGI::Carp qw(fatalsToBrowser); CGI::ReadParse();
        now all the values from your form are in a hash called "%in".

        So if you've got a field called "Title", the contents are now easily accessed because they're in $in{'Title'}.

        Note that there's a case-sensitivity issue here too. If your form field is called "title" then you need $in{'title'} instead.

        You don't need to grab things from the hash and put them into variables, just grab them directly from the hash, i.e. do this

        print MAIL "$in{'Title'}\n";
        in your mail-sending code, rather than doing that bizarre "GETVALUES" thing which is just double handling. Skip that stage altogether. Just go straight to sending the mail.

        And the fatalsToBrowser thing will mean you get useful error messages.



        ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
          I followed your instructions but the only thing I am having trouble with is the sendmail code. What are your suggestions? Thanks for all your help This is what I have so far
          #!/usr/bin/perl -w use CGI; use CGI::Carp qw(fatalsToBrowser); CGI::ReadParse(); print MAIL "$in{'Title'}\n"; print MAIL "$in{'Name'}\n"; print MAIL "$in{'Position'}\n"; print MAIL "$in{'School'}\n"; print MAIL "$in{'Address'}\n"; print MAIL "$in{'Suburb'}\n"; print MAIL "$in{'State'}\n"; print MAIL "$in{'PCode'}\n"; print MAIL "$in{'Email'}\n"; print MAIL "$in{'Phone}\n"; print MAIL "$in{'Fax'}\n"; print MAIL "$in{'PR02'}\n\n"; print MAIL "$in{'PR03'}\n"; print MAIL "$in{'PR04'}\n"; print MAIL "$in{'Att1'}\n"; print MAIL "$in{'Att2'}\n"; print MAIL "$in{'Att3'}\n"; print MAIL "$in{'Att4'}\n"; print MAIL "$in{'Att5'}\n"; print MAIL "$in{'Att6'}\n"; print MAIL "$in{'Att7}\n"; print MAIL "$in{'Att8'}\n"; print MAIL "$in{'Att9'}\n"; print MAIL "$in{'Comments'}\n"; $recipients = "dawson.nicholas.a\@edumail.vic.gov.au"; $mailprog = '/usr/sbin/sendmail'; open(MAIL,"|$mailprog -t"); print MAIL "To: $recipients \n"; print MAIL "From: $Email \n"; print MAIL "Subject: Travancore School Professional Development \n\n" +; print MAIL "Travancore School Professional Development\n\n"; print MAIL "Hi, I have just visited your website.\n\n"; print MAIL "Personal Details\n";
        Nickd_69,
        It looks like the sole purpose of the CGI is to send the form information to an email address and then redirect to a new URL. This could probably be done with just JavaScript.

        I came up with the following code, but have no idea if it will work correctly as I am on a machine without a web server. It is more concise and it should be a lot easier to debug than your original script. I did verify the syntax using perl -c.

        #!/usr/bin/perl -wT use strict; use CGI; use Mail::Mailer; my $query = CGI->new(); my %DATA = $query->Vars; my $body = "Hi, I have just visited your website.\n\n"; $body .= "Personal Details\n"; $body .= join "\n" , @DATA{qw(Title Name Position School Address Subur +b State PCode)}, ''; $body .= "Contact Details:\n"; $body .= "Tel: $DATA{Tel}\n"; $body .= "Tel: $DATA{Fax}\n"; $body .= "Tel: $DATA{Email}\n\n"; $body .= "I would like to register for the following presentations.\n" +; $body .= join "\n" , @DATA{qw(PR01 PR02 PR03 PR04 PR05 PR06 PR07 PR08) +}, ''; $body .= "List of Attendees.\n"; $body .= join "\n" , @DATA{qw(Att1 Att2 Att3 Att4 Att5 Att6 Att7 Att8 +Att9)}, ''; $body .= "Comments:\n"; $body .= "$DATA{Comments}\n\n"; my $mailer = new Mail::Mailer ( "smtp" ); $mailer->open( { To => 'info@travancoresch.vic.edu.au', From => $DATA{Email}, Subject => 'Travancore School Professional Development' } ); print $mailer $body; $mailer->close; print $query->redirect('http://www.travancoresch.vic.edu.au/developmen +t/reply02.html');

        You really should get in the practice of validating form information. I have turned on Taint with the -T shebang line option as a future precaution against accepting user data without sanitizing it first if the script's functionality expands.

        Cheers - L~R

        Update: With some help from the CB, a few minor nits have been corrected

          I tried to make my own code up using the second response and taking the sendmail out of the first lot of code you gave me but I am still having no luck. Could you just have a look at my revised code and give me some thoughts and feedback. I tried the original script but I couldn't even get that working. Thanks heaps for your time. I thought no one would help but you have been more than helpful.
          #!/usr/bin/perl -wT use CGI; use CGI::Carp qw(fatalsToBrowser); CGI::ReadParse(); print MAIL "$in{'Title'}\n"; print MAIL "$in{'Name'}\n"; print MAIL "$in{'Position'}\n"; print MAIL "$in{'School'}\n"; print MAIL "$in{'Address'}\n"; print MAIL "$in{'Suburb'}\n"; print MAIL "$in{'State'}\n"; print MAIL "$in{'PCode'}\n"; print MAIL "$in{'Email'}\n"; print MAIL "$in{'Telephone'}\n"; print MAIL "$in{'Fax'}\n"; print MAIL "$in{'PR01'}\n"; print MAIL "$in{'PR02'}\n"; print MAIL "$in{'PR03'}\n"; print MAIL "$in{'PR04'}\n"; print MAIL "$in{'PR05'}\n"; print MAIL "$in{'PR06'}\n"; print MAIL "$in{'PR07'}\n"; print MAIL "$in{'PR08'}\n"; print MAIL "$in{'Att1'}\n"; print MAIL "$in{'Att2'}\n"; print MAIL "$in{'Att3'}\n"; print MAIL "$in{'Att4'}\n"; print MAIL "$in{'Att5'}\n"; print MAIL "$in{'Att6'}\n"; print MAIL "$in{'Att7'}\n"; print MAIL "$in{'Att8'}\n"; print MAIL "$in{'Att9'}\n"; print MAIL "$in{'Comments'}\n"; my $mailer = new Mail::Mailer ( "smtp" ); $mailer->open( { To => 'dawson.nicholas.a@edumail.vic.gov.au', From => $in{'Email'}, Subject => 'Travancore School Professional Development' } ); print $mailer; $mailer->close; print $query->redirect('http://www.travancoresch.vic.edu.au/developmen +t/reply02.html');