]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/SendEmail.pm
while we're at it, condense the js/css/images deflat and cache control into 1 block
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Trigger / Reactor / SendEmail.pm
1 package OpenILS::Application::Trigger::Reactor::SendEmail;
2 use strict; use warnings;
3 use Error qw/:try/;
4 use Data::Dumper;
5 use Email::Send;
6 use OpenSRF::Utils::SettingsClient;
7 use OpenILS::Application::Trigger::Reactor;
8 use OpenSRF::Utils::Logger qw/:logger/;
9 use utf8;
10 $Data::Dumper::Indent = 0;
11
12 use base 'OpenILS::Application::Trigger::Reactor';
13
14 my $log = 'OpenSRF::Utils::Logger';
15
16 sub ABOUT {
17     return <<ABOUT;
18
19 The SendEmail Reactor Module attempts to email out, via Email::Send,
20 whatever is constructed by the template passed in from the Event Definition.
21
22 The SMTP server specified by the /opensrf/default/email_notify/smtp_server
23 setting is used to send the email, and the value at
24 /opensrf/default/email_notify/sender_address is passed into the template as
25 the 'default_sender' variable.
26
27 No default template is assumed, and all information other than the
28 default_sender that the system provides is expected to be gathered by the
29 Event Definition through either Environment or Parameter definitions.
30
31 ABOUT
32 }
33
34 sub handler {
35     my $self = shift;
36     my $env = shift;
37
38     my $conf = OpenSRF::Utils::SettingsClient->new;
39     my $smtp = $conf->config_value('email_notify', 'smtp_server');
40     $$env{default_sender} = $conf->config_value('email_notify', 'sender_address');
41
42     my $text = $self->run_TT($env);
43     return 0 if (!$text);
44
45     my $sender = Email::Send->new({mailer => 'SMTP'});
46     $sender->mailer_args([Host => $smtp]);
47
48     my $stat;
49     my $err;
50
51     utf8::encode($text); # prevent "Wide character" errors in Email::Send
52
53     try {
54         $stat = $sender->send($text);
55     } catch Error with {
56         $err = $stat = shift;
57         $logger->error("SendEmail Reactor: Email failed with error: $err");
58     };
59
60     if( !$err and $stat and $stat->type eq 'success' ) {
61         $logger->info("SendEmail Reactor: successfully sent email");
62         return 1;
63     } else {
64         $logger->warn("SendEmail Reactor: unable to send email: ".Dumper($stat));
65         $text =~ s/\n//og;
66         $logger->warn("SendEmail Reactor: failed email template: $text");
67         return 0;
68     }
69
70 }
71
72 1;
73