#!/usr/bin/perl
#
use DBI;
#
$mailprog = '/usr/sbin/sendmail';
$absender = 'prorataweb@prorata.de';
$subject = 'Information-Anforderung durch Musterbefragung';
#
my $DB_DSN      = "DBI:mysql:prorata2:sql";
my $DB_USER     = "prorata";
my $DB_PASSWD   = "altoids7";
#
my $dbh = DBI->connect($DB_DSN, $DB_USER, 
   $DB_PASSWD) or err::error_seite{"Cannot connect to DB"};
   my $FROM="";
   my $TO="info@prorata.de";
   my $date = getdate();


open(LOG,">>/var/qmail/alias/abmeldungen.log") or exit 99;

print LOG "$date:\n"."Löschvorgang eingeleitet.........\n";



   ($firma)=$dbh->selectrow_array("select text from ergebnis where umfid=59 AND fid=12 AND aid=1");
   ($anrede)=$dbh->selectrow_array("select text from ergebnis where umfid=59 AND fid=12 AND aid=2");
   ($vname)=$dbh->selectrow_array("select text from ergebnis where umfid=59 AND fid=12 AND aid=3");
   ($nname)=$dbh->selectrow_array("select text from ergebnis where umfid=59 AND fid=12 AND aid=4");

   ($email)=$dbh->selectrow_array("select text from ergebnis where umfid=59 AND fid=12 AND aid=7");
   ($telefon)=$dbh->selectrow_array("select text from ergebnis where umfid=59 AND fid=12 AND aid=5");
   ($fax)=$dbh->selectrow_array("select text from ergebnis where umfid=59 AND fid=12 AND aid=6");


   if (($email != "") or ($telefon != "") or ($fax != "")){
   $dbh->disconnect;
   benachrichtigungsmail();
   } else {$dbh->disconnect;}

   close LOG;
   exit 0;


   sub benachrichtigungsmail {
   # Open The Mail Program
     open(MAIL,"|$mailprog -t");
     #open(MAIL,">>tehest");
     my $date = getdate();

     print MAIL "To: $TO\n";
     print MAIL "From: $absender\n";

    # Print Message Subject
     print MAIL "Subject: $subject \n\n";
     print MAIL "Hallo!  \n\n";
     print MAIL "durch Musterbefragung wurde Information angefordert.\n\n";
     print MAIL "Interessent:\n\n";
     print MAIL "Firma: $firma\n\n";
     print MAIL "Anrede: $anrede \n\n";
     print MAIL "Vorname: $vname\n\n";
     print MAIL "Nachname: $nname \n\n";
     print MAIL "eMail-Adresse: $email\n\n";
     print MAIL "Telefon: $telefon \n\n";
     print MAIL "Fax: $fax \n\n";
     print MAIL "Mit freundlichem Gruß\n\nPRORATA.WEB - TEAM!\n";
     print MAIL "-------------------------------------------------------\n\n".
                 "Automatisch erstellt am ".$date."\n";
     close (MAIL);
     print "Benachrichtigung geschickt";
     $dbh->disconnect;
     exit;
  }


  ####################################################################
  #####         Gibt die locale Zeit aus
  #####         Argumente  :
  #####         Returnwert : -
  #####################################################################
  #
  sub getdate {
  
     @days   = ('Sonntag','Montag','Dienstag','Mittwoch',
               'Donnerstag','Freitag','Samstag');
     @months = ('Januar','Februar','März','April','Mai','Juni','Juli',
                'August','September','Oktober','November','Dezember');
    ($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
     $time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);
     $year += 1900;
     $date = "$days[$wday], $months[$mon] $mday, $year um $time";
     return $date;
  }
