#!/usr/bin/perl use warnings; use Mail::Sender; &ReadConfig; &ActuallySendEmail; sub ReadConfig { $ConfigFile = "/srv/script/etc/ConfigFile.conf"; # open(CONFIGFILE, "<", $ConfigFile) or die "ERROR: Unable to open $ConfigFile for reading"; # open $CONFIGFILE, '<', $ConfigFile or die "ERROR: Unable to open '$ConfigFile' for reading $!"; #use feature qw(say); # while (){ # next if !~ m/^To\s*=\s*(.*)/; # if $Line =~ m/^To\s*=\s*(.*)/; # say "To = <$1>\n"; # } if (!open(CONFIGFILE,$ConfigFile)) { print "ERROR: Unable to open Configuration File: $ConfigFile"; print "Fix the filepath or the file itself"; exit(); } @To = (); foreach $Line () { next if ($Line =~ m/^#/); # Get Alarm Thresholds Values if ($Line =~ m/LowAlarmThreshold\s*=\s*(.*)/) { $LowAlarmThreshold = $1; print "Low Alarm Threshold = <$LowAlarmThreshold>\n"; } if ($Line =~ m/MediumAlarmThreshold\s*=\s*(.*)/) { $MediumAlarmThreshold = $1; print "Medium Alarm Threshold = <$MediumAlarmThreshold>\n"; } if ($Line =~ m/HighAlarmThreshold\s*=\s*(.*)/) { $HighAlarmThreshold = $1; print "High Alarm Threshold = <$HighAlarmThreshold>\n"; } # Get Email Attributes Values if ($Line =~ m/To\s*=\s*(.*)/) { $To = $1; push(@To,$To); print "To = <@To>\n"; } if ($Line =~ m/From\s*=\s*(.*)/) { $From = $1; print "From = <$From>\n"; } if ($Line =~ m/SMTP_Server\s*=\s*(.*)/) { $SMTP_Server = $1; print "SMTP Server = <$SMTP_Server>\n"; } # Get LogRotate Hour Value if ($Line =~ m/LogRotateHour\s*=\s*(.*)/) { $LogRotateHour = $1; print "Log Rotate Hour = <$LogRotateHour>\n"; } } close(CONFIGFILE); } sub ActuallySendEMail { ($Flag) = @_; close(TEMPEMAILMESSAGEFILE); $TempEMailMessageFile =~ s/^>//; # $To = 'someemail@yahoo.com,exampleemailaddress@yahoo.ca'; # $To = 'exampleemailaddress@yahoo.ca'; # @Message = ; $Sender = new Mail::Sender({ smtp => 'smtp.isp.com', from => "\'$From\'", }); if ($Flag eq "TempEmailMessageFileOpenError") { $Subject = 'Script - Temporary email message open error'; } elsif ($Flag eq "TempEmailMessageFileLockError") { $Subject = 'Script - Temporary email message lock error'; } elsif ($Flag eq "Recovered") { $Subject = 'Script - has recovered from one or more errors'; } else { $Subject = 'Script encountered errors..'; }; print "to <@To>\n"; $Sender->OpenMultipart({ to => \@To, subject => "$Subject", }); $Sender->Body; $Sender->SendLineEnc("Script has encountered errors:"); $Sender->SendLineEnc("-----------------------------------\n"); if ($Flag eq "TempEmailMessageFileOpenError") { $Sender->SendLineEnc("ERROR: Unable to open file for writing: $TempEMailMessageFile"); } elsif ($Flag eq "TempEmailMessageFileLockError") { $Sender->SendLineEnc("ERROR: Unable to lock file: $TempEMailMessageFile"); } elsif (!open(TEMPEMAILMESSAGEFILE,$TempEMailMessageFile)) { print "\nERROR: Unable to open temporary email message file: $TempEMailMessageFile\n"; &PrintLog("ERROR: Unable to open temporary email message file: $TempEMailMessageFile"); return $FAILURE; } else { @Message = ; print "Message = <@Message>\n"; $Sender->SendLineEnc(@Message); close(TEMPEMAILMESSAGEFILE); }; if (($Flag eq "")&&(-e $PDFile)) { $Sender->Attach({ description => 'PDF file', ctype => 'application/pdf', encoding => 'Base64', disposition => attachment; filename="File.pdf"; type="PDF"', file => "$PDFFile", }); } $Sender->Close(); return $SUCCESS; }