0

I need help figuring out where to call my send_mail (). Where I currently place it in the code, it sends out an email for every condition and each email it sends it adds another record as part of the set. I'm only interested in sending a single email with the collected records, the last email shown (msg4). Can I do this within the same loop? I'm not sure.

Example: (msg1)

Service: MST Engine  - Stopped - Manual

(msg2):

Service: MST Engine  - Stopped - Manual
Service: MST Logging   - Stopped - Manual

(msg3):

Service: MST Engine  - Stopped - Manual
Service: MST Logging   - Stopped - Manual
Service: MST Server  - Stopped - Manual

(msg4): (intersted in only this email)

Service: MST Engine  - Stopped - Manual
Service: MST Logging   - Stopped - Manual
Service: MST Server  - Stopped - Manual
Service: MST Formatter  - Stopped - Manual

Here is the main piece where I set the conditions: (I'm using Win32::OLE package has a method in(COLLECTION). So its not an array reference.)

foreach my $serv (in $servSet) 
{
    next if $serv->{started};
    my $sname  = $serv->{name};
    my $sstate = $serv->{started};
    my $ssmode = $serv->{startmode};
    $winsvcs .= "Service: $sname  - $servicestate[$sstate] - $ssmode\n";
    send_email();
 }

1 Answer 1

2

Move the send_email call out of the loop or it will call it every time it goes through the loop. I assume the function just sends the contents of $winsvcs.

my $winsvcs = '';
foreach my $serv (in $servSet) {
    next if $serv->{started};
    my $sname  = $serv->{name};
    my $sstate = $serv->{started};
    my $ssmode = $serv->{startmode};
    $winsvcs .= "Service: $sname  - $servicestate[$sstate] - $ssmode\n";

}
send_email();
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for clearing that up. its working fine now. What if I wanted to filter my output on only specific services? For example, only interested in Engine | Logging |Server. Would I use like a regex for this? thanks again.
You can filter it by added another "next" line. For example, if you wanted to filter out "Logging" you could add "next if $sname eq 'Logging';
after testing it throughout the day. what i'm seeing is if I have an empty set (basically no recs for $winsvcs, it sends the email with of course an empty/no mesg body. How do I change this so, if there is no body or $winsvcs is empty, don't call send_email? I don't want the email if there is nothing to report. thanks again.
if ( $winsvcs ) { send_email(); }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.