#!/usr/local/bin/perl # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= # Stray's General Purpose Form Mailer 17 September 1998 # info@stray.ch Version 1.1 # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= # This script will take the input of any HTML form and send it to a list # of recipients. Compulsory fields must be filled in or the error page will # be displayed. When successful, mail will be sent, omitting empty fields, # arranging the field results in a properly manner. # # The original version of this script can always be foud at # http://crypt.stray.ch/scripts.html # # HTML definitions: # ----------------- # - # - # - # - # # - # This is a special field name to insert an empty line in the mail # message; useful to separate various information groups in a form. # # - Fields with their names starting with a "#" sign are compulsory, # i.e. they must be filled out in order for the form to be sent; else, # the error page as defined in the hidden field will be loaded. # e.g. " will be compulsory. # # - Any field called "email" will be used for the reply-to address of the # mail sent. You can use a user field for "email", so the mail will # have the correct reply-to address of the person filling in the form. # If omitted, a standard reply-to address will be given instead. # # =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= # =-=-= The happy variable setting part -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= $mailprog = '/usr/lib/sendmail'; # the mail programm on your server $date = `/bin/date`; chop ($date); # time and date $emptyline = "\n"; # set this to change the separator # =-=-= Converting server input into name / value pairs -=-=-=-=-=-=-=-=-=-= read(STDIN, $input, $ENV{'CONTENT_LENGTH'}); @pairs = split(/&/, $input); foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $FORM{$name} = $value; } # =-=-= Checking hidden fields for completeness -=-=-=-=-=-=-=-=-=-=-=-=-=-= if ( (!$FORM{'error'}) || (!$FORM{'thanks'}) ) { print "Content: text/html\n\n"; print "

Stray's General Purpose Form Mailer


"; print "Error: Hidden fields not supplied.

";
   print "Values submitted were:\nError URL:  $FORM{'error'}\n";
   print "Thanks URL: $FORM{'thanks'}
"; print '
For more help see http://crypt.stray.ch/'; exit; } # =-=-= Checking user input for completeness -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-= # ... and while we're at it, we parse the field names for the longest # entry to add spaces as necessary later for the mailing. $maxlength = 0; foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $maxlength = length($name) if (length($name) > $maxlength); if (($value eq "") && ($name =~ /^#.*/)) { print "Location: $FORM{'error'}\n\n"; exit; } } # =-=-= Mailing contents to those who are worthy -=-=-=-=-=-=-=-=-=-=-=-=-=-= $reply = $FORM{'email'}; $reply = $FORM{'Email'} if (!$reply); $reply = $FORM{'E-mail'} if (!$reply); $reply = $FORM{'E-Mail'} if (!$reply); $reply = "webform\@nowhere (Form Mailer)" if (!$reply); $indent = " " x ($maxlength+3); @reclist = split (/,/, $FORM{'recipients'}); foreach $address (@reclist) { $address =~ s/ //; if ($address =~ /..*\@..*/gio) { open (MAIL, "|$mailprog $address") || die "Unable to open $mailprog!\n"; print MAIL "From: $reply\n"; print MAIL "Reply-To: $reply\n"; print MAIL "To: $address\n"; print MAIL "Subject: $FORM{'subject'}\n\n"; foreach $pair (@pairs) { ($name, $value) = split(/=/, $pair); $value =~ tr/+/ /; $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $name =~ tr/+/ /; $name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg; $name =~ s/#//; if ($name eq "emptyline") { print MAIL $emptyline; } if ( ($name eq "recipients") || ($name eq "thanks") || ($name eq "error") || ($name eq "subject") || ($name eq "Send") || ($name eq "Submit") || ($name eq "submit") || ($name eq "emptyline") || ($value eq "0") || ($value eq "") ) { print ""; } else { $addspace = " " x ($maxlength - (length($name))); $value = &wrap($value,50); $value =~ s/\n/\n$indent/gi; print MAIL "$name:$addspace $value\n"; } } } } close MAIL; print "Location: $FORM{'thanks'}\n\n"; exit; # =-=-= Subroutine for wrapping long text fields -=-=-=-=-=-=-=-=-=-=-=-=-=-= sub wrap { local ($input,$linelength,@para,$para,@words,$word,$length,$line,$newtext,$newpara); ($input,$linelength) = (@_[0],@_[1]); $linelength++; @para = split(/\n/, $input); while (@para) { $para = shift @para; $para = $para."\n"; @words = split(/ /, $para); word: while (@words) { $word = shift @words; if ((length($word)+1) > $linelength) { if ($line) { $newtext = $newtext.$line."\n"; $line = ""; } $newtext = $newtext.$word."\n"; next word; } if ((length($line)+(length($word))) < $linelength) { $line = $line.$word." "; } else { unshift(@words,$word); chop $line; $newtext = $newtext.$line."\n"; $line = ""; $word = ""; } } if ($line) { $newtext = $newtext.$line;} $newpara=$newpara.$newtext; $newtext = ""; $word = ""; @words = ""; $line = ""; chop $newpara; } chop $newpara; $newpara; }