#!/usr/bin/perl -w
use CGI qw(:param);

## two methods to send mail...
$sendmail="/usr/sbin/sendmail";
$use_nsmtp=0;
BEGIN {
   $use_nsmtp=1 if (eval "use Net::SMTP");
}
## ...if Net::SMTP is installed use that,
## otherwise pipe to sendmail (or exim, etc.)
## Net::SMTP is more direct; sendmail queues.

# get data from web page posting
$subject=param("mailsubject");
   $name=param("name");
     $to=param("mailto");
   $from=param("mailfrom");
$message=param("message");
 $linkto=param("linkto");

# handle dodgy inputs
 $subject="Message from website" unless(defined($subject));
 $subject="Message from website" if ($subject eq "");
    $from="" unless(defined($from));
 $message="" unless(defined($message));
      $to=$owner unless(defined($to));
      $to=$owner if ($to eq "");
  $linkto="http://www.freedom2.com/" unless(defined($linkto));
  $linkto="http://www.freedom2.com/" if ($linkto eq "");
  $linkfrom=$ENV{HTTP_REFERER};
  if (defined($linkfrom)) {
    $linkfrom="<a href=\"".$linkfrom."\">Click here to go back to the page you were on.</a>";
  }
  
$to=lc($to);
$from=lc($from);

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

$err="";

if ($message eq "") {
  $err.="<li>No message was left - please type something in the message box!</li>\n";
}
if ($name eq "") {
  $err.="<li>No name entered - please tell us who you are!</li>\n";
}

if ($from ne "") {
  unless ($from =~ m~^[a-z0-9\.\-]{3,}\@([a-z0-9]{1,}[\-]{0,1}[a-z0-9]{1,}\.){1,2}([a-z0-9]{2}\.){0,1}[a-z0-9]{2,3}$~ix) {
    $err.="<li>Email address does not validate - please enter a valid email address</li>\n";
  }
} else {
  $err.="<li>No email address entered - please enter a valid email address</li>\n";
}

if ($err eq "") {
  #send_smtp_email($from,$to,$subject,$message,$name);

   if ($use_nsmtp==0) {
     send_email($from,$to,$subject,$message,$name);
   } else {
     send_netsmtp_email($from,$to,$subject,$message,$name);
   }

header("Mail sent",$linkto);

print "<h2>Message sent</h2>\n";
if ($use_nsmtp==0) {
  print "Thanks! Your message has been queued for sending, and should arrive in a while.\n";
} else {
  print "Thanks! Your message has been sent and should arrive soon.\n";
}
print <<EOF;
<p>
<a href="$linkto">Click here to continue</a><br>
(you should be redirected automatically in five seconds anyway).
<p>&nbsp;
EOF
footer();

} else {
header("Mail error!","");
print <<EOF;
<h2><font color="#FF0000">Mail error!</font></h2>
Your message couldn't be sent because:
<p><ul>
$err
</ul><p>
$linkfrom
<p>&nbsp;
EOF
footer();
}

sub send_email {
$frm=$_[0];
$to=$_[1];
$sub=$_[2];
$msg=$_[3];
$luser=$_[4];
open SENDMAIL, "|$sendmail -oi -t -odi" or die("Can't run sendmail\n");
print SENDMAIL <<EOF;
From: $frm
To: $to
Subject: $sub

$msg

Cheers,
$luser.
--
Sent from your Web site
$linkto

EOF
close SENDMAIL;
}

sub send_netsmtp_email {
$frm=$_[0];
$to=$_[1];
$sub=$_[2];
$msg=$_[3];
$luser=$_[4];

$smtp = Net::SMTP->new('localhost');
$smtp->mail("$frm");
$smtp->to("$to");

$smtp->data();
$smtp->datasend("To: $to\n");
$smtp->datasend("From: $frm <$luser>\n");
$smtp->datasend("Subject: $sub\n");
$smtp->datasend("\n");

$smtp->datasend("$msg\n");

$smtp->datasend("\nCheers,\n");
$smtp->datasend("$luser.");
$smtp->datasend("\n-- \n");
$smtp->datasend("Sent from your Web site\n");
$smtp->datasend("$linkto\n\n");

$smtp->dataend();

$smtp->quit;
}

