]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/SendEmail.pm
LP#1749475: (follow-up) fix SendEmail's preview check
[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::MIME;
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 The From, To, Bcc, Cc, Reply-To, Sender, and Subject fields are
33 automatically MIME-encoded.
34
35 No default template is assumed, and all information other than the
36 default_sender that the system provides is expected to be gathered by the
37 Event Definition through either Environment or Parameter definitions.
38
39 ABOUT
40 }
41
42 # Utility method to interrogate the user data to see if a preview
43 # has been requested. As noted in the discussion in LP#1749475, there
44 # are likely alternative designs that would be better, but this does
45 # the job for the moment.
46 sub _is_preview {
47     my $user_data = shift;
48
49     return 0 unless $user_data;
50     return 1 if ref($user_data) =~ /HASH/ && $user_data->{preview};
51     return 1 if ref($user_data) =~ /ARRAY/ &&
52                 @$user_data > 0 &&
53                 ref($user_data->[0]) =~ /HASH/ &&
54                 $user_data->[0]->{preview};
55     return 0;
56 }
57
58 sub handler {
59     my $self = shift;
60     my $env = shift;
61
62     my $conf = OpenSRF::Utils::SettingsClient->new;
63     my $smtp = $conf->config_value('email_notify', 'smtp_server');
64     $$env{default_sender} = $conf->config_value('email_notify', 'sender_address');
65
66     my $text = encode_utf8($self->run_TT($env));
67     return 0 if (!$text);
68     if (_is_preview($$env{user_data})) {
69         $logger->info("SendEmail Reactor: success in preview mode, not sending email");
70         return 1;
71     }
72
73     my $sender = Email::Send->new({mailer => 'SMTP'});
74     $sender->mailer_args([Host => $smtp]);
75
76     my $stat;
77     my $err;
78
79     my $email = Email::MIME->new($text);
80
81     # Handle the address fields.  In addition to encoding the values
82     # properly, we make sure there is only 1 each.
83     for my $hfield (qw/From To Bcc Cc Reply-To Sender/) {
84         my @headers = $email->header($hfield);
85         $email->header_str_set($hfield => decode_utf8(join(',', @headers))) if ($headers[0]);
86     }
87
88     # Handle the Subject field.  Again, the standard says there can be
89     # only one.
90     my @headers = $email->header('Subject');
91     $email->header_str_set('Subject' => decode_utf8($headers[0])) if ($headers[0]);
92
93     $email->header_set('MIME-Version' => '1.0');
94     $email->header_set('Content-Type' => "text/plain; charset=UTF-8");
95     $email->header_set('Content-Transfer-Encoding' => '8bit');
96
97     try {
98         $stat = $sender->send($email);
99     } catch Error with {
100         $err = $stat = shift;
101         $logger->error("SendEmail Reactor: Email failed with error: $err");
102     };
103
104     if( !$err and $stat and $stat->type eq 'success' ) {
105         $logger->info("SendEmail Reactor: successfully sent email");
106         return 1;
107     } else {
108         $logger->warn("SendEmail Reactor: unable to send email: ".Dumper($stat));
109         $text =~ s/\n//og;
110         $logger->warn("SendEmail Reactor: failed email template: $text");
111         return 0;
112     }
113
114 }
115
116 1;
117