comment form - hi guys, i just can't figure this out!
i've been trying to figure this out for the longest time...
i built a comment form page a site. i had the "contact.html" page with
perl behind it. after i uploaded both files, i know the script was not
working correctly because no email is actually shooting through.
i've checked everything and make sure the site host support perl and
everything i can possibly think of. at this point i am wondering
if anything can help me look over my code. thanks! -spt
________________________________________perl starts here
#!/usr/bin/perl
print "Content-type: text/html\n\n";
# put all the variable pairs (name=value) into an array
@vars = split( /&/, $input_string );
# process each individual name, value pair, putting them
# into a hash for easy access
foreach $indiv_var ( @vars ) {
# separate the pair
($name, $value) = split( /=/, $indiv_var, 2 );
# translate encoding
$name =~ s/%([\da-fA-F]{2})/pack("C", hex($1))/eg;
$name =~ tr/+/ /;
unless (defined $value) {
# just in case there was no equals as in ISINDEX
$value = "" ;
}
$value =~ s/%([\da-fA-F]{2})/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
# put the pair in the hash for easy access
$form{$name} = $value ;
}
return wantarray ? %form : undef ;
}
sub send_email {
my ($mailprog, $email, $name, $mailto, $subject, $message) = @_ ;
if (open MAIL, "|$mailprog -t") {
print MAIL "To: $mailto\n" ;
print MAIL "From: \"$name\" <$email>\n" ;
print MAIL "Reply-To: \"$name\" <$email>\n" ;
print MAIL "X-Mailer: chfeedback.pl 2.01\n" ;
print MAIL "Subject: $subject\n\n" ;
print MAIL $message ;
close MAIL ;
}
# ignore fails, just don't do anything
I looked it over quickly and spotted a couple of problems.
For starters:
Code:
#!/usr/bin/perl
use strict;use warnings;
Try to get into the practice of using strict and warnings. If nothing else, at least while you are developing the application. The addition of those two lines alone would have tipped you off to a lot of the problems.
Next, use CGI.pm to parse form data, it will eliminate the need for your parse_form_data sub:
Code:
use CGI;
my $in = new CGI;
# It is also great for printing headers
print $in->header();
Then you can access your form data like so:
Code:
my $email = $in->param('email');
# instead of:
my $email ;
if (exists $form{"email"}) {
$email = $form{"email"} ;
}
As far as not sending the mail, here is the problem:
Code:
send_email ( $mailprog, $email, $name, $mailto, $subject, $message );
# You need something like this:
my $mailprog = '/usr/sbin/sendmail';
send_email ( $mailprog, $email, $name, $mailto, $subject, $message );
You never define $mailprog in your script. Using strict would have let you know about that. You should also be checking to make sure that you did in fact open sendmail:
Code:
open MAIL, "|$mailprog -t" or die "Cannot open sendmail. Reason: $!";
That script would have died and left a nice message for you in the error_log.
One last thing, try to post perl specific questions in the perl forum. You'll get a better response there. Maybe a mod can move it over for you.
That should be enough to get you going for now. If you have more problems, post back and I can take a closer look at it when I'm not in as much of a hurry. Include any errors from your error log if you have access to it. Oh, this might help too: http://learn.perl.org/library/beginning_perl/. Nothing beats a free book.