#!/usr/bin/perl
# The line above tells the script where to find Perl. Ask
# your system administrator what the path to Perl is on
# your server and enter it on the line above. The bang (!)
# is mandatory.
#
# mailform.cgi
# http://www.macromedia.com/support/dreamweaver/
# 08/30/97
#
# FUNCTION
# -------------------
# Sends the contents of an HTML form via e-mail. This mailer is
# designed to print form fields that are named with a leading F
# and a number (for example, F01_TextField, F02_SelectField, 
# etc.) in order in the e-mail. Field names that do not begin 
# with F, a number, and an underscore are also allowed, but they 
# will not be printed in the body of the e-mail. This allows you 
# to collect names and e-mail addresses for inclusion in the header 
# of the mail without having to repeat them in the body. It also 
# allows you to determine the order of the fields without having 
# to put the field names in the script.
#
# CONFIGURATION
# -------------------
# The MAIL variable tells the script how to find the mailer on
# your system. If you are not sure about the path to the sendmail 
# program, ask your system administrator. The -t option allows 
# you to specify the recipients in the content of the message.

$MAIL="/usr/lib/sendmail -t";

#
#
use CGI;
$\="\n";

$req=new CGI;
print $req->header;

# THE MAIN EVENT
# -------------------
%fields=&read_fields;
&send_form;
&print_thanks_page;
exit(0);


# SUBROUTINES
# -------------------

sub read_fields{
  my(%fields);
  foreach $f ($req->param){
    $name=&clean_name($f);
    $fields{$f}{name}=$name;
    $value=&clean_value($f);
    $fields{$f}{value}=$value;
  }
  return(%fields);
}

# The read_fields subroutine reads in each of the field names and
# values from the form. It calls the clean_name and clean_value
# subroutines, which remove the leading number and underscore from 
# the name and concatenate multiple values, respectively.


sub clean_name{
  local($f)=shift;
  $f=~s/^F\d+_//;
  $f=~s/_/ /g;
  return($f);
}

sub clean_value{
  local($f)=shift;
  local(@val,$val);
  @val=$req->param($f);
  $#val-- unless $val[-1]=~/\S/;
  $val=join(" - ",@val);
  return($val);
}


sub send_form{
#  return unless $fields{'mailto'}{'value'}=~/^[\w-]+@(yourdomain)\.com$/;

# If you would like to ensure that mail is not re-routed to 
# another domain by a sneaky user who manipulates the 'mailto' 
# field, replace the word 'yourdomain' in the line above with 
# your actual domain. If your domain is a .net, .edu, or some 
# suffix other than .com, you'll also need to replace that. When 
# you've made the substitutions, uncomment the line by removing 
# the pound sign (#).

  open(MAIL,"| $MAIL") or error("can't send mail");
	
# This is where the $MAIL variable you defined at the top of the 
# script comes in. This line "pipes" the print MAIL commands to 
# the sendmail program on your server.
	
  print MAIL "To: $fields{'mailto'}{'value'}";
  print MAIL "From: $fields{'F01_name'}{'value'} <$fields{'F06_email'}{'value'}>";
  print MAIL "Reply-To: $fields{'F06_email'}{'value'}";
  print MAIL "Subject: $fields{'subject'}{'value'}";
  print MAIL "";
	
# This part of the send_form routine is customizable. It 
# currently relies on the fact that hidden form fields called 
# 'mailto' and 'subject' have been included in the form to define 
# the intended recipient(s) and the subject of the message, 
# respectively. These fields could be called something else, as 
# long as the names match in both the script and the form. The 
# 'Name' and 'E-mail' fields are also included in the form, but 
# their values are defined by the user. If you want to see how 
# the form works as-is before making any changes, simply change 
# the 'mailto' value to your own e-mail address in testform.html,
# fill out the form, and submit it. 
	
  $\="";
  $,=" - ";
  
  foreach $f (sort keys %fields){
    next unless $f=~/^F\d/;
    if ($fields{$f}{name} =~ /Comment/i) {
      print MAIL "\n";
      print MAIL "$fields{$f}{value}";
      print MAIL "\n";
    }
    else{
      print MAIL "$fields{$f}{name}: ";
      print MAIL ($fields{$f}{value} ? $fields{$f}{value} : @{$fields{$f}{'values'}});
      print MAIL "\n";
    }
  }
	
# This part of the send_form routine goes through each of the 
# fields, one at a time. The second line of the foreach loop 
# (line 125) says "skip to the next field unless the field name 
# begins with F and a number." This is what prevents the 
# unnumbered fields from printing. By the way, if you would like 
# to use a field in the header *and* have it appear in the body 
# of the message, simply assign it F and a number (for example, 
# F02_Email). Just make sure that the field is referenced as 
# F02_Email in the header section (lines 103-107). 
#
# Lines 126-130 say, "if the field name contains the word 
#'Comment' (in upper case, lowercase, or mixed case), print a 
# blank line, then the value of the field, and then another blank
# line before moving on to the next field. This conditional is 
# designed to set comments apart from other fields. If you'd like 
# to test for something other than 'Comment,' or something in 
# addition to 'Comment,' simply replace 'Comment' with the 
# keyword you expect to appear in the field name or add " || 
# /yourtext/i " (without the quotation marks) before the closing 
# parentheses on line 126.
#
# Lines 131-135 execute when the field name does not include the 
# word 'Comment.' They say, "First, print the field name and a 
# colon. Then, if there's a single value, print that; otherwise, 
# print the list of multiple values.
	
  $,="";
  close(MAIL);
}

# close(MAIL) wraps up the send_form function and sends the mail 
# to the specified recipient (you, at least during the testing 
# phase).


sub print_thanks_page{
  $\="";
  print <<"EOM";
<HTML>
<HEAD>
<TITLE>Vielen Dank für Ihr Interesse</TITLE>
</HEAD>
<BODY BGCOLOR="#525a67" TEXT="#FFFFFF">
<font size="3" face="Arial, Helvetica, sans-serif">
<pre>
Vielen Dank $fields{'F20_anrede'}{'value'} $fields{'F01_name'}{'value'} für Ihre Bestellung!
Wir liefern innerhalb 24 Stunden.
<hr>
Lieferadresse:
		$fields{'F01_name'}{'value'}, $fields{'F02_vorname'}{'value'}
		$fields{'F03_strasse'}{'value'}
		$fields{'F05_plz'}{'value'}  $fields{'F04_ort'}{'value'}
		$fields{'F06_email'}{'value'}
<hr>
Ihre Bestellung:
		
$fields{'F19_Bestellung'}{'value'}

<hr>

Wir danken Ihnen für Ihr Vertrauen.
Ihr KMT-PC-Shop.
</pre> 
 
</font>
</BODY>
</HTML>

EOM
}





