in reply to Re: Premature end of script headers
in thread Premature end of script headers

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!!!
  • Comment on Re: Re: Premature end of script headers

Replies are listed 'Best First'.
Re: Re: Re: Premature end of script headers
by Cody Pendant (Prior) on Aug 01, 2003 at 06:36 UTC
    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";
        Well if that's what you're using then you've just got the code in the wrong order.

        You can't print to MAIL before it's opened.

        Do the open() thing first, then all the prints.



        ($_='kkvvttuubbooppuuiiffssqqffssmmiibbddllffss') =~y~b-v~a-z~s; print
Re: Re: Re: Premature end of script headers
by Limbic~Region (Chancellor) on Aug 01, 2003 at 06:24 UTC
    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');
        Nickd_69,
        Ok - you have forgetten to use Mail::Mailer which might just be a typo on your part. I would suggest reading the docs - it can use Sendmail if you want by replacing 'smtp' with 'sendmail'.

        Additionally - you are printing to the MAIL filehandle, but haven't opened it anywhere. You don't want to do that. Move the my $mailer = stuff before any of the print statements and then change all the MAIL lines to $mailer.

        You also didn't create the CGI object used to redirect the page later.

        You really want to look into hash slices. It could make your life a lot easier. The problem is that unless you have done form validation using JavaScript - you might be attempting to print an uninitalized variable. See my update to your code below:

        #!/usr/bin/perl -wT use CGI; use CGI::Carp qw(fatalsToBrowser); use Mail::Mailer; CGI::ReadParse(); my $query = CGI->new(); 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 "$in{'Title'}\n"; print $mailer "$in{'Name'}\n"; print $mailer "$in{'Position'}\n"; $mailer->close; print $query->redirect('http://www.travancoresch.vic.edu.au/developmen +t/reply02.html');
        That should move you along I think, still don't have a web server to test it on though. What were the errors you got with my code anyway?

        Cheers - L~R