package GUI::Email;

use System;
use Html;
use Util;
use strict;

######################################################
#   TEST EMAIL
######################################################

sub test {
  my($q) = @_;
  my($err, $MAILER, $addr, $header);

  my($renv, $dev, $host, $notifs) = PDM::ConfigFile->read();
  my $G = Labels->read();

  my($default) = $notifs->[0]->{email};
  my($h) = Html->header2("admin.gen_maint.test_email");
 
  print "\n<body bgcolor=$main'BGCOLOR><center>&nbsp;";

  if ($q->{ACTION_submit}) {
        ($renv, $dev, $host, $notifs) = PDM::ConfigFile->read();
        $MAILER = $renv->{mailer} || "/usr/bin/mail";
        $addr = $q->{email};
        if ($addr =~ /.+\@.+/) {
          $header= "From: StorADE Agent\nSubject: Test Message\n\n" .
                   "$G->{customer}: $renv->{customer}\n";
          open(O, "|$MAILER $addr");
          print O "$header"  . "-" x 50 . "\n" . $q->{message};
          close(O);
          $err = "<table border=0 bgcolor=white><tr><td><b><font color=green>$G->{delivered}</table>";
        } else  {
          $err = "<table border=0 bgcolor=white><tr><td><b><font color=red>Invalid Email Address, try again!</table>";
        }
  }


  my $o =<<EOF;
  $h
$err
&nbsp;
<table border=2 cellspacing=0 cellpadding=0 bgcolor=white width=90%><tr><td>
<form  method=get>
<input type=hidden name=GO value=GUI::Email::test>
<table border=0 cellspacing=2 cellpadding=2 bgcolor=white width=100%>
 <tr><td align=right bgcolor=$main'LIGHT><b>&nbsp;$G->{email_addr}: </td>
     <td><input type=text name=email size=30 value=\"$default\">
 <tr><td bgcolor=$main'LIGHT align=right><b>&nbsp;$G->{message}: </td>
     <td><input type=text name=message size=50>
</table></table>
<b>
<input type=submit name=ACTION_submit value="$G->{submit}">
</form>
EOF

  print $o;

}
1;

