#!/usr/bin/perl -w
# referral.pl

# configuration:

$sendmail = '/usr/sbin/sendmail';
$recipient = 'referral@naturalattachment.com';
$sender = 'Anonymous User'; # default sender?
$site_name = 'Success!'; # name of site to return to afterwards
$site_url = 'http://www.naturalattachment.com/success.html'; # 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 us shortly.</P>

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

</BODY>
</HTML>
EOF
