1 package OpenILS::Application::Trigger::Reactor::SendEmail;
2 use strict; use warnings;
7 use OpenSRF::Utils::SettingsClient;
8 use OpenILS::Application::Trigger::Reactor;
9 use OpenSRF::Utils::Logger qw/:logger/;
11 $Data::Dumper::Indent = 0;
13 use base 'OpenILS::Application::Trigger::Reactor';
15 my $log = 'OpenSRF::Utils::Logger';
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.
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.
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
32 The From, To, Bcc, Cc, Reply-To, Sender, and Subject fields are
33 automatically MIME-encoded.
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.
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');
50 my $text = encode_utf8($self->run_TT($env));
52 if ($$env{user_data} && ref($$env{user_data}) =~ /HASH/ && $$env{user_data}{preview}) {
53 $logger->info("SendEmail Reactor: success in preview mode, not sending email");
57 my $sender = Email::Send->new({mailer => 'SMTP'});
58 $sender->mailer_args([Host => $smtp]);
63 my $email = Email::MIME->new($text);
65 # Handle the address fields. In addition to encoding the values
66 # properly, we make sure there is only 1 each.
67 for my $hfield (qw/From To Bcc Cc Reply-To Sender/) {
68 my @headers = $email->header($hfield);
69 $email->header_str_set($hfield => decode_utf8(join(',', @headers))) if ($headers[0]);
72 # Handle the Subject field. Again, the standard says there can be
74 my @headers = $email->header('Subject');
75 $email->header_str_set('Subject' => decode_utf8($headers[0])) if ($headers[0]);
77 $email->header_set('MIME-Version' => '1.0');
78 $email->header_set('Content-Type' => "text/plain; charset=UTF-8");
79 $email->header_set('Content-Transfer-Encoding' => '8bit');
82 $stat = $sender->send($email);
85 $logger->error("SendEmail Reactor: Email failed with error: $err");
88 if( !$err and $stat and $stat->type eq 'success' ) {
89 $logger->info("SendEmail Reactor: successfully sent email");
92 $logger->warn("SendEmail Reactor: unable to send email: ".Dumper($stat));
94 $logger->warn("SendEmail Reactor: failed email template: $text");