start of a generic static email test reactor
authormiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 25 Feb 2009 03:23:05 +0000 (03:23 +0000)
committermiker <miker@dcc99617-32d9-48b4-a31d-7c20da2025e4>
Wed, 25 Feb 2009 03:23:05 +0000 (03:23 +0000)
git-svn-id: svn://svn.open-ils.org/ILS/trunk@12293 dcc99617-32d9-48b4-a31d-7c20da2025e4

Open-ILS/src/perlmods/OpenILS/Application/Trigger/Event.pm
Open-ILS/src/perlmods/OpenILS/Application/Trigger/Reactor/StaticEmail.pm [new file with mode: 0644]

index aee5cb3..580bff4 100644 (file)
@@ -293,6 +293,8 @@ sub update_state {
     my $state = shift;
     return undef unless ($state);
 
+    my $fields = shift;
+
     if ($self->standalone) {
         $self->editor->xact_begin || return undef;
     }
@@ -303,6 +305,10 @@ sub update_state {
         return undef;
     }
 
+    if ($fields && ref($fields)) {
+        $e->$_($$fields{$_}) for (keys %$fields);
+    }
+
     $log->info( "Retrieved object ".$self->id." for update" );
     $e->start_time( 'now' ) unless $e->start_time;
     $e->update_time( 'now' );
diff --git a/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Reactor/StaticEmail.pm b/Open-ILS/src/perlmods/OpenILS/Application/Trigger/Reactor/StaticEmail.pm
new file mode 100644 (file)
index 0000000..634dc70
--- /dev/null
@@ -0,0 +1,61 @@
+package OpenILS::Application::Trigger::Reactor::StaticEmail;
+use Email::Send;
+use OpenSRF::Utils::SettingsClient;
+use OpenILS::Application::Trigger::Reactor;
+use OpenSRF::Utils::Logger qw/:level/;
+
+use base 'OpenILS::Application::Trigger::Reactor';
+
+my $log = 'OpenSRF::Utils::Logger';
+
+my $default_template = <<TT;
+To: [%- env.params.recipient -%]
+From: [%- env.params.sender -%]
+Subject: [%- env.params.subject -%]
+
+[% env.params.body %]
+TT
+
+sub handler {
+    my $self = shift;
+    my $env = shift;
+
+    my $conf = OpenSRF::Utils::SettingsClient->new;
+    my $smtp = $conf->config_value('email_notify', 'smtp_server');
+    $$env{params}{sender} ||= $conf->config_value('email_notify', 'sender_address');
+    $$env{params}{subject} ||= 'Test subject -- StaticEmail Reactor';
+    $$env{params}{body} ||= 'Test body -- StaticEmail Reactor';
+
+    $$env{params}{recipient} or return 0;
+
+    $logger->info("StaticEmail Reactor: sending email to ".
+        $$env{params}{recipient}." via SMTP server $smtp");
+
+    my $sender = Email::Send->new({mailer => 'SMTP'});
+    $sender->mailer_args([Host => $smtp]);
+
+    my $TT = $$env{template} || $default_template;
+    my $text = ''; # XXX TemplateToolkit stuff goes here...
+
+    my $stat;
+    my $err;
+
+    try {
+        $stat = $sender->send($text);
+    } catch Error with {
+        $err = $stat = shift;
+        $logger->error("StaticEmail Reactor: Email failed with error: $err");
+    };
+
+    if( !$err and $stat and $stat->type eq 'success' ) {
+        $logger->info("StaticEmail Reactor: successfully sent email");
+        return 1;
+    } else {
+        $logger->warn("StaticEmail Reactor: unable to send email: ".Dumper($stat));
+        return 0;
+    }
+
+}
+
+1;
+