Perl idiot needs help with simple script 
Author Message
 Perl idiot needs help with simple script

OK I've written a CGI script to almost the textbook standard. The
script asks a user to fill in a form, and then emails the results to

Straightforward, I thought, but when I run it, it gives no error
messages, but no email is sent to that address.

I am a complete newbie when it comes to programmming in Perl and I
need all the help I can get. Any suggestions will be _gladly_
accepted.

My script appears, in full, below.

Thanks in advance,
Rich

PS the site is hosted on f2s.com, which doesn't allow for shell
logins.

---------------------

#!/usr/bin/perl

use CGI;

$query = new CGI;

#setup variables
$sendmail = "/usr/sbin/sendmail -n -t -oi";

if (!$query->param()) {
        &print_page_start;
        &print_form;
        &print_page_end;

Quote:
}

else {
        &set_form_vars;
        if (&valid_form eq 'yes') {
        &send_email;
        &print_page_start;
        &print_success;
        &print_page_end;
        }
else {
        &print_page_start;
        &print_error_message;
        &print_form;
        &print_page_end;
        }

Quote:
}

sub print_page_start {
        print $query->header;
        print "<HTML>\n";
        print "<HEAD>\n";
        print "<TITLE>ndlearning - contact form</TITLE>\n";
        print "</HEAD>\n";
        print "<BODY>\n";
        print "<font face=\"Verdana, Arial, Helvetica, sans-serif\" ";
        print "color=\"#000000\" size=2>\n";
        print "<table border=\"0\" width=\"100%\"
bgcolor=\"#9900CC\">\n";
        print   "<tr><td>\n";
        print   "<b><font face=\"Verdana, Arial, Helvetica, sans-serif\"
color=\"#ffffff\" size=5>\n";
        print   "Contact Form\n";
        print   "</font></b>\n";
        print "</td></tr>\n";
        print   "</table>\n";
        print "<p><font face=\"Verdana, Arial, Helvetica, sans-serif\"
color=\"#9900CC\" size=2>\n";
        print "<b>Note: The fields marked with asterisks (*) must be
completed</b></font></p>\n";

Quote:
}

sub print_page_end {
        print "</BODY>\n";
        print "</HTML>\n";

Quote:
}

sub set_form_vars {
        $name = $query->param('name');
        $email = $query->param('email');
        $company = $query->param('company');
        $address1 = $query->param('address1');
        $address2 = $query->param('address2');
        $town = $query->param('town');
        $county = $query->param('county');
        $postcode = $query->param('postcode');
        $country = $query->param('country');
        $phone = $query->param('phone');
        $fax = $query->param('fax');
        $comments = $query->param('comments');
        $contact = $query->param('contact');
        $how = $query->param('how');

Quote:
}

sub print_form {
        print "<FORM METHOD=\"post\">\n";
        print "<table border=\"0\">\n";
        print "<tr><td><font face=\"Verdana, Arial, Helvetica,
sans-serif\" ";
        print "color=\"#000000\" size=2>\nName*:</td>\n";
        print "<td><input type=\"text\" name=\"name\"
value=\"$name\">\n";
        print "</td></tr>\n";
        print "<tr><td><font face=\"Verdana, Arial, Helvetica,
sans-serif\" ";
        print "color=\"#000000\" size=2>\nEmail address*:</td>\n";
        print "<td><input type=\"text\" name=\"email\"
value=\"$email\">\n";
        print "</td></tr>\n";
        print "<tr><td><font face=\"Verdana, Arial, Helvetica,
sans-serif\" ";
        print "color=\"#000000\" size=2>\nCompany:</td>\n";
        print "<td><input type=\"text\" name=\"company\"
value=\"$company\">\n";
        print "</td></tr>\n";
        print "<tr><td><font face=\"Verdana, Arial, Helvetica,
sans-serif\" ";
        print "color=\"#000000\" size=2>\nAddress (line 1):</td>\n";
        print "<td><input type=\"text\" name=\"address1\"
value=\"$address1\">\n";
        print "</td></tr>\n";
        print "<tr><td><font face=\"Verdana, Arial, Helvetica,
sans-serif\" ";
        print "color=\"#000000\" size=2>\nAddress (line 2):</td>\n";
        print "<td><input type=\"text\" name=\"address2\"
value=\"$address2\">\n";
        print "</td></tr>\n";
        print "<tr><td><font face=\"Verdana, Arial, Helvetica,
sans-serif\" ";
        print "color=\"#000000\" size=2>\nTown/City:</td>\n";
        print "<td><input type=\"text\" name=\"town\"
value=\"$town\">\n";
        print "</td></tr>\n";
        print "<tr><td><font face=\"Verdana, Arial, Helvetica,
sans-serif\" ";
        print "color=\"#000000\" size=2>\nCounty:</td>\n";
        print "<td><input type=\"text\" name=\"county\"
value=\"$county\">\n";
        print "</td></tr>\n";
        print "<tr><td><font face=\"Verdana, Arial, Helvetica,
sans-serif\" ";
        print "color=\"#000000\" size=2>\nPost Code:</td>\n";
        print "<td><input type=\"text\" name=\"postcode\"
value=\"$postcode\">\n";
        print "</td></tr>\n";
        print "<tr><td><font face=\"Verdana, Arial, Helvetica,
sans-serif\" ";
        print "color=\"#000000\" size=2>\nCountry (if not
UK):</td>\n";
        print "<td><input type=\"text\" name=\"country\"
value=\"$country\">\n";
        print "</td></tr>\n";
        print "<tr><td><font face=\"Verdana, Arial, Helvetica,
