#!/usr/bin/perl ### web2mail -- An anonymous CGI mailer. ### See http://www.halcyon.com/sanford/cgi/web2mail/web2mail.html ### for usage and description. ### Copyright 1995-6, Sanford Morton ### ### Modified 8April97 by Alan McLean ### - Added timestamp when saved to file. ### - Removed the requirement for .email_target ### when .writefile specified. Now, when ### .email_target is specified with .writefile ### an email message is sent informing that new ### data has been saved (or an order has been made). ### ### Configuration Variables ### $home=$ENV{"DOCUMENT_ROOT"}; ### the local mail program -- without sendmail, revise &send_mail $mail_program = "/usr/lib/sendmail -t"; ### instructions page -- make sure this is current $instructions_url = "http://www.halcyon.com/sanford/cgi/web2mail/web2mail.html"; ### ### Main body of script ### ### parse the form data &ReadParse; #### is required email address supplied #if ( ! $in{'.email_target'} ) { # print &instructions_page; # exit; #} ### any required data? if so, has user supplied it? if ( $in{'.required_data'} && &missing_data ) { print &incomplete_page; exit; } ### prepare first line of mail message $message = $in{'.mail_intro'} ? "$in{'.mail_intro'}\n\n" : "Form data submitted to $ENV{'HTTP_REFERER'}:\n\n"; ### write the form data to the mail message foreach (sort keys %in) { next if /^\./; # skip hidden form data in mail message $item = "$_: $in{$_}"; $item =~ s/^..// if $in{'.remove_indexing'}; $item =~ s/: on$//; # remove checkbox flags $message .= "\t$item\n"; } ## do you want the environment variables? if ( $in{'.environment'} ) { $message .= "\nThe environment variables are:\n\n"; foreach (keys %ENV) { $message .= "\t$_: $ENV{$_}\n"; } } ## prepare subject line in mail message $subject_line = $in{'.mail_subject'} ? "$in{'.mail_subject'}" : "Form data submitted to $ENV{'HTTP_REFERER'}"; ## mail it (or web it if in test mode) &send_mail ($mail_program, $in{'.email_target'}, $subject_line, $message); ## Return an acknowledgement page &acknowledge_page; ### ### End of main body of script. ### Subroutines below. ### ### ### Return an acknowledgement page. ### Look first for a matching custom response url, ### then for a thanks page, or send the default acknowledgement page. ### sub acknowledge_page { local($i, @triples); ## search for a match among custom responses if ( $in{'.custom_response_url'} ) { ## for each custom response, the triple is: ## form tag name :: triggering value :: response url @triples = split( /::/, $in{'.custom_response_url'} ); for ($i=0; $i < $#triples; $i+=3) { ## if name matches value if ( $in{"$triples[$i]"} eq $triples[$i+1] ) { ## print URL if ( $in{'.test'} ) { # test mode print "The acknowledgement page that would be returned is: $triples[$i+2]"; } else { print "Location: $triples[$i+2]\n\n"; } exit; } } } ## if no custom response matches, return the supplied ## base thanks page, if any if ( $in{'.thanks_url'} ) { if ( $in{'.test'} ) { # test mode print "The acknowledgement page that would be returned is: $in{'.thanks_url'}"; } else { print "Location: $in{'.thanks_url'}\n\n"; } ## or just return the built-in default } else { print &thanks_page; } } ### ### .email_target form data is missing, return an instruction page ### sub instructions_page { local ($page_source) = <<"INSTRUCTIONS_PAGE"; Content-type: text/html\n\n Oops, .email_target is missing

Oops, .email_target is missing

You must include a hidden form tag called .email_target whose value is the email address to which you want to mail the form data. Something like
<INPUT TYPE="hidden" NAME=".email_target" VALUE="user\@site.com">
For your information, the form elements and their values that actually were supplied in this page were:You can return to the instructions page describing the use of this mailing script, or press your BACK button to return to the web form. LAST_PART return $page_source; } ### ### send mail containing the form data ### sub send_mail { local ($mail_program, $email_address, $subject, $message) = @_; if ($in{'.test'}) { # test mode. Send mail to web page print <<"TEST"; Content-type: text/html\n\n

Here is the letter that would be mailed:

To: $email_address
Subject: $subject
\n\n$message\n

The rest of this page is the Thanks page


TEST } else { # production mode. Send mail to mail. if ($in{'.writefile'}) { # # Here's where it sets the file name that it's going to write to. # Historically, it just used the name in .writefile, however, due # to security concerns, .writefile will now contain the name of # the template file to use to get the actual filename. # $template=$home.$in{'.writefile'}; if (-f $template) { if (open(TEMPLATE,$template)) { $output=$home.