Engine/Alerting/Email.pm untested

filter: Statementstmt 0.0% 0 / 83 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 3 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 110 CC 7 SCAR 25.2
CC
7
Cov
0%
sendEmail
30.0
new
6.0
_send_email
6.0
CRAP 12.4
1 package Service::Engine::Alerting::Email;
2  
3 use 5.010;
4 use strict;
5 use warnings;
6 use Data::Dumper;
7 use Service::Engine;
8 use Service::Engine::Utility::Email;
9  
10 our $Config;
11 our $Log;
12 our $Alert;
13 our $EngineName;
14 our $EngineInstance;
15  
16 sub new {
17
18 my ($class,$options) = @_;
19
20 # set some defaults
21 my $attributes = {'method'=>''};
22
23 # load options
24 if (ref($options) eq 'HASH') {
25 foreach my $option (keys %{$options}) {
26 $attributes->{$option} = $options->{$option};
27 }
28 }
29
30 # pull in some Service::Engine globals
31 $Config = $Service::Engine::Config;
32 $Log = $Service::Engine::Log;
33 $EngineName = $Service::Engine::EngineName;
34 $EngineInstance = $Service::Engine::EngineInstance;
35
36 my $self = bless $attributes, $class;
37
38 return $self;
39  
40 }
41  
42 sub sendEmail {
43 my ($self, $args) = @_;
44
45 $Alert = $Service::Engine::Alert;
46
47 my $status = 1;
48
49 my $msg = $args->{'msg'};
50 my $options = $args->{'options'};
51 my $handle = $args->{'handle'};
52 my $contacts = $options->{'contacts'};
53 my $groups = $options->{'groups'};
54 my $recipients = $Alert->get_recipients({'groups'=>$groups,'contacts'=>$contacts});
55
56 my $handle_config = $Config->get_config('alerting')->{'modules'}->{'Email'}->{$handle};
57
58 if (ref($handle_config) ne 'HASH') {
59 $Log->log({'msg'=>"Missing $handle config " . Dumper($handle_config),'level'=>2});
60 return 0;
61 }
62
63 $args->{'from'} = $handle_config->{'from'};
64 $args->{'smtp_ip'} = $handle_config->{'smtp_ip'};
65 $args->{'smtp_port'} = $handle_config->{'smtp_port'};
66 $args->{'subject'} = "$EngineName" . "[$EngineInstance] ALERT: $msg";
67 $args->{'force_text'} = 1;
68
69 if (!$args->{'from'}) {
70 $Log->log({'msg'=>"Missing required account options to send Email " . Dumper($handle_config),'level'=>2});
71 return 0;
72 }
73
74 foreach my $recipient (keys %{$recipients}) {
75 if ($recipients->{$recipient}->{'email'}) {
76 $args->{'to'} = $recipients->{$recipient}->{'email'};
77 my $status = $self->_send_email($args);
78 $Log->log({'msg'=>"could not send mail to $args->{'to'}",'level'=>2}) unless $status;
79 }
80 }
81
82 return $status;
83 }
84  
85 sub _send_email {
86
87 my ($self, $args) = @_;
88
89 my $msg = $args->{'msg'};
90 my $to = $args->{'to'};
91
92 if (!$msg || !$to) {
93 $Log->log({'msg'=>"Missing required options to send email " . Dumper($args),'level'=>2});
94 return 0;
95 }
96
97 # send our email here
98 my $smtp = Service::Engine::Utility::Email->new();
99  
100 my $response = $smtp->send($args);
101  
102 return $response;
103  
104 }
105  
106 1;