#!/usr/bin/perl
print "Content-type: text/html", "\n\n";
print "<html>\n";
print "<body background=\"nevada.gif\">\n";
require TripodCGI;
use CGI;
$cgi = new CGI;
#
# get the information they entered on the form
#
$from=$cgi->param('from');
$subject=$cgi->param('subject');
$text=$cgi->param('text');
$from=$cgi->param('from');
$bcc_file="krud.txt";
&check_errors;
open(EMAIL,"|/usr/sbin/sendmail -t");
print EMAIL "TO: $from\n";
print EMAIL "FROM: $from\n";
print EMAIL "sender: $from\n";
print EMAIL "return-path: $from\n";
print EMAIL "Subject: $subject\n";
open (BCC,"$bcc_file");
while (<BCC>) {
 print EMAIL "BCC: $_";
 $sentletters++;
}
close(BCC);
print EMAIL "$text\n";
print EMAIL "\n";
print EMAIL ".\n";
close(EMAIL);
print "$sentletters e-mails sent\n";
exit;
sub check_errors {
 $errors = 0;
 if (! -f $bcc_file) {
  print "ERROR! file $bcc_file does not exist!<br>";
  $errors++;
 }
 if (-z $bcc_file) {
  print "ERROR! file $bcc_file is empty and contains no email addresses!<br>";
  $errors++;
 }
 if ( $from =~ /^ *$/ ) {
  print "ERROR! From e-mail address required!<br>";
  $errors++;
 }
 else {
  if ( $from !~ /@/ ) {
   print "ERROR! From e-mail address must contain an \@ character<br>";
   $errors++;
  }
  else {
   if ( $from !~ /.@/ ) {
     print "ERROR! From e-mail '$from' address must contain a username like username\@site.com<br>";
     $errors++;
   }
   if ( $from !~ /@./ ) {
     print "ERROR! From e-mail '$from' address must contain a site name like username\@sitename.com<br>";
     $errors++;
   }
   if ( $from !~ /@.*[^.]\.[^.]/ ) {
     print "ERROR! From e-mail '$from' address site name must contain a "." like  username\@sitename.com<br>";
     $errors++;
   }
   if ( $from =~ /\.$/ ) {
     print "ERROR! From e-mail '$from' address site name can't end with a "."<br>";
     $errors++;
   }
  }
 }
 if ( $subject =~ /^ *$/ ) {
  print "ERROR! Subject required!<br>";
  $errors++;
 }
 if ( $text =~ /^ *$/ ) {
  print "ERROR! Some Text in the body is required!<br>";
  $errors++;
 }
 if ($errors > 0 ) {
  print "Error! E-mail(s) not sent<p>";
  exit;
 }
 return;
}