#!/usr/bin/perl #ass3mail.cgi - by Rich Gibson Rich@chilidog.com http://www.chilidog.com #4/13/98 #This is an example of a q&d cgi mailer. I am not using CGI.pm #in order to see everything, and show everything that 'really' happens #First: I want the user to get a page, rather than an error. #If there is a run time error _after_ the header is sent, the user gets #a page (maybe :-) So, I'll send a header, and a bit of other muck. print "Content-type: text/html\n\n"; #this is the 'Here' document syntax. 'Ending_Tag' can be any word. #The text between < ass3mail.cgi response END_OF_TEXT #Get the user input. If CONTENT_LENGTH == 0, assume this is being #tested from the command line...so allow the user to enter name/value #pairs, ala CGI.pm if (defined $ENV{'CONTENT_LENGTH'}) { read(STDIN, $input, $ENV{'CONTENT_LENGTH'}); } else { print "called from command line: Enter name=value pairs separated by a newline \n"; while (<>){ chomp; #removes the trailing \n (newline) $input .= $_ . "&"; #dot (.) is the string concatanation operator } #get rid of the last & $input =~ s/&$//; } @input = split(/&/,$input); #now turn the array into a hash. foreach $element (@input) { ($key, $data) = split(/=/,$element) ; $key = uc $key; #this could be written as $key = uc($key) $data = UnescapeString($data); $param{$key} = $data; } #we want a subject $param{"SUBJECT"}="Please look at: " . $param{"URL"}; #do some data validation #email address? if ($param{"SENDTO"} == 1){ #Prof. Babb $param{"SENDTO"}="babb\@cs.du.edu"; } elsif ($param{"SENDTO"} == 2){ #Raghu $param{"SENDTO"}="rpillutl\@evans.cs.du.edu"; } elsif ($param{"SENDTO"} == 3){ #Rich Gibson $param{"SENDTO"}="rich\@chilidog.com"; } else { #some bogusity-send it to Rich #remember, just because _your_ html calls a cgi with the 'right' #parameters doesn't mean someone else won't call it with really #messed up parameters... $param{"SENDTO"}="rich\@chilidog.com"; #and let him know there was something fishy going on... $param{"SUBJECT"}="Bogusity Alert from: ASS3Mail.cgi Generated Comment"; } #Now we have good parameters. Open a pipe to sendmail and have at it. if (!open(MAIL, "|/usr/lib/sendmail -t -oi") ){ print "Major Error in trying to send mail...sorry.
"; exit; } # the real to line.To: $param{"SENDTO"} print MAIL <\n"; print "Parameters sent to this CGI\n"; foreach $k (keys %param){ print "$i = $k $param{$k}\n"; $i++; } print "/
\n";
}


sub UnescapeString 
{
	#from http://www.boutell.com/cgibook/cd/SOURCE/PERL/comments
        local($s) = $_[0];
        local($pos, $ascii);
        # Replace the + sign with spaces
        $s =~ s/\+/ /g;
        # Seek out and replace %xx hexadecimal escapes
        $pos = 0;
        while (($pos = index($s, "%", $pos)) != -1) {   
                $ascii = hex(substr($s, $pos + 1, 2));
                substr($s, $pos, 3) = pack("c", $ascii);
        }
        $s;
}