Click to See Complete Forum and Search --> : Perl/CGI question


lanjoky
03-31-2006, 12:00 AM
Didnt know where else to post this type of question .. so if this is the wrong area please forgive me. It has been quite a while since i've posted here.

I am working on creating a simple CGI/Perl script to e-mail a form. Nothing fancy.

Below is the code I am working with. Basically I can post to the script and it runs, but I never receive an e-mail/ never get an error message. If you all can provide me with posssible problems I would be much ablidged. I usually don't deal with perl at all, but in this case I don't have the choice.



#!/usr/bin/perl

use CGI;

my $sendmail = "/usr/sbin/sendmail";

my $emailConfPath = "/export/home/fac/mis16/public_html/email.conf";

# Parse any submitted form fields and return an object we can use
# to retrieve them
my $query = new CGI;

my $name = &clean($query->param('name'));
my $email = &clean($query->param('email'));
my $recipient = &clean($query->param('recipient'));
my $content = &clean($query->param('content'));
my $subject = &clean($query->param('subject'));

#Note: subject is not mandatory, but you can easily change that
if (($name eq "") || ($email eq "") || ($content eq "") || ($recipient eq ""))
{
&error("Email Rejected",
"Please fill out all fields provided. Back up to the " .
"previous page to try again.");
}

if (!open(IN, "$emailConfPath")) {
&error("Configuration Error",
"The file $emailConfPath does not exist or cannot be " .
"opened. Please read the documentation before installing " .
"email.cgi.");
}

my $returnpage;

my $ok = 0;
while (1) {
my $recipientc = <IN>;
$recipientc =~ s/\s+$//;
if ($recipientc eq "") {
last;
}
my $returnpagec = <IN>;
$returnpagec =~ s/\s+$//;
if ($returnpagec eq "") {
last;
}
if ($recipientc eq $recipient) {
$ok = 1;
$returnpage = $returnpagec;
last;
}
}
close(IN);
if (!$ok) {
&error("Email Rejected",
"The requested destination address is not one of " .
"the permitted email recipients. Please read the " .
"documentation before installing email.cgi.");
}

# Open a pipe to the sendmail program
open(OUT, "|$sendmail -t");
# Use the highly convenient <<EOM notation to include the message
# in this script more or less as it will actually appear
print OUT <<EOM
To: $recipient
Subject: $subject
Reply-To: $email
Supposedly-From: $name
[This message was sent through a www-email gateway.]

$content
EOM
;
close(OUT);
# Now redirect to the appropriate "landing" page for this recipient.
print $query->redirect($returnpage);

exit 0;

sub clean
{
# Clean up any leading and trailing whitespace
# using regular expressions.
my $s = shift @_;
$s =~ s/^\s+//;
$s =~ s/\s+$//;
return $s;
}

sub error
{
# Output a valid HTML page as an error message
my($title, $content) = @_;
print $query->header;
print <<EOM
<html>
<head>
<title>$title</title>
</head>
<body>
<h1 align="center">$title</h1>
<p>
$content
</p>
EOM
;
exit 0;
}

dalecosp
04-01-2006, 01:29 AM
Well, this is the point where I grab a browser in one hand and run "tail -f /var/log/maillog" in a terminal with the other ....

lanjoky
04-01-2006, 02:57 PM
Well, this is the point where I grab a browser in one hand and run "tail -f /var/log/maillog" in a terminal with the other ....

Ty for the info!

I attempted to do so before I posted this problem, but to no avail as I do not have the access required to get to the log file. This is being hosted by a school server (not my choice) and thye are reluctant to give students access to these areas. I was hoping it was just something stupid in my script ... The script above is one that I found and altered to fit my needs.

Anyway .. thanks for trying!
Lan