#!/usr/bin/perl
#
# 1) for this program to work your site must be a unix, linux or 
#    some other OS (like newer MACS) that that has the sendmail command
# 
# 2) your site must also allow you to use the sendmail command
#    when you run the CGI program
#
# 3) your site also must have a perl compiler for you to run
#    
# 4) your site must also allow you to run the perl compiler 
#    when you run CGI programs
#
# 5) in this program the path to the PERL compiler is
#         /usr/bin/perl
#    if your computer site has the perl compiler stored
#    in a different path you will have to change that.
#    that path is at the very 1st line of this program
#    look up to find it.
#
# 6) in this program the path to the sendmail command is
#         /usr/sbin/sendmail
#    if your computer site has the sendmail command stored
#    in a different path you will have to change that.
#
# 7) i orginally wrote the program on a www.tripod.com
#    free web site and i used their free software to 
#    process the data coming in from the web site. the files
#        TripodCGI.pm
#        CGI.pm
#    must be copied to your cgi-bin directory for the
#    perl program to work. they can be gotten for free
#    from www.tripod.com
#    and i have them displayed on the tripod website
#        http://phoenix_copwatch.tripod.com
#  
# 
# this program is used to send press releases to many email addresses
#
# the program displays a HTML form and the user must enter
# a from email address, a subject of the press release and
# the text of the press release.
#
# when the user clicks on the SUBMIT button an email is
# generate and mailed to all the email addresses in the
# file krud.txt
#
# the email appears to be sent from the "from email address"
# the user entered in the form with the subject name.
#
# the default heading or title printed on the HTML form is
#
#       Send Press Releases to Arizona Media
#
# you can change the both the name of the file name
# which contains the email addresses and the name of
# the default heading by passing emailfile and  title
# as variable to the program. this is done by coding
# them as
#
#           title=xxxxx
#           emailfile=xxxxx
#
# xxxxxx can not contain spaces or other special characters
# if you wish for xxxxx to contains spaces or other special
# characters you must put write the character in hex as %xx
# such as:
#
#           title=Press%20Releases%20For%20Indy%20Media
#
#
# the default url is:
#
#        href="cgi-bin/email_perl2.pl"
#
# in your html you add the variables to the end of the
# url  by first adding a "?" after the URL and the first
# variable as in:
#
#        href="cgi-bin/email_perl2.pl?emailfile=foo.txt"
#
# if you wish to add a second variable to this mess you
# add an "&" to theprior mess and add the next variable
#
#        href="cgi-bin/email_perl2.pl?emailfile=foo.txt&title=new%20title"
#
# if you have a 3rd or more variable to add you would add another
# "&" for each new variable followed by the variable name and text.
#
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');
#
# remove leading spaces from the from email address
#
$from=~s/^ *//;
#
# remove trailing spaces from the from email address
#
$from=~s/ *$//;
$subject=$cgi->param('subject');
#
# remove leading and trailing spaces from the subject
#
$subject=~s/^ *//;
$subject=~s/ *$//;
if ($subject eq "" ) {
 $subject = "Press Release - ";
}
$text=$cgi->param('text');
#
# if they pass us a title in the variable "title"
# use it for the title.
# else use a default title
#
#     you set the title by calling the program like this
#         email_perl2.pl?title=xxxxxxx
#         in xxxx all spaces and other special characters must be converted to hex
#     you set the title and emailfile by calling the program like this
#         email_perl2.pl?title=xxxxxxx&emailfile=xxxxxxx
#         in xxxx all spaces and other special characters must be converted to hex

$title=$cgi->param('title');
if ($title eq "" ) {
 $title = "Send Press Releases to Arizona Media";
}
#
# they can pass us a file name in the variable "emailfile"
# else use the default file of krud.txt
#
#
#     you set the emailfile by calling the program like this
#         email_perl2.pl?emailfile=xxxxxxx
#         in xxxx all spaces and other special characters must be converted to hex
#     you set the emailfile and title by calling the program like this
#         email_perl2.pl?emailfile=xxxxxxx&title=xxxxxxx
#         in xxxx all spaces and other special characters must be converted to hex
$emailfile=$cgi->param('emailfile');
if ($emailfile eq "") {
 $emailfile="krud.txt";
}
#
# verify that the email file exists before we let the user play with the program
#
&does_email_file_exist;
#
# first time thru display the stinking form
#
if ( $from eq "" && $text eq "" ) {
 &display_form;
 exit;
}
&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,"$emailfile");
while (<BCC>) {
 print EMAIL "BCC: $_";
 $sentletters++;
}
close(BCC);
print EMAIL "$text\n";
print EMAIL "\n";
print EMAIL ".\n";
close(EMAIL);
print "<font color=\"red\" size=\"300%\">$sentletters e-mails sent from $emailfile</font>\n";
&display_form;
exit;
sub check_errors {
 $errors = 0;
 if (does_email_file_exist()) {
  print "ERROR! file $emailfile does not exist!<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>";
    &display_form;
    exit;
 }
 return;
}
sub display_form {
 print "<h1 align=\"center\">$title</h1>\n";
 print "<center>\n";
 print "<table border=\"0\">\n";
 print "<tr>\n";
 print "<td>\n";
 print "<form action=\"email_perl2.pl\">\n";
 print "<table border=\"0\">\n";
 print "<tr valign=\"top\"><td>From:</td><td> <input type=\"text\" size=\"60\" name=\"from\" value=\"$from\"></td></tr>\n";
 print "<tr valign=\"top\"><td>Subject:</td><td> <input type=\"text\" size=\"60\" name=\"subject\" value=\"$subject\"></td></tr>\n";
 print "</table>\n";
 print "<p>\n";
 print "<input type=\"submit\" name=submit value=\"Send Press Release\"> \n";
 print "<p>\n";
 print "Text of Press Release - NO GRAPHICS ALLOWED - just ASCII text!<br>\n";
 print "<textarea name=\"text\" cols=\"79\" rows=\"20\">$text</textarea>\n";
 print "<p>\n";
 print "<input type=\"reset\" name=reset value=\"CLEAR MENU\">\n";
 print "<input type=\"hidden\" name=\"title\" value=\"$title\">\n";
 print "<input type=\"hidden\" name=\"emailfile\" value=\"$emailfile\">\n";
 print "</form>\n";
 print "</td>\n";
 print "</tr>\n";
 print "</table>\n";
 print "</center>\n";
 return;
}
sub does_email_file_exist {
 $rc=0;
 if (! -f $emailfile) {
  print "<font size=\"300%\" color=\"red\">ERROR! file $emailfile does not exist!</font><p>";
  $rc = 1;
 }
 if (-z $emailfile) {
  print "<font size=\"300%\" color=\"red\">ERROR! file $emailfile is empty and contains no email addresses!</font><p>";
  $rc = 1;
 }
 return $rc;
}