#!/usr/bin/perl
#
# SendMail.pl -- This is a simple SendMail.pm module
#		     to send a mail.
# $Id: SendMail.pl 74 2011-04-20 19:55:19Z  $

use warnings;
use strict;
use SendMail 2.09;

# config stuff
my $value;
my $debug;
my $smtp_server;
my $smtp_port;
my $AuthReq;
my $AuthLogin;
my $AuthPass;
my $Sender;

# main 
my @body_data = <STDIN>;
my $numArgs = $#ARGV + 1;
my $EAddr;
my $SubJect;
my $sm;
my $argnum;

# Read Configuation File
open(CFG, "/home/irlp/custom/SendMail/SendMail.cfg") || die("Could not open file!");
while (<CFG>) {
    s/#.*//;            # ignore comments by erasing them
    next if /^(\s)*$/;  # skip blank lines
    chomp;
    next unless my($key,$value) = split /=/;
    # s/^"//, s/"$// for $key, $value;
    s/^"//,s/"$// for $key, $value;

    #remove whitespaces "value=ON     "
    $value =~ s/(^\s+|\s+$)//g;

    # simple parse
    if ($key eq 'debug') {
        $debug=$value;
    } elsif ($key eq 'smtp_server') {
        $smtp_server=$value;
    } elsif ($key eq 'smtp_port') {
        $smtp_port=$value;
    } elsif ($key eq 'AuthReq') {
        $AuthReq=$value;
    } elsif ($key eq 'AuthLogin') {
        $AuthLogin=$value;
    } elsif ($key eq 'AuthPass') {
        $AuthPass=$value;
    } elsif ($key eq 'Sender') {
        $Sender=$value;
    }
}
close CFG;

if ($debug eq 'ON') {
    print "== Debug Start ================\n";
    print "debug=[$debug] \n";
    print "smtp_server=[$smtp_server] \n";
    print "smtp_port=[$smtp_port] \n";
    print "AuthReq=[$AuthReq] \n";
    print "AuthLogin=[$AuthLogin] \n";
    print "AuthPass=[$AuthPass] \n";
    print "Sender=[$Sender] \n\n";
}

# check minimal required variables smtp_server, smtp_org, Sender


## debug info
if ($debug eq 'ON') {
    print "============================\n";
    print "$numArgs command-line arguments:\n";

    foreach $argnum (0 .. $#ARGV) {
        print "      $argnum -- $ARGV[$argnum]\n";
    }
}

## if no arguments, exit with syntax rule  
if ($numArgs == 0) {
    print "Command Syntax...\n";
    exit 0;
}

# if argv 0 = -s then argv1 is the subject, if not subject=blank  

if ($ARGV[0] eq '-s') {
    $SubJect = $ARGV[1];
    $EAddr = $ARGV[2];
} else {
    # else set subject to blank, and set email address
    $SubJect = "";
    $EAddr = $ARGV[0];
}

##  debug stuff
if ($debug eq 'ON') {
    print "============================\n";
    if ($numArgs == 2) {
      print "ArgV1: $ARGV[1]\n";
    }
    print "Subject is: $SubJect \n";
    print "Email Address is $EAddr \n";
}

#
# Create the object without any arguments, # i.e. localhost is the default SMTP server.
#
$sm = new SendMail("$smtp_server", $smtp_port);

## Set SMTP AUTH login profile.
if ($AuthReq eq 'YES') {
    $sm->setAuth($sm->AUTHLOGIN, "$AuthLogin", "$AuthPass");
}
 
## Turn on debugging in SendMail Module
#
if ($debug eq 'ON') {
    # We set the debug mode "ON".
    #
    $sm->setDebug($sm->ON);
} else {
    $sm->setDebug($sm->OFF);
}
 
#
# We set the sender.
#
$sm->From("<$Sender>");
 
## Set the Subject
#
$sm->Subject("$SubJect");

## use argument for address
#
$sm->To("<$EAddr>");

## use STDIN for body of text
#
$sm->setMailBody("@body_data");

### Not used
#
# Attach a testing image.
#
## $sm->Attach("./welcome.gif");

## Check if the mail sent successfully or not.
#
if ($sm->sendMail() != 0) {
    print $sm->{'error'}."\n";
    exit -1;
}

#
# Mail sent successfully.
#
if ($debug eq 'ON') {
    print "==Debug End ====================\n";
}

exit 0;
