]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Trigger/Reactor/SendEmail.pm
Lp 1801163: Switch to Email::MIME in SendEmail A/T Reactor
[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 sub handler {
43     my $self = shift;
44     my $env = shift;
45
46     my $conf = OpenSRF::Utils::SettingsClient->new;
47     my $smtp = $conf->config_value('email_notify', 'smtp_server');
48     $$env{default_sender} = $conf->config_value('email_notify', 'sender_address');
49
50     my $text = encode_utf8($self->run_TT($env));
51     return 0 if (!$text);
52
53     my $sender = Email::Send->new({mailer => 'SMTP'});
54     $sender->mailer_args([Host => $smtp]);
55
56     my $stat;
57     my $err;
58
59     my $email = Email::MIME->new($text);
60
61     # Handle the address fields.  In addition to encoding the values
62     # properly, we make sure there is only 1 each.
63     for my $hfield (qw/From To Bcc Cc Reply-To Sender/) {
64         my @headers = $email->header($hfield);
65         $email->header_str_set($hfield => join(',', @headers)) if ($headers[0]);
66     }
67
68     # Handle the Subject field.  Again, the standard says there can be
69     # only one.
70     my @headers = $email->header('Subject');
71     $email->header_str_set('Subject' => $headers[0]) if ($headers[0]);
72
73     $email->header_set('MIME-Version' => '1.0');
74     $email->header_set('Content-Type' => "text/plain; charset=UTF-8");
75     $email->header_set('Content-Transfer-Encoding' => '8bit');
76
77     try {
78         $stat = $sender->send($email);
79     } catch Error with {
80         $err = $stat = shift;
81         $logger->error("SendEmail Reactor: Email failed with error: $err");
82     };
83
84     if( !$err and $stat and $stat->type eq 'success' ) {
85         $logger->info("SendEmail Reactor: successfully sent email");
86         return 1;
87     } else {
88         $logger->warn("SendEmail Reactor: unable to send email: ".Dumper($stat));
89         $text =~ s/\n//og;
90         $logger->warn("SendEmail Reactor: failed email template: $text");
91         return 0;
92     }
93
94 }
95
96 1;
97