#!/usr/bin/perl -wT
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use strict;

print header;
print start_html("Results");

# first print the mail message...
my $recipient = 'nullbox@cgi101.com';

$ENV{PATH} = "/usr/sbin";
open (MAIL, "|/usr/sbin/sendmail -oi -t -odq") or 
   &dienice("Can't fork for sendmail: $!\n");
print MAIL "To: $recipient\n";
print MAIL "From: webserver\n";
print MAIL "Subject: Guestbook form\n\n";
foreach my $p (param()) {
   print MAIL "$p = ", param($p), "\n";
}
close(MAIL);

# now write to the file

open(OUT, ">>guestbook.txt") or &dienice("Couldn't open output file: $!");
foreach my $p (param()) {
   print OUT "$p = ", param($p), ",";
}
print OUT "\n";
close(OUT);

print <<EndHTML;
<h2>Thank You</h2>
<p>Thank you for writing!</p>
<p>Return to our <a href="index.html">home page</a>.</p>
EndHTML

print end_html;

sub dienice {
        my($errmsg) = @_;
        print "<h2>Error</h2>\n";
        print "<p>$errmsg</p>\n";
        print end_html;
        exit;
}