Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations dencom on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PERL Script Returning Error 500 3

Status
Not open for further replies.

thevenerablez

Technical User
Feb 6, 2007
6
US
Hi,

I am new to PERL coding and I am stuck on my first CGI script. I am trying to make an email form work. Here is my script:

#!/usr/local/bin/perl -wT
use strict;
use CGI ':standard';
my ($to, $from, $name, $subject, $contents);

print "Content-type:text/html\n\n";

$to = param('to');
$from = param('from');
$name = param('name');
$subject = param('subject');
$contents = param('contents');

open(MAIL, "|usr/sbin/sendmail -t") || Error ('open', 'mail program');

print MAIL "To: $to \nFrom: $from\nName: $name\n";
print MAIL "Subject: $subject\n";
print MAIL "$contents\n",

close (MAIL);

print "Thanks for your comments.";

sub Error {
print "The server cannot $_[0] the $_[1]: $! \n";
exit;
}

If anyone could give me any pointers, I would greatly appreciate it. If you need the HTML source, I can post that too.

Thanks,
_zach
 
maybe
open(MAIL, "|usr/sbin/sendmail -t") || Error ('open', 'mail program');

should be /usr/sbin/sendmail

ave you looked at the error logs on the server?

 
remove all shebang line switches for now and try this code:

Code:
[gray]#!/usr/local/bin/perl[/gray]
[black][b]use[/b][/black] [green]strict[/green][red];[/red]
[black][b]use[/b][/black] [green]warnings[/green][red];[/red]
[black][b]use[/b][/black] [green]CGI::Carp[/green] [red]'[/red][purple]fatalsToBrowser[/purple][red]'[/red][red];[/red]
[black][b]use[/b][/black] [green]CGI[/green] [red]'[/red][purple]:standard[/purple][red]'[/red][red];[/red]
[black][b]my[/b][/black] [red]([/red][blue]$to[/blue][black],[/black] [blue]$from[/blue][black],[/black] [blue]$name[/blue][black],[/black] [blue]$subject[/blue][black],[/black] [blue]$contents[/blue][red])[/red][red];[/red]

[black][b]print[/b][/black] [maroon]header[/maroon][red]([/red][red])[/red][black],[/black] [maroon]start_html[/maroon][red]([/red][red])[/red][red];[/red]

[blue]$to[/blue] [black]=[/black] [maroon]param[/maroon][red]([/red][red]'[/red][purple]to[/purple][red]'[/red][red])[/red][red];[/red]
[blue]$from[/blue] [black]=[/black] [maroon]param[/maroon][red]([/red][red]'[/red][purple]from[/purple][red]'[/red][red])[/red][red];[/red]
[blue]$name[/blue] [black]=[/black] [maroon]param[/maroon][red]([/red][red]'[/red][purple]name[/purple][red]'[/red][red])[/red][red];[/red]
[blue]$subject[/blue] [black]=[/black] [maroon]param[/maroon][red]([/red][red]'[/red][purple]subject[/purple][red]'[/red][red])[/red][red];[/red]
[blue]$contents[/blue] [black]=[/black] [maroon]param[/maroon][red]([/red][red]'[/red][purple]contents[/purple][red]'[/red][red])[/red][red];[/red]

[black][b]open[/b][/black][red]([/red][black]MAIL[/black][black],[/black] [red]"[/red][purple]|usr/sbin/sendmail -t[/purple][red]"[/red][red])[/red] [black]||[/black] [maroon]Error[/maroon][red]([/red][red]'[/red][purple]open[/purple][red]'[/red][black],[/black] [red]'[/red][purple]mail program[/purple][red]'[/red][red])[/red][red];[/red]

