@lay:3449l2

------------------------------------------------------------
# Listing 2: A version of send-mail.pl that allows mail to be sent to
# a restricted list of recipients

#!/usr/bin/perl -wT

use strict;
use diagnostics;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use Mail::Sendmail;

# Create a new instance of CGI
my $query = new CGI;

# Send a MIME header to the user's browser
print $query->header;

# Set up a list of approved recipients
my %approved_recipient = ('reuven@lerner.co.il' => 1,
			  'ljeditor@ssc.com' => 1);

# Get the fields from the HTML form
my $sender = $query->param("sender");
my $recipient = $query->param("recipient");
my $message = $query->param("message");

# Make sure that the recipient is approved
if (!$approved_recipient{$recipient})
{
    die "Unapproved address \"$recipient\": Mail was not sent.\n";
}

# Create the mail message
my %mail = (To => $recipient,
	    From => $sender,
	    Message => $message);

# Send the message
if (sendmail %mail) 
{
    # Send some output to the user's browser
    print $query->start_html(-title => "Success!");

    print "<H1>Success!</H1>\n";
    print "<P>The following message was successfully sent:</P>\n";

    print "<Form method=\"POST\" action=\"\">\n";
    print "<P><HR></P>\n";
    print "<P>Sender: $sender</P>\n";
    print "<P>Recipient: $recipient</P>\n";
    print "<P>Message:<P></P>";
    print "<textarea rows=\"20\" cols=\"60\">$message</textarea></P>\n";
    print "<P><HR></P>\n";
    print "</Form>\n";

    print $query->end_html;
}
else
{
    print "Error sending mail: $Mail::Sendmail::error \n";
}
