Tuesday, October 22, 2013

SMTP Perl example / Send email with Perl

#!/usr/bin/perl
use Net::SMTP;
my @NAMELIST=(
 #list all name who can receive the information!
 'cybertech@cybertech.com',

 );
if ($#ARGV != 2)
{
 print "\nUsage: perl smtp_alert_email.pl [ChassisName] [Status] [ERROR_MESSAGE]\n";
 exit -1;
}
chomp($ARGV[0]);
chomp($ARGV[1]);
chomp($ARGV[2]);

my $subject = "Validation script $ARGV[1] on $ARGV[0]";
my $string = <Message:
$ARGV[2]
NOTE: This is an auto-generated email, please do not reply.

;

for ($i=0; $i < @NAMELIST; $i++)
{
    $smtp = Net::SMTP->new('ORCAS01.cybertech.com');
    $smtp->mail('cybertech@cybertech.com');
    $smtp->to($NAMELIST[$i]);
    $smtp->data();
    $smtp->datasend("From:XXXX\n");
    $smtp->datasend("To:$NAMELIST[$i]\n");
    $smtp->datasend("Subject: ALERT: $subject.\n");
    $smtp->datasend("\n");
    $smtp->datasend("$string\n");
    $smtp->dataend();
    $smtp->quit;
}

No comments: