# perl script for sending logfiles
# contributed by Tobias Richter, tobias@richter-tuerkheim.de
# place blat.exe, sendlog.pl \\server\install\bin
# some documentation included in appsonly.bat
# Release 2005.02.08
# GPLv2 and thanks to tim

#!perl
use strict;
use warnings;

my $Debug=0; # =0 gets NO console output
my ($DTS, $SMTP, $subject, $body, $filename, $gethostname, $ipaddress, $address);
DTS(); # go build the Date Time Stamp
filename(); #the log filename
gethostname(); #go get some hostname
ipaddress(); #go and give me your ipaddress

$SMTP="mailserver.domain.xx"; # the SMTP mail server name or IP address

$subject="\"Unattended Logfile $filename from $gethostname $ipaddress at $DTS\"";

my %Envelope = ( # Envelope bits
    -f  => 'mailfrom@domain.de', # FROM:
    -to => 'mailto@domain.de', # TO:
);

my %Data = ( # Data bits
        # you can add other parms here as needed
    -subject  => $subject,   # just what it sounds like!
    -server   => $SMTP,      # specify the SMTP server to use
    -debug    => " ",        # run Blat with debuging output
    -attach   => $filename,  # the filename
    -log      => "\"$0.log\"", # dump screen output to a file instead.
);

GetBodyFromFile($filename); # go get the message body from a file
                # put "D:/path/filename.txt" in place of $0 to use your own file
# $body = 'You can also set the $body Variable directly if you don\'t want to get the message body from a file';

SendIt($body, %Envelope, %Data);

sub SendIt {
        my $BlatCmd="Blat.exe"; # the Blat binary (path if needed)
        
        my $body = shift @_; # get the msg body
        $BlatCmd .= " - @_"; # add all the parms
        # now we have something like
        # blat.exe - -f tim@blat.tld -to tim@blat.tdl
        #          -debug -log "C:\mail\tims-blat.pl.log"
        #          -server localhost
        #          -subject "Testing blat 2003-8-20 12:46:6"

        print "\n\nCommand Line = $BlatCmd\n\nMessage Body = $body\n" if $Debug;

        open (MAIL, "| $BlatCmd") || die $!; # start Blat with all it's parms
        print MAIL $body; # now put in the msg body (bigger this way than CL)
}

sub DTS { # build the Date Time Stamp
        #Sec=$T[0],M=1,H=2, mDay=3,Mon=4,Yr=5, wDay=6,yDay=7, isDST=8
        my (@T)=localtime; ++$T[4]; $T[5]+=1900;
        $DTS = qq[$T[5]-$T[4]-$T[3] $T[2]:$T[1]:$T[0]];
}

sub filename {
$filename = $ARGV[0];
}

sub gethostname {
use Sys::Hostname; # use thie
$gethostname = hostname; # get hostname
}

sub ipaddress {
($address)=(gethostbyname($gethostname))[4]; #get ipaddress from hostname
$ipaddress= join(".",unpack("C4",$address))," "; #make it look nice
}

sub GetBodyFromFile {
        my $file = shift @_;
        local $/; # set the 'input record separator' to nothing
        open INFO, $file or die "Cannot open $file: $!"; # open it
        $body = <INFO>; # slurp it into $body
}
