Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Premature end of script headers

by Nickd_69 (Novice)
on Aug 01, 2003 at 03:13 UTC ( [id://279838]=perlquestion: print w/replies, xml ) Need Help??

Nickd_69 has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone help me??? I cannot get this script running at the moment and I have tried everything. I know it's CHMOD fine and I know I have the right perl dir. It just keeps coming up premature end of script headers. If anyone could help I would be eternally grateful. Heres the code:
#!/usr/bin/perl print "Content-type: text/html","\n"; print "Pragma: no-cache","\n\n"; print &Header; &GETVALUES; &SENDMAIL; ############################# GET INPUT ############################## +#### sub GETVALUES { read( STDIN, $buffer, $ENV{'CONTENT_LENGTH'} ); @pairs= split( /&/, $buffer ); } foreach $pair( @pairs ){ ( $label, $value )= split( /=/, $pair ); $value=~ tr/+/ /; $value=~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $DATA{ $label }= $value; } { $Title = $DATA{ 'Title' }; $Name = $DATA{ 'Name' }; $Position = $DATA{ 'Position' }; $School = $DATA{ 'School' }; $Address = $DATA{ 'Address' }; $Suburb = $DATA{ 'Suburb' }; $State = $DATA{ 'State' }; $PCode = $DATA{ 'PCode' }; $Email = $DATA{ 'Email' }; $Tel = $DATA{ 'Tel' }; $Fax = $DATA{ 'Fax' }; $PR01 = $DATA{ 'PR01' }; $PR02 = $DATA{ 'PR02' }; $PR03 = $DATA{ 'PR03' }; $PR04 = $DATA{ 'PR04' }; $PR05 = $DATA{ 'PR05' }; $PR06 = $DATA{ 'PR06' }; $PR07 = $DATA{ 'PR07' }; $PR08 = $DATA{ 'PR08' }; $Att1 = $DATA{ 'Att1' }; $Att2 = $DATA{ 'Att2' }; $Att3 = $DATA{ 'Att3' }; $Att4 = $DATA{ 'Att4' }; $Att5 = $DATA{ 'Att5' }; $Att6 = $DATA{ 'Att6' }; $Att7 = $DATA{ 'Att7' }; $Att8 = $DATA{ 'Att8' }; $Att9 = $DATA{ 'Att9' }; $Comments = $DATA{ 'Comments' }; } ###################################################################### +###### sub SENDMAIL { $recipients = "info\@travancoresch.vic.edu.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"; print MAIL "$Title\n"; print MAIL "$Name\n"; print MAIL "$Position \n"; print MAIL "$School \n"; print MAIL "$Address \n"; print MAIL "$Suburb \n"; print MAIL "$State \n"; print MAIL "$PCode \n\n"; print MAIL "Contact Details: \n"; print MAIL "Tel: $Tel \n"; print MAIL "Fax: $Fax \n"; print MAIL "Email: $Email \n\n"; print MAIL "I would like to register for the following presentations. +\n"; print MAIL "$PR01 \n"; print MAIL "$PR02 \n"; print MAIL "$PR03 \n"; print MAIL "$PR04 \n"; print MAIL "$PR05 \n"; print MAIL "$PR06 \n"; print MAIL "$PR05 \n"; print MAIL "$PR06 \n"; print MAIL "$PR07 \n"; print MAIL "$PR08 \n\n"; print MAIL "List of Attendees. \n"; print MAIL "$Att1 \n"; print MAIL "$Att2 \n"; print MAIL "$Att3 \n"; print MAIL "$Att4 \n"; print MAIL "$Att5 \n"; print MAIL "$Att6 \n"; print MAIL "$Att7 \n"; print MAIL "$Att8 \n"; print MAIL "$Att9 \n\n"; print MAIL "Comments: \n"; print MAIL "$Comments \n\n"; close MAIL; } sub Header { $Header = <<EOM; <html> <head> <meta http-equiv="refresh" content="0;URL=http://www.travancoresch +.vic.edu.au/development/reply02.html"> </head> </html> EOM }

edited by ybiC: reduced octothorpe lines to 75 chars, to eliminate lateral browser scrolling

Replies are listed 'Best First'.
Re: Premature end of script headers
by Limbic~Region (Chancellor) on Aug 01, 2003 at 03:37 UTC
    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

      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
        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

Re: Premature end of script headers
by bobn (Chaplain) on Aug 01, 2003 at 03:29 UTC

    Since you print a minimal http header, followed by an immediate redirect to some other page, I'd look on that other page, because your browser is going there and not coming back (because that's what you told it to do). GETVALUES will never recieve anything all from the browser.

    You would also want to see what is in your error_log just before the "Premature end of script headers" message.

    Finally, use CGI or comse other module to properly parse the query (when you actually receive one).

    --Bob Niederman, http://bob-n.com
      Hey bob. Thanks for your comments. The premature end of script headers is the last thing in my log. There are no others. Any other ideas?

        There are no ther entries in the error_log? that is strange

        Other posters have had great suggestions coming down to

        1. use "\r\n" instead of "\n" when sending headers (though my testing doesn't indicate that this is the problem, YMMV)

        2. turn on warnings with -w in the #! line or use warnings; in the script.

        3. run this from the command line and see what happens, possibly finding some way to feed it the values it wants. At least run as perl -cw scriptname which will find compile time errors.

        4. use CGI; to rpint the redirection (probably without the headers, as others have noted here,

        --Bob Niederman, http://bob-n.com
Re: Premature end of script headers
by sauoq (Abbot) on Aug 01, 2003 at 03:23 UTC

    <canned-reply>

    What does your error log say?

    </canned-reply>

    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Premature end of script headers
by tedrek (Pilgrim) on Aug 01, 2003 at 03:28 UTC

    Header lines end with "\x0d\x0a", which can usually be written as "\r\n".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://279838]
Approved by ybiC
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (5)
As of 2024-04-19 22:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found