sub header {
my $header_title=$_[0];
my $header_linkto=$_[1];
print <<EOF;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
 <title>Mail Sender: $header_title</title>
EOF
if ($header_linkto ne "") {
print "<meta http-equiv=\"refresh\" content=\"5; URL=$header_linkto\">\n";
}
print <<EOF;
 <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
 <meta http-equiv="content-language" content="en-GB">
 <meta name="robots" content="noindex,follow">
 <meta http-equiv="expires" content="now">
 <meta http-equiv="pragma" content="no-cache">
 <style type="text/css">
 <!--
 a:hover {color: #FF9900;}
 a:link  {color: #990000;}
 
 body {font-family: arial, helvetica, sans-serif;
       font-size: 11pt;}
   td {font-family: arial, helvetica, sans-serif;
       font-size: 11pt;}
       
 h1  {color: #FFFFFF;
      font-family: arial black, helvetica black, arial, helvetica, sans-serif;
      font-size: 24pt;
      margin-bottom: 0px;
      font-style:normal;}
 h2  {color: #cc3300;
      font-size: 18pt;
      font-family: arial, helvetica, sans-serif;
      margin-bottom: 0px;
      font-style:normal;}
 h3  {color: #cc3300;
      font-size: 16pt;
      font-family: arial, helvetica, sans-serif;
      margin-bottom: 0px;
      font-style:normal;}
 -->
 </style>
 </head>
 <body bgcolor="#FFFFFF" text="#000000" link="#990000" alink="#FF9900" vlink="#CC3300" marginwidth="0" marginheight="0" leftmargin="0" topmargin="0">

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td bgcolor="#DDDDDD"><img src="/gfx/pad.gif" alt=" " width="100%" height="2"></td></tr>
<tr><td bgcolor="#CCCCCC"><img src="/gfx/pad.gif" alt=" " width="100%" height="15"></td></tr>
<tr><td bgcolor="#999999"><img src="/gfx/pad.gif" alt=" " width="100%" height="2"></td></tr>
<tr><td bgcolor="#DD5500"><img src="/gfx/pad.gif" alt=" " width="100%" height="5"></td></tr>
<tr><td bgcolor="#FF6600"><img src="/gfx/pad.gif" alt=" " width="100%" height="5"></td></tr>
<tr><td align="center" bgcolor="#ff6600"><h1><font color="#ffffff">Mail Sender</font></h1></td></tr>

<tr><td bgcolor="#FF6600"><img src="/gfx/pad.gif" alt=" " width="100" height="5"></td></tr>
<tr><td bgcolor="#DDDDDD"><img src="/gfx/pad.gif" alt=" " width="100" height="2"></td></tr>
<tr><td bgcolor="#CCCCCC"><img src="/gfx/pad.gif" alt=" " width="100" height="8"></td></tr>
<tr><td bgcolor="#999999"><img src="/gfx/pad.gif" alt=" " width="100" height="3"></td></tr>
</table>

<!-- Main Area -->
<ul>
EOF
}

sub footer {
print <<EOF;
</ul>
<!-- / Main Area -->

<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr><td bgcolor="#DDDDDD"><img src="/gfx/pad.gif" alt=" " width="100" height="2"></td></tr>
<tr><td bgcolor="#CCCCCC"><img src="/gfx/pad.gif" alt=" " width="100" height="8"></td></tr>
<tr><td bgcolor="#999999"><img src="/gfx/pad.gif" alt=" " width="100" height="2"></td></tr>
<tr><td bgcolor="#DD5500"><img src="/gfx/pad.gif" alt=" " width="100" height="5"></td></tr>
<tr><td bgcolor="#FF6600"><img src="/gfx/pad.gif" alt=" " width="100" height="5"></td></tr>

<tr><td align="center" bgcolor="#ff6600">
<!-- some text here -->
</td></tr>

<tr><td bgcolor="#FF6600"><img src="/gfx/pad.gif" alt=" " width="100" height="5"></td></tr>
<tr><td bgcolor="#DDDDDD"><img src="/gfx/pad.gif" alt=" " width="100" height="2"></td></tr>
<tr><td bgcolor="#CCCCCC"><img src="/gfx/pad.gif" alt=" " width="100" height="8"></td></tr>
<tr><td bgcolor="#999999"><img src="/gfx/pad.gif" alt=" " width="100" height="3"></td></tr>
</table>
</body></html>
EOF
}
