#!/usr/bin/perl

# email_sender.cgi ... RJM Programming - April, 2014 ... emailing via sendmail

print "Content-type:text/html\n\n";

$emailpassword = "";
$valerr = "";
$warnerr = "";
$oursubject = "Default Subject";
$ourcontents = "";
$ourto = "";
$ourcc = "";
$ourbcc = "";
$ourfrom = "rmetcalfe@rjmprogramming.com.au";
$yourorigin = $ENV{'HTTP_REFERER'};
$ourdomain = $ENV{SERVER_NAME};

if ($yourorigin =~ m#^http://www.rjmprogramming.com.au/#) {   # Please be accessing the proper way ... 

 if ($ENV{'REQUEST_METHOD'} eq "GET") {    # GET mode of use ... 
   @yourpairs = split(/&/, $ENV{'QUERY_STRING'});
   foreach $yourpair (@yourpairs) {
     ($yourname, $yourvalue) = split(/=/, $yourpair);
     $yourvalue =~ tr/+/ /;
     $yourvalue =~ s/%([\dA-Fa-f]{2})/pack("C", hex($1))/eg;
     $getdata{$yourname} = $yourvalue;
     if ($yourname eq "Subject" || $yourname eq "Subject:") {
       $oursubject = $yourvalue;
     } elsif ($yourname eq "To" || $yourname eq "To:") {
       if ($yourvalue =~ /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) {
        $ourto = $yourvalue;
        $valerr = "";
       } elsif ($ourto eq "") {
        $valerr = "Please enter legal To email address.";
       }
     } elsif ($yourname eq "Passname" || $yourname eq "Passname:") {
       $emailpassword = "y";
     } elsif ($yourname eq "From" || $yourname eq "From:") {
       if ($yourvalue =~ /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) {
        $ourfrom = $yourvalue;
        $warnerr = s/Please enter legal From email address next time.//g;
       } elsif ($ourfrom eq "") {
        $warnerr .= "Please enter legal From email address next time. ";
       }
     } elsif ($yourname eq "Cc" || $yourname eq "Cc:") {
       if ($yourvalue =~ /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) {
        $ourcc = $yourvalue;
        $warnerr = s/Please enter legal Cc email address next time.//g;
       } elsif ($ourcc eq "") {
        $warnerr .= "Please enter legal Cc email address next time. ";
       }
     } elsif ($yourname eq "Bcc" || $yourname eq "Bcc:") {
       if ($yourvalue =~ /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) {
        $ourbcc = $yourvalue;
        $warnerr = s/Please enter legal Bcc email address next time.//g;
       } elsif ($ourbcc eq "") {
        $warnerr .= "Please enter legal Bcc email address next time. ";
       }
     } elsif ($yourname eq "Body" || $yourname eq "Body:") {
       $ourcontents .= ($yourvalue . "\n");
     } else {
       $yourname =~ s/%2B/+/g;
       $yourname =~ s/%2D/-/g;
       $yourname =~ s/%2A/*/g;
       $yourname =~ s/%2F/\//g;
       $yourname =~ s/%28/(/g;
       $yourname =~ s/%29/)/g;
       $ourcontents .= ($yourname . " = " . $yourvalue . "\n");
     }    
   }
 } else {  # POST from a form mode of use ...

   $emailpassword = "y";
   read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
   @ourpairs = split(/&/, $buffer);

   foreach $ourpair (@ourpairs) {
     ($ourname, $ourvalue) = split(/=/, $ourpair);
     $ourvalue =~ tr/+/ /;
     $ourvalue =~ s/%([\dA-Fa-f]{2})/pack("C", hex($1))/eg;
     $formdata{$ourname} = $ourvalue;
     if ($ourname eq "Subject" || $ourname eq "Subject:") {
       $oursubject = $ourvalue;
     } elsif ($ourname eq "To" || $ourname eq "To:") {
       if ($ourvalue =~ /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) {
        $ourto = $ourvalue;
        $valerr = "";
       } elsif ($ourto eq "") {
        $valerr = "Please enter legal To email address.";
       }
     } elsif ($ourname eq "From" || $ourname eq "From:") {
       if ($ourvalue =~ /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) {
        $ourfrom = $ourvalue;
        $warnerr = s/Please enter legal From email address next time.//g;
       } elsif ($ourfrom eq "") {
        $warnerr .= "Please enter legal From email address next time. ";
       }
     } elsif ($ourname eq "Cc" || $ourname eq "Cc:") {
       if ($ourvalue =~ /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) {
        $ourcc = $ourvalue;
        $warnerr = s/Please enter legal Cc email address next time.//g;
       } elsif ($ourcc eq "") {
        $warnerr .= "Please enter legal Cc email address next time. ";
       }
     } elsif ($ourname eq "Bcc" || $ourname eq "Bcc:") {
       if ($ourvalue =~ /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/) {
        $ourbcc = $ourvalue;
        $warnerr = s/Please enter legal Bcc email address next time.//g;
       } elsif ($ourbcc eq "") {
        $warnerr .= "Please enter legal Bcc email address next time. ";
       }
     } elsif ($ourname eq "Body" || $ourname eq "Body:") {
       $ourcontents .= ($ourvalue . "\n");
     } else {
       $ourname =~ s/%2B/+/g;
       $ourname =~ s/%2D/-/g;
       $ourname =~ s/%2A/*/g;
       $ourname =~ s/%2F/\//g;
       $ourname =~ s/%28/(/g;
       $ourname =~ s/%29/)/g;
       $ourcontents .= ($ourname . " = " . $ourvalue . "\n");
     }     
   }
   
 }
 
 if ($valerr ne "") {
    print "<p>$valerr</p>";  
 } elsif ($ourto ne "" && $oursubject ne "") {
   if ($emailpassword eq "y") {
    if ($yourorigin =~ m#^http://www.rjmprogramming.com.au/#) { 
      open(SENDMAIL, "|/usr/lib/sendmail -oi -t") or die "Can't fork for sendmail: $!\n";
      print SENDMAIL "From: $ourfrom\n";
      print SENDMAIL "To: $ourto\n";
      if ($ourcc ne "") {
       print SENDMAIL "Cc: $ourcc\n";
      }
      if ($ourbcc ne "") {
       print SENDMAIL "Bcc: $ourbcc\n";
      }
      print SENDMAIL "Subject: $oursubject\n\n";
      print SENDMAIL "$ourcontents\n";
      close(SENDMAIL)     or warn "sendmail didn't close nicely";
    } else {
      open(MAIL, "| /usr/sbin/sendmail -t") || &OurErrorMessage;
      print MAIL "From: $ourfrom\n";
      print MAIL "To: $ourto\n";
      if ($ourcc ne "") {
       print MAIL "Cc: $ourcc\n";
      }
      if ($ourbcc ne "") {
       print MAIL "Bcc: $ourbcc\n";
      }
      print MAIL "Subject: $oursubject\n\n";
      print MAIL "$ourcontents\n";
      close(MAIL);
    } 
    print "<h1>Thanks for sending your email which was sent at local time " . localtime() . ".</h1>&nbsp;<p>$warnerr</p>&nbsp;<a id='back' href='$yourorigin' title='Back'>Back</a>";  
   } else {
    print "<p>You do not have permission to do this. $warnerr</p>";  
   }
 } else {
   print "<p>Nothing to do.</p>";  
 }
 
}

sub OurErrorMessage {
   print "<p>Server has a problem with this.</p>";  
   exit; 
}
