in reply to Re: Sending Email to a list of people using Mail::Sender
in thread Sending Email to a list of people using Mail::Sender

OK.. So I cleaned up a few things from the recommendations that some of you made. (thanks) I then got FunkyMonk's Message and I must say that I did read the documentation on Mail::Sender (I had to since I am new to perl and needed to know how it initially worked) I guess the real problem is that I wasn't 100% sure what they meant by "This parameter may be either a comma separated list of email addresses or a reference to a list of addresses." Initially, I had created a list of comma separated addresses(within the script) and it worked. When I moved that list to the new file, it didn't work. I assume that this is where the "reference to a list of addresses" comes in at. Perhaps Someone can give me the right syntax since I still cannot get it to work. (for the push part). Here is my "real" code that I have modified so far. Since my script is over 1000 lines therefore I am only posting a portion of it and it's very likely that I may be missing stuff. This is not my final code, this is my code in progress. ( so commented out stuff that i tried is also in there too ) Either way, the code works when within the script, it just doesn't work outside of it. Here it is:

The options file:

# Configuration File for Script # Set Alarm Thresholds by count of reoccurance based on 5 minutes cron + job. LowAlarmThreshold = 3 MediumAlarmThreshold = 2 HighAlarmThreshold = 1 # Set the Email Options # Email addresses are in comma separated values # To =,someotheremailaddress@ To = To = # To = From = SMTP_Server = # Set the Hour at which daily log rotation will happen (accepted range + is 0 - 23) LogRotateHour = 0

The Script File:

#!/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 (<MCONFIGFILE>){ # 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 (<CONFIGFILE>) { 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 = ','; # $To = ''; # @Message = <TEMPEMAILMESSAGEFILE>; $Sender = new Mail::Sender({ smtp => '', 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: $TempEMailMe +ssageFile"); } 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 = <TEMPEMAILMESSAGEFILE>; 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; }

So I beleive FunkyMonk is on the right track to what I am looking for. Thanks.

Replies are listed 'Best First'.
Re^3: Sending Email to a list of people using Mail::Sender
by wfsp (Abbot) on Aug 14, 2011 at 15:45 UTC
    I think what I would do is first write a small test script to see if the config file was being read correctly. Something like (using your config file)
    #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my $cnf_file = q{config.txt}; my %cnf = read_config($cnf_file); print Dumper \%cnf; sub read_config { my $file = shift; open my $fh, q{<}, $file or die qq{cant open *$file* to read: $!}; my %cnf; while (my $line = <$fh>){ chomp $line; next unless $line; next if $line =~ /^#/; my ($key, $value) = split /\s+=\s+/, $line; if ($key eq q{To}){ push @{$cnf{$key}}, $value; } else{ $cnf{$key} = $value; } } return %cnf; }
    $VAR1 = { 'MediumAlarmThreshold' => '2', 'SMTP_Server' => '', 'LowAlarmThreshold' => '3', 'LogRotateHour' => '0', 'To' => [ '', '' ], 'HighAlarmThreshold' => '1', 'From' => '' };
    As you can see, the To key holds an array ref. Which is, apparently, what Mail::Sender requires.

    If the output is indeed as you expect I would consider a second test script which just sends a dummy email to those email addresses.

    This approach has many advantages. You take on one problem at a time (which is my top limit :-) and you end up with a collection of subs you are confident with. Also, if you hit a particular snag with one of the subs you have a simple script that you can post here and which monks can download and run. This approach, in my experience, will result in a lot more help and solutions.

    Try the script above. If it appears ok, write your test_send_email and see if that's ok.

    If there are still snags, you know where we live. :-)