#!/usr/bin/perl -w
# ======================================================================
#
# Perl Source File -- Created with SAPIEN Technologies PrimalScript 3.1
#
# NAME: smtp_send.pl
#
# ORIGINAL AUTHOR: Scott Herold , RapidApp
# ORIGINAL DATE  : 11/30/2004
#
# MODIFY AUTHOR: Jeremy Pries, Xcedex (jpries-at-xcedex-dot-com)
# MODIFY DATE  : 4/20/2005
#
# MODIFY AUTHOR: Duncan Epping, Yellow-Bricks.com
# MODIFY DATE  : 9/7/2007
# MODIFIED: Line 99 changed TEXT to TEXT/HTML
#
# PURPOSE: This is a small script that can send emails through an
# external smtp gateway.  This is useful when sending log files from an
# ESX host to an administrator or group on a scheduled basis. 
#
# Handles piped (|) input, command line message content and file attachments
#
# PREREQUESITES: The MIME-Lite module is required for this script 
# to function.
#
# http://search.cpan.org/~yves/MIME-Lite-3.01/lib/MIME/Lite.pm
#
# This module must be placed in the following directory of your ESX host:
# (Note, the directory will need to be created and is case sensative)
#
# /usr/lib/perl5/5.6.1/MIME
#
#
# ======================================================================

use MIME::Lite;
use Sys::Hostname;
use Getopt::Long;

# Variables to configure
#
# From Address.  By default it just uses a hostname lookup.  You may change this to something else if desired.
my $fromAddress = hostname();

# Set your smarthost here
my $smartHost = "smarthost.example.com";

### Command Line Arguments
if (@ARGV < 2) {
   print "\n";
   print "Usage $0: [-t address] [-s subject] [-m Body] [-a path] [-f address] [-r smarthost]\n\n";
   print "-Enclose any options that contains spaces in quotes\n";
   print "-All options may be shortened to one character.  Ex. -t instead of -toAddress\n\n";
   print "Options:\n";
   print "  -toAddress		rcpt to address (required)\n";
   print "  -subject		subect (required)\n";
   print "  -messageBody		body (optional)\n";
   print "  -attach		full path to attachment (optional)\n";
   print "  -fromAddress		mail from address (optional)\n";
   print "  -relay		smarthost/relayhost used to deliver mail. (optional)\n\n";
   print "-If no fromAddress is specified, the address $fromAddress will be used\n\n";
   print "-If no relay is specified, the variable $smartHost in the script is used.\n";
   print " You may wish to change the value of this variable to prevent need to specify it on the command line.\n\n";
   print "As of version 0.2, script will handle message body input via a pipe.  If input is received via a pipe\n";
   print " and message body on command line, the command line text will precede the pipe input in the body.\n";
   print "\n";
   exit(1);
}

my $toAddress = '';
my $subject = '';
my $messageBody = '';
my $attach = 'none';
GetOptions ('toAddress=s' => \$toAddress,'subject=s' => \$subject, 'messageBody=s' => \$messageBody, 'attach=s' => \$attach, 'fromAddress=s' => \$fromAddress, 'relay=s' => \$smartHost);

unless (-t) {
  $messageBody = $messageBody . "\n";
  while ( $line = <STDIN> ) {
    chomp $line;
    $messageBody = $messageBody . $line . "\n";
  }
}
print "smartHost: $smartHost\n";
print "to: $toAddress\n";
print "subject: $subject\n";
print "attach: $attach\n";

### Default Email header stuff
$msg = MIME::Lite->new(
From =>$fromAddress,
To =>$toAddress,
Subject =>$subject,
Type =>'multipart/mixed'
);


### The text message portion of the email. Remember \n to break lines
$msg->attach(Type =>'TEXT/HTML',
Data =>$messageBody
);

### Attach a file if necessary
if ($attach ne "none") {
### Path is full path to file, Filename is file name as attached (Can be
### different than the source file name)
$msg->attach(Type =>'applications/zip',
Path =>$attach
);
}

### Mail type and SMTP server. Send email based off those settings
MIME::Lite->send('smtp', $smartHost);
$msg->send;
