#!/usr/bin/perl -w
# FILE:           sendmail.pl
# DESCRIPTION:    Example code for sending mail in a Perl script.
# SMTP Mail part LAST MODIFIED:  1999.03.11
# the rest modified again by JKH 2003.11.11


  use Socket;
  use strict;
  use UCSExt;

  my $server = UCSExt->new("UCX:Server") or die "Can't get UCX:Server object";
  my $sname = $server->{"NAME"} or die "Can't get NAME from ucx:server object";
  my $volume_mgr = UCSExt->new("ucx:volumemgr") or die "Can't get ucx:volumemgr";
  my $volumes = $volume_mgr->{"VOLUMES"} or die "Can't get VOLUMES from ucx:volumemgr object";
  # Change these to values you'd like to use for your test.
  my($mailTo)     = 'you@yourcompany.com';
  my($mailServer) = 'mail.yourmailserver.com';
  my($mailFrom)   = 'John';
  my($realName)   = "$sname";
  my($subject)    = 'This mail message was sent via an SMTP conversation in Perl';
  my($body1)      = "Test of script.\n";
  my($body2)      = "Sent from $sname.\n";
  


  # These constants shouldn't need changing.
  my($packFormat) = 'S n a4 x8'; #Internet address format (AF_INET:portNumber:serverAddress:eightNulls)
  my($proto)      = getprotobyname("tcp")        || 6;
  my($port)       = getservbyname("SMTP", "tcp") || 25;
  my($name,$aliases,$addrtype,$len,@addrs) = gethostbyname($mailServer);
  my($serverAddr) = $addrs[0];
  
    if (! defined($len)) {
     die('gethostbyname failed.');
  }

  socket(SMTP, AF_INET(), SOCK_STREAM(), $proto)
     or die("socket: $!");

  connect(SMTP, pack($packFormat, AF_INET(), $port, $serverAddr))
     or die("connect: $!");

  select(SMTP); $| = 1; select(STDOUT);    # use unbuffered i/o.

  {
     my($inpBuf) = '';

     recv(SMTP, $inpBuf, 200, 0);
  }

  sendSMTP(1, "HELO\n");
  sendSMTP(1, "MAIL FROM: <$mailFrom>\n");
  sendSMTP(1, "RCPT TO: <$mailTo>\n");
  sendSMTP(1, "VRFY\n");
  sendSMTP(1, "DATA\n");

  sendSMTPnoresponse(1, "From: $realName\n");
  sendSMTPnoresponse(1, "Subject: $subject\n");
  sendSMTPnoresponse(1, "\n");
  
  while ($volumes->hasMoreElements()) {
  my $volume = $volumes->Next() or die "Can't get Next volume from volumes object";

  my($name, $size, $free, $bksz, $tdir) = @{$volume}{
     "Name", "TotalSpace", "FreeSpace", "BlockSize", "TotalDir"
  };
  my($freespm) = $free/1000;
  my($freespg) = $freespm/1000;
  my($totalsm) = $size/1000;
  my($totalsg) = $totalsm/1000;
  my($percentfree) = ($free/$size)*100;
  my $perfree = int($percentfree);
  
  if ($percentfree < 8){
	$body1 = "WARNING! $name only has $perfree percent free!!!\n  Better do some maintenance on $sname!\n \(That's only $freespm Meg out of $totalsm Meg total!\)\n";}
	else {$body1 = "It's okay, $name has $perfree percent free!!\n";}
	
  sendSMTPnoresponse(1, $body1);
  }
# print "Here's the message: $body1\n";

  sendSMTPnoresponse(1, $body2);
  sendSMTPnoresponse(1, "\n");
  sendSMTP(1, ".\n");
  sendSMTP(1, "QUIT\n");

  close(SMTP);

  sub closeSocket {     # close smtp socket on error
     close(SMTP);
     die("SMTP socket closed due to SIGINT\n");
  }

  sub send_to_smtp {
     my($debug)  = shift;
     my($response) = shift;
     my($buffer) = @_;

     print STDERR ("> $buffer") if $debug;
     send(SMTP, $buffer, 0);

     if ($response) {
       recv(SMTP, $buffer, 200, 0);
       print STDERR ("< $buffer") if $debug;
       return( (split(/ /, $buffer))[0] );
     }
  }

  sub sendSMTP {
    my ($debug) = shift;
    send_to_smtp($debug, 1, @_);
  }

  sub sendSMTPnoresponse {
    my ($debug) = shift;
    send_to_smtp($debug, 0, @_);
  }