sans-serif\" ";
        print "color=\"#000000\" size=2>\nDaytime telephone
number:</td>\n";
        print "<td><input type=\"text\" name=\"phone\"
value=\"$phone\">\n";
        print "</td></tr>\n";
        print "<tr><td><font face=\"Verdana, Arial, Helvetica,
sans-serif\" ";
        print "color=\"#000000\" size=2>\nFax number:</td>\n";
        print "<td><input type=\"text\" name=\"fax\"
value=\"$fax\">\n";
        print "</td></tr>\n";
        print "<tr><td><font face=\"Verdana, Arial, Helvetica,
sans-serif\" ";
        print "color=\"#000000\" size=2>\nAdditional
Comments:</td>\n";
        print "<td><textarea name=\"comments\" wrap=\"physical\"
rows=5 cols=40>";
        print $comments;
        print "</textarea></td></tr>\n";
        print "<tr><td colspan=\"2\"><font face=\"Verdana, Arial,
Helvetica, sans-serif\" ";
        print "color=\"#000000\" size=2>\nTick if you would like me to
contact you: \n";
        print "<input type=\"checkbox\" name=\"contact\"
value=\"yes\"";

        if ((!$query->param()) || ($contact eq 'yes')) {
                print " CHECKED ";
        }

        print ">\n";
        print "</td></tr>\n";
        print "<tr><td><font face=\"Verdana, Arial, Helvetica,
sans-serif\" ";
        print "color=\"#000000\" size=2>\nIf &quot;yes&quot;,
how?</td>\n";
        print "<td><select name=\"how\">\n";
        print "<option value=\"email\">Email\n";
        print "<option value=\"post\" ";
        if ($how eq 'post') {
                print "SELECTED";
        }
        print ">Post\n";
        print "<option value=\"phone\" ";
        if ($how eq 'phone') {
                print "SELECTED";
        }
        print ">Telephone\n";
        print "<option value=\"fax\" ";
        if ($how eq 'fax') {
                print "SELECTED";
        }
        print ">Fax\n";
        print "<option value=\"visit\" ";
        if ($how eq 'visit') {
                print "SELECTED";
        }
        print ">Personal Visit\n";
        print "</select></td>\n";
        print "<tr><td height=\"15\"></td></tr>\n";
        print "<tr><td><input type=\"submit\" value=\"Send\">\n";
        print "</td></tr></table>\n";
        print "</form>\n";

Quote:
}

sub valid_form {
        $success = "yes";
        if (!$name) {
        $name_error = "<p><font color=\"#ff0000\"><b>";
        $name_error .= "ERROR: You must specify your
name.</b></font></p>\n";
        $success = "no";
        }

        $email_error = "<p><font color=\"#ff0000\"><b>";
        $email_error .= "ERROR: You must enter a VALID email
address.</b></font></p>\n";
        $success = "no";
        }
        if (!$email) {
        $email_error = "<p><font color=\"#ff0000\"><b>";
        $email_error .= "ERROR: You must enter your email
address.</b></font></p>\n";
        $success = "no";
        }
        return $success;

Quote:
}

sub print_error_message {
        if ($name_error) {
        print $name_error, "\n";
        }
        if ($email_error) {
        print $email_error, "\n";
        }

Quote:
}

sub print_success {
        print "<p>Thankyou, $name. Nicola has been emailed with ";
        print "those details.</p>\n";
        print "<p>Please close this window to continue</p>\n";

Quote:
}

sub send_email {
        open(MAIL, "| $sendmail") or
        die "Couldn't open sendmail: ";
        print MAIL <<EOF;
From: $email
To: $to
Subject: Email Contact Form Response

Form filled in by: $name ( $email )

Company: $company
Address:
$address1
$address2
$town
$county
$postcode
$country

Phone: $phone   Fax: $fax

Their comments:
$comments

Should you contact them back: $contact
How: by $how

EOF
        close MAIL;
//}



Sun, 22 Feb 2004 22:03:36 GMT  
 Perl idiot needs help with simple script

Quote:
>#!/usr/bin/perl

>use CGI;

please start everything with

#!/usr/bin/perl -wT

use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

then go on with

my $q         = new CGI;

Quote:
>$sendmail = "/usr/sbin/sendmail -n -t -oi";


change to
see below....

Quote:
>sub set_form_vars {

     my $email = $q->param('email');

Quote:
>}
>sub send_email {

        open(SENDMAIL, "| /usr/sbin/sendmail -n -t -oi") or
        die "Couldn't open sendmail: ";
        print SENDMAIL <<EOF;
Quote:
>From: $email
>To: $to
>Subject: Email Contact Form Response

>Form filled in by: $name ( $email )

>Company: $company
>Address:
>$address1
>$address2
>$town
>$county
>$postcode
>$country

>Phone: $phone   Fax: $fax

>Their comments:
>$comments

>Should you contact them back: $contact
>How: by $how

>EOF

        close MAIL or die "Something going wrong!";

Quote:
>//}

--
Andreas Schmitz http://www.medialsoft.de
 _ _  _  _ _     _ _  _ _
| | ||_ | \|| || _| (_)|- |
| | ||_ |_/||-||__              


Sun, 22 Feb 2004 22:58:14 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Need help on very simple Perl script

2. Help with simple simple perl script...

3. Help needed with CGI script (a very simple problem)

4. Need a Simple but Unique CGI script - please help

5. need help with simple script

6. need help with simple script

7. need help with simple script

8. Need help debugging this simple script

9. need help with simple script

10. Need help with simple parsing script

11. Idiot needs reply of FTP-in-Perl thread...

12. newbie to perl - need simple search perl script

 

 
Powered by phpBB® Forum Software