Why won't this Perl script work - you must not miss this discussion!

They have: 4 posts

Joined: Jun 1999

I wrote a cgi script to post a form and I don't have a clue why it won't work. I even got the technician at my server to look at it, and he doesn't know what the problem could be. Here's what happens when I try to use it: Everything prints normally to the browser, telling whoever used the script that their info. was sent. However, I don't get the mail, and yes, the address is right. Here's the script:

#!/usr/bin/perl5
print "Content-type:text/html\n\n";

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

$mailprog ='/usr/bin/mail';

$recipient = '[email protected]';

open (MAIL, "$mailprog -t") or &dienice("Can't access $mailprog!\n");

print MAIL "To: $recipient\n";

print MAIL "Reply-to: $FORM{'email_address'} ($FORM{'name'})\n";

print MAIL "Subject: Twilley Form Catalog Request\n\n";

print MAIL "Twilley Customer's First Name = $FORM{'twilley_customer_first_name'}<br>";
print MAIL "Last Name = $FORM{'last_name'}<br>";
print MAIL "Business Name= $FORM{'business'}<br>";
print MAIL "Address = $FORM{'address'}<br>";
print MAIL "City = $FORM{'city'}<br>";
print MAIL "State = $FORM{'state'}<br>";
print MAIL "Zip Code = $FORM{'zip'}<br>";
print MAIL "Country = $FORM{'country'}<br>";
print MAIL "Phone Number = $FORM{'phone_number'}<br>";
print MAIL "Fax Number = $FORM{'fax_number'}<br>";
print MAIL "Email Address = $FORM{'email_address'}<br>";
@growertype = ("Fresh_market","Roadside","U-Pick","Shipper","Greenhouse_Grower","Home_Gardener");
foreach $x (@growertype) {
if ($FORM{$x} == 1) {
print MAIL "Type of Grower = $x <br>\n";
}
}
print MAIL "Farm Acreage = $FORM{'farm_acreage'}<br>\n";
print MAIL "Approximate Greenhouse Area =$FORM{'greenhouse_area'}<br>\n";
print MAIL "Comments and Crops of most interest = $FORM{'crops'}<br>";

close(MAIL);

print <<HOIHOI;
<h2><center>Thank You</h2><br>
Your request for a Twilley Catalog has been submitted. <br>
<a href="http://www.twilleyseed.com">Return to the Twilley Home Page</a><br>
Or<br>
<a href="http://www.twilleyseed.com/geo">Visit GeoSeed's Home Page</a>.</center>
</body></html>
HOIHOI
;

sub dienice {
($errmsg) = @_;
print "<h2>ERROR</h2>\n";
print "$errmsg<p>\n";
print "</body></html>\n";
exit;
}

Here's the form it goes to, just in case that helps you:

<form method="post" action="mailto:twformreturn.cgi">
<input type="text" name="twilley_customer_first_name" size="20">
<input type="text" name="last_name" size="25">
<input type="text" name="business" size="50">
<input type="text" name="address" size="40">
<input type="text" name="city" size="20">
<input type="text" name="state" size="20">
<input type="text" name="zip" size="10">
<input type="text" name="country" size="30">
<input type="text" name="phone_number" size="20">
<input type="text" name="fax_number" size="20">
<input type="text" name="email_address" size="30">
<input type="checkbox" name="Fresh_market" value=1>
<input type="checkbox" name="Roadside" value=1>
<input type="checkbox" name="U-Pick" value=1>
<input type="checkbox" name="Shipper" value=1>
<input type="radio" name="farm_acreage" value="less_than_10">
<input type="radio" name="farm_acreage" value="10-100">
<input type="radio" name="farm_acreage" value="more_than_100">
<input type="checkbox" name="Greenhouse_Grower" value=1>
<input type="radio" name="greenhouse_area" value="Less_than_1000_square_feet">
<input type="radio" name="greenhouse_area"
<input type="radio" name="greenhouse_area" value="More_than_10000_square_feet">
<input type="checkbox" name="type_of_grower" value="Home_Gardener">
<textarea name="crops" rows="7" cols="50"></textarea><input type="submit" value="Submit">
<input type ="reset" value="Reset"></form>

If y'all could help me out, it would be greatly appreciated.

Thanks
--Barratt

They have: 62 posts

Joined: May 1999

Hi-
I'm not sure, but on my host I am required to have a \ in front of the @ sign.... i.e:

$recipient = 'twilley\@emeraldis.com';

Just pointing that out Smiling

----------
Casey Hansen
www.caseynet.com

They have: 453 posts

Joined: Jan 1999

Hi,

Have you tried the script locally ??
Have you tested it with "perl -c" ??
Where is the "require strict;" ??

And Yes, @ in Strings have to be escaped. On every system.

ciao
Anti
ps:
sorry I only have a stupid terminal right now, so I can't test it.

They have: 4 posts

Joined: Jun 1999

Still didn't work even with the \ before the @. Don't know what could be wrong

They have: 32 posts

Joined: Jun 1999

Ok, I'm not very good at PERL, but in the HTML for the form, is it supposed to be:

<form method="post" action="mailto:twformreturn.cgi"> ?

I thought it's <form ACTION="twformreturn.cgi" METHOD="POST">

If course, I may be wrong....

-steve

They have: 453 posts

Joined: Jan 1999

Hi,

try this:
1. don't open mail, but a normal file.
2. type the contents of that file to mail (by hand !!!)
3. does it work ?

