Re: Help with mailto script
Brad Cox (bcox@gmu.edu)
Wed, 27 Mar 1996 20:31:31 -0500
At 16:48 03/27/96, Jon Wynacht wrote:
>I'm a newbie to Perl (and to programming...haven't touched it since
>Fortran my college year at Chico State). I need a mailto program that
>allows me to take the info from the form, send it to myself, and print it
>back to the user.
See http://www.virtualschool.edu/mon/LRNG572/96a/Task13.html for instructions
#! /usr/local/bin/perl
#Usage:
#<FORM METHOD=POST action="http://www.virtualschool.edu/mon/cgibin/mailto.cgi" >
#<input type=hidden name=mailto value="accountname\@hostname">
#<input type=hidden name=title value="Email Subject and Web Page Title">
#<input type=hidden name=greeting value="Congratulatory message">
open(STDERR, ">&STDOUT") || die "Can't dup stdout to stderr";
select STDERR; $| = 1;
select STDOUT; $| = 1;
require "cgi-lib.pl";
require "ctime.pl";
undef %in;
$in{'Date'} = &DateNow();
$in{'HTTP_REFERER'} = $ENV{'HTTP_REFERER'};
$in{'REMOTE_HOST'} = $ENV{'REMOTE_HOST'};
$in{'REMOTE_ADDR'} = $ENV{'REMOTE_ADDR'};
$in{'HTTP_USER_AGENT'} = $ENV{'HTTP_USER_AGENT'};
print &PrintHeader, "<html>\n";
if (&ReadParse(*in)) { print &ProcessForm; }
else { print qq[<p>This script is designed to be invoked via a
HTML/CGI form.]; }
print "<hr></body></html>";
sub ProcessForm {
my $mailto = delete $in{'mailto'} || &Fail("The mailto field is
missing or empty");
my $title = delete $in{'title'};
my $greeting = delete $in{'greeting'};
my $from = "bcox\@gmu.edu (VirtualSchool cgimail program)";
$title = "Generic HTML/FORM Handler" unless $title;
$greeting = "Congratulations; Your submission has been accepted and
delivered. Thanks for visiting our page." unless $greeting;
my $need = "";
foreach $key (split(/, */, delete $in{'require'})) {
$from = $in{$key} if $key =~ /mail/i;
next if $in{$key};
$need .= "$key ";
}
&Fail("Please complete the <b>$need</b> fields and try again") if $need;
my ($hdr) = qq[From: $from
To: $mailto
Reply-To: $from
Subject: $title\n\n];
open (OUT, "| /usr/lib/sendmail -t -oi -n") || &Fail("Can't mail");
print OUT $hdr, &toHtml(%in) || &Fail("Can't print email");
close OUT || &Fail("Can't close email");
return qq[<head><title>$title</title></head><body bgcolor=#ffffff>
<img src="../Pix/Creatures/NerdBirdTree.gif" align=left width=100>
<img src="../Pix/Creatures/NerdBirdTree.gif" align=right width=100>
<center><h1>Thanks!</h1><br clear><hr></center>\n$greeting\n];
}
# Convert %in into <dt>key<dd>value format, escaping anything the user
# put there that might interfere with parsing this data.
sub toHtml {
local (%in) = @_;
local ($data, $dataput);
$dataput = "<DL>\n";
foreach $key (sort keys %in) {
next unless $key;
foreach $val (split("\0", $in{$key})) {
next unless $val;
$val =~ s/<(D[DLT][^>]*)>/\<$1\>/ig;
$dataput .= "<DT>$key<DD>$val\n";
}
}
$dataput .= "</DL>\n";
return $dataput;
}
sub DateNow {
local($Date) = &ctime(time);;
chop($Date);
return $Date;
}
sub Fail {
local($err) = @_;
print qq[<center><h1>Sorry. Please try again.</h1></center>
<br clear><hr><p>$err\n];
exit;
}
--
Brad Cox; bcox@gmu.edu; GMU Pgm on Social and Organizational Learning
(703) 968-8229 Fax (703) 934 1578; Web http://www.virtualschool.edu/mon
The Middle of Nowhere Web, a project with paradoxical goals