#!/usr/local/bin/perl -w

# mail_form.cgi

# bundle up form output and mail it to the specified address

# This program is copyright 1999 by John Callender.

# This program is free and open software. You may use, copy, modify,
# distribute and sell this program (and any modified variants) in any way
# you wish, provided you do not restrict others from doing the same.

# configuration:

$sendmail = '/usr/bin/mail'; # where is sendmail?
$recipient = 'testament@naturalattachment.com'; # who gets the form data?
$sender = 'Anonymous User'; # default sender?
$site_name = 'my site'; # name of site to return to afterwards
$site_url = '/begperl/'; # URL of site to return to afterwards

# script proper begins...

use CGI;
$query = new CGI;

# bundle up form submissions into a mail_body

$mail_body = '';

foreach $field (sort ($query->param)) {
    foreach $value ($query->param($field)) {
        $mail_body .= "$field: $value\n";
    }
}

# set an appropriate From: address

if ($email = $query->param('07_email')) {
    # the user supplied an email address
    if ($name = $query->param('01_name')) {
        # the user supplied a name
        $name =~ s/"//g; # lose any double-quotes in name
        $sender = "\"$name\" <$email>";
    } else {
        # user did not supply a name
        $sender = "$email";
    }
}

# send the email message

open(MAIL, "|$sendmail -oi -t") or die "Can't open pipe to $sendmail:
$!\n";
print MAIL "To: $recipient\n";
print MAIL "From: $sender\n"; 
print MAIL "Subject: Sample Web Form Submission\n\n";
print MAIL "$mail_body";
close(MAIL) or die "Can't close pipe to $sendmail: $!\n";

# now show the thank-you screen

print "Content-type: text/html\n\n";
print <<"EOF";
<HTML>
<HEAD>
<TITLE>Thank you</TITLE>
</HEAD>

<BODY>

<H1>Thank you</H1>

<P>Thank you for your form submission. You will be hearing 
from me shortly.</P>

<P>Return to 
<A HREF="$site_url">$site_name</A>.</P>

</BODY>
</HTML>
EOF