btw:
the "-t" option is not supported by every "mail". most of them try to emulate sendmai, but not all of them.

ciao
Anti

They have: 29 posts

Joined: May 1999

Ok, I'm new to Perl so this isn't even close to an attempt to answer your question. But, I saw something in your script I didn't know will work.

In the mailing part of the program you ended each line with the carriage return command used in html. Aren't you supposed to use the new line character used in Perl?
"\n"

----------
Webmasters-Pub: A Webmasters Guide to Success
http://www.webmasters-pub.com

They have: 6 posts

Joined: Aug 1999

I'm pretty new to perl so I'm probably wrong but in my little knowledge, I dont understand why you have PRINT <<HOIHOI; (do you need 2 of the < signs?) Since I'm wrong, could you explain why it is there? Thanks.

They have: 5,633 posts

Joined: Jan 1970

Yes, Steve is right, and yes there has to be a backslash in front of the @ for Perl versions > 5.0.

Later,

Malte

----------
Malte Ubl - [red]Germany[/red] - http://goKewl.com/
[red]The beats of 1000 hearts inside...[/red] the merciless pulsation... [red]and all I know is I'll be back...[/red] back to trance nation!

They have: 2 posts

Joined: Aug 1999

What?!?...I always thought that if you had PERL 5 you didn't need the slash

----------
Shawn Muder
www.realmsofevil.com

They have: 6 posts

Joined: Aug 1999

Well here it goes.

I did a little changing to your script, but not much.

<---Script Starts--->

#!/usr/bin/perl

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

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$FORM{$name} = $value;
}

$mailprog ='/usr/lib/sendmail';

$recipient = 'you@your_domain.com';

open (MAIL, "¦$mailprog -t");

print MAIL "To: $recipient\n";

print MAIL "From: $FORM{'email_address'}\n";

print MAIL "Subject: Twilley Form Catalog Request\n\n";

print MAIL "Twilley Customer's First Name = $FORM{'twilley_customer_first_name'}\n";
print MAIL "Last Name = $FORM{'last_name'}\n";
print MAIL "Business Name= $FORM{'business'}\n";
print MAIL "Address = $FORM{'address'}\n";
print MAIL "City = $FORM{'city'}\n";
print MAIL "State = $FORM{'state'}\n";
print MAIL "Zip Code = $FORM{'zip'}\n";
print MAIL "Country = $FORM{'country'}\n";
print MAIL "Phone Number = $FORM{'phone_number'}\n";
print MAIL "Fax Number = $FORM{'fax_number'}\n";
print MAIL "Email Address = $FORM{'email_address'}\n";

print MAIL "Type of Grower = ";

if ($FORM{'Fresh_market'}) {
print MAIL "Fresh Market, ";
}

if ($FORM{'Roadside'}) {
print MAIL "Roadside, ";
}

if ($FORM{'U-Pick'}) {
print MAIL "U-Pick, ";
}

if ($FORM{'Shipper'}) {
print MAIL "Shipper, ";
}

if ($FORM{'Greenhouse_Grower'}) {
print MAIL "Greenhouse Grower, ";
}

if ($FORM{'Home_Gardner'}) {
print MAIL "Home Gardner";
}

print MAIL "\n";

print MAIL "Farm Acreage = $FORM{'farm_acreage'}\n";
print MAIL "Approximate Greenhouse Area =$FORM{'greenhouse_area'}\n";
print MAIL "Comments and Crops of most interest = $FORM{'crops'}\n";

close(MAIL);

print <<EOF;
<h2><center>Thank You</h2><br>
Your request for a Twilley Catalog has been submitted. <br>
<a href="http://www.twilleyseed.com">Return to the Twilley Home Page</a><br>
Or<br>
<a href="http://www.twilleyseed.com/geo">Visit GeoSeed's Home Page</a>.</center>
</body></html>
EOF
;

exit;

<---Script Ends--->

<---Form Starts--->

<HTML>
<BODY>
<form method="post" action="/test.cgi">
<input type="text" name="twilley_customer_first_name" size="20">
<input type="text" name="last_name" size="25">
<input type="text" name="business" size="50">
<input type="text" name="address" size="40">
<input type="text" name="city" size="20">
<input type="text" name="state" size="20">
<input type="text" name="zip" size="10">
<input type="text" name="country" size="30">
<input type="text" name="phone_number" size="20">
<input type="text" name="fax_number" size="20">
<input type="text" name="email_address" size="30">
<input type="checkbox" name="Fresh_market">
<input type="checkbox" name="Roadside">
<input type="checkbox" name="U-Pick">
<input type="checkbox" name="Shipper">
<input type="radio" name="farm_acreage" value="less_than_10">
<input type="radio" name="farm_acreage" value="10-100">
<input type="radio" name="farm_acreage" value="more_than_100">
<input type="checkbox" name="Greenhouse_Grower">
<input type="radio" name="greenhouse_area" value="Less_than_1000_square_feet">
<input type="radio" name="greenhouse_area"
<input type="radio" name="greenhouse_area" value="More_than_10000_square_feet">
<input type="checkbox" name="Home_Gardner">
<textarea name="crops" rows="7" cols="50"></textarea><input type="submit" value="Submit">
<input type ="reset" value="Reset"></form>
</BODY>
</HTML>

<---Form Ends--->

I hope that this helps you get script up and running smoothly.

You will still need to change $mailprog to match the location and type of mail program that your server is using.

Brian
LotionBarn.com

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.