]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/SendEmail.pm
Merge branch 'master' of git.evergreen-ils.org:Evergreen-DocBook into doc_consolidati...
[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 Email::Simple;
7 use OpenSRF::Utils::SettingsClient;
8 use OpenILS::Application::Trigger::Reactor;
9 use OpenSRF::Utils::Logger qw/:logger/;
10 use Encode;
11 $Data::Dumper::Indent = 0;
12
13 use base 'OpenILS::Application::Trigger::Reactor';
14
15 my $log = 'OpenSRF::Utils::Logger';
16
17 sub ABOUT {
18     return <<ABOUT;
19
20 The SendEmail Reactor Module attempts to email out, via Email::Send,
21 whatever is constructed by the template passed in from the Event Definition.
22
23 The SMTP server specified by the /opensrf/default/email_notify/smtp_server
24 setting is used to send the email, and the value at
25 /opensrf/default/email_notify/sender_address is passed into the template as
26 the 'default_sender' variable.
27
28 Email is encoded in UTF-8 and the corresponding MIME-Version, Content-Type,
29 and Content-Transfer-Encoding headers are set to help mail user agents
30 decode the content.
31
32 No default template is assumed, and all information other than the
33 default_sender that the system provides is expected to be gathered by the
34 Event Definition through either Environment or Parameter definitions.
35
36 ABOUT
37 }
38
39 sub handler {
40     my $self = shift;
41     my $env = shift;
42
43     my $conf = OpenSRF::Utils::SettingsClient->new;
44     my $smtp = $conf->config_value('email_notify', 'smtp_server');
45     $$env{default_sender} = $conf->config_value('email_notify', 'sender_address');
46
47     my $text = encode_utf8($self->run_TT($env));
48     return 0 if (!$text);
49
50     my $sender = Email::Send->new({mailer => 'SMTP'});
51     $sender->mailer_args([Host => $smtp]);
52
53     my $stat;
54     my $err;
55
56     my $email = Email::Simple->new($text);
57     $email->header_set('MIME-Version' => '1.0');
58     $email->header_set('Content-Type' => "text/plain; charset=UTF-8");
59     $email->header_set('Content-Transfer-Encoding' => '8bit');
60
61     try {
62         $stat = $sender->send($email);
63     } catch Error with {
64         $err = $stat = shift;
65         $logger->error("SendEmail Reactor: Email failed with error: $err");
66     };
67
68     if( !$err and $stat and $stat->type eq 'success' ) {
69         $logger->info("SendEmail Reactor: successfully sent email");
70         return 1;
71     } else {
72         $logger->warn("SendEmail Reactor: unable to send email: ".Dumper($stat));
73         $text =~ s/\n//og;
74         $logger->warn("SendEmail Reactor: failed email template: $text");
75         return 0;
76     }
77
78 }
79
80 1;
81