[black][b]print[/b][/black] [black]MAIL[/black] [red]"[/red][purple]To: [blue]$to[/blue] [purple][b]\n[/b][/purple]From: [blue]$from[/blue][purple][b]\n[/b][/purple]Name: [blue]$name[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]print[/b][/black] [black]MAIL[/black] [red]"[/red][purple]Subject: [blue]$subject[/blue][purple][b]\n[/b][/purple][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[black][b]print[/b][/black] [black]MAIL[/black] [red]"[/red][purple][blue]$contents[/blue][purple][b]\n[/b][/purple][/purple][red]"[/red][black],[/black]

[black][b]close[/b][/black] [red]([/red][black]MAIL[/black][red])[/red][red];[/red]

[black][b]print[/b][/black] [red]"[/red][purple]Thanks for your comments.[/purple][red]"[/red][black],[/black][maroon]end_html[/maroon][red]([/red][red])[/red][red];[/red]

[black][b]sub[/b][/black] [maroon]Error[/maroon] [red]{[/red]
    [black][b]print[/b][/black] [red]"[/red][purple]The server cannot [blue]$_[/blue][0] the [blue]$_[/blue][1]: [blue]$![/blue] [purple][b]\n[/b][/purple][/purple][red]"[/red][black],[/black][maroon]end_html[/maroon][red]([/red][red])[/red][red];[/red]
    [black][b]exit[/b][/black][red];[/red]
[red]}[/red]

make sure to upload the script in ASCII (text) mode and chmod to 755. If you still get a "500" error most likely the shebang line is wrong.




- Kevin, perl coder unexceptional! [wiggle]
 
Thanks for your responses! I'm still a little lost. Kevin, I copied your code and made it my sendmail.cgi file. I'm still getting a 500 error. How do I upload in ASCII mode and what is chmod? How can I set chmod to 755? I'm running Mac OS X 10.4.8 and am uploading the file in Dreamweaver 8.

Thanks again for your help,
_zach
 
You will have to check the Dreamweaver help files and see how to do it with dreamweaver. Search for "transfer mode" or "ftp transfer mode" and "chmod" or "setting remote file permissions" or similar searches. Or ask in a dreamweaver forum if you get stuck. I have nver used dreamweaver myself so I have no idea.

You can try changing the sebang line to:

#!/usr/bin/perl

unless you know the one you are using is correct.

- Kevin, perl coder unexceptional! [wiggle]
 
Kevin,

I changed the shebang line to #!/usr/bin/perl as you suggested. Otherwise, my sendmail.cgi file is the same as your previous post.

As for uploading through Dreamweaver, I didn't bother. I did my upload in terminal. Here is the code that uploaded my script:

ftp> put sendmail.cgi
local: sendmail.cgi remote: sendmail.cgi
229 Extended Passive mode OK (|||19814|)
150 Accepted data connection
100% |*************************************| 662 254.02 KB/s --:-- ETA
226-File successfully transferred
226 0.079 seconds (measured here), 7.86 Kbytes per second
662 bytes sent in 00:00 (7.87 KB/s)
ftp> chmod
(mode) 755
(remote-file) sendmail.cgi
200 Permissions changed on sendmail.cgi
ftp> quit
221-Goodbye. You uploaded 3 and downloaded 0 kbytes.
221 Logout.

I'm still getting a 500 error when I test the script. Do you have any idea?

I really appreciate your time.

Thanks,
_zach
 
I don't know how the file is transfered using the method you used. You will need to use an ftp client where you can set the type of transfer mode. WS_FTP95_LE is free and easy to use. I'm sure there are other free ones.

- Kevin, perl coder unexceptional! [wiggle]
 
If I could just chip in with this. The error code 500, you are seeing is not from Perl but from 'ftp'. It means "Syntax error, command unrecognized. This may include errors such as command line too long". For more explanation and a list of other FTP error codes, Google for: ftp + error + code

I hope that helps.

Mike
 
Is the CGI or HTML file causing the error?

Does anyone know where I could look to remove the error?

Thanks,
_zach
 
Hi _zach,

Sorry, I jumped in a bit quick with the suggestion that its an 'ftp' error. Looks like it is actually a Sendmail error (but still not a Perl error). Try a Google for: sendmail + 500 + error

I had a quick look at some of the hits, one suggested that this error occurs when 'to' & 'from' are the same address. You might also look through the 'sendmail' log files on the server.

I hope that helps.

Mike
 
Hi,

My server's error log says, "Premature end of script headers" as the result of the error 500 on sendmail.cgi.

What needs to be done?

Thanks,
_zach
 
if you have this line in your script:

use CGI::Carp 'fatalsToBrowser';

and all you get is a generic "500 Internal Server Error" in the browser and a "premature end of script headers" in the server error log, the reasons for the script bombing are very limited:

1. shebang line is wrong
2. uploaded in binary instead of text mode
3. permission are wrong

In the code you and I posted earlier I see an error:

Code:
print MAIL "$contents\n"[red],[/red]

the comma on the end should be a semi-colon:

Code:
print MAIL "$contents\n"[red];[/red]

but I don't think that is causing your problem as the CGI::Carp module would trap that error and print it to the screen, but it should still be fixed.




- Kevin, perl coder unexceptional! [wiggle]
 
Try two versions of the script to see which part is broken:

One with the html only (remove the mail section)
Code:
#!/usr/local/bin/perl
use strict;
use warnings;
use CGI::Carp 'fatalsToBrowser';
use CGI ':standard';
my ($to, $from, $name, $subject, $contents);

print header(), start_html();

$to = param('to');
$from = param('from');
$name = param('name');
$subject = param('subject');
$contents = param('contents');
print "Thanks for your comments.",end_html();

The other with ONLY the mail section and predefine variables
for testing.
Code:
#!/usr/local/bin/perl
use strict;
use warnings;
use CGI::Carp 'fatalsToBrowser';
use CGI ':standard';
my ($to, $from, $name, $subject, $contents);

$to = "emailto@account.com";
$from = "emailfrom@account.com";
$name = "Joe Blow";
$subject = "TEST1";
$contents = "Blah Blah, Yak Yak";

open(MAIL, "|usr/sbin/sendmail -t") || warn("The server cannot open mail program");

print MAIL "To: $to \nFrom: $from\nName: $name\n";
print MAIL "Subject: $subject\n\n";
print MAIL "$contents\n",

close (MAIL);

That should help narrow things down
 
these two lines need to be changd to avoid interpolation of the @ symbol as an array:

Code:
$to = "emailto@account.com";
$from = "emailfrom@account.com";

change to:

Code:
$to = 'emailto@account.com';
$from = 'emailfrom@account.com';



- Kevin, perl coder unexceptional! [wiggle]
 
premature end of script headers" generally means that there is output being sent to the browser from the server before the Content-Type header is being sent, usually an error, or something being printed.

instead of;
Code:
print "Content-Type: text/html\n\n";
try
Code:
$q=new CGI;
print $q->header();
and move it as high up in the script as possible, as per Kevin's earlier advice.

Also try running this script from the command line, and post the output.

HTH

@Kevin, are you using icrf's color coder for that script above?




Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
@Kevin, are you using icrf's color coder for that script above?

no, Im using a modified version of Syntax::Highlight::perl. I was not aware of the other module (Text::Highlight) but I see something interesting in the modules source code:

$TGML_FORMAT = '[ignore][COLOR=%s]%s[/color][/ignore]';
$TGML_WRAPPER = "[ignore]
Code:
\n%s\n
[/ignore]";

support for TGML code [smile]

- Kevin, perl coder unexceptional! [wiggle]
 
yeah, I *think* he wrote while he was a frequent visitor here ;-)
mebbee still is, haven't been in these here parts for a while
--Paul

Paul
------------------------------------
Spend an hour a week on CPAN, helps cure all known programming ailments ;-)
 
I can't remember last time I saw him here, it's been a few months at least.

- Kevin, perl coder unexceptional! [wiggle]
 
Thanks everyone, I'm about to try all this now. I'll let you know how it works.

Thanks again!

_zach
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top