OSDN Git Service

implement sendmail helper
authorhylom <hylom@users.sourceforge.jp>
Fri, 26 Oct 2018 11:17:05 +0000 (20:17 +0900)
committerhylom <hylom@users.sourceforge.jp>
Fri, 26 Oct 2018 11:17:05 +0000 (20:17 +0900)
debian/control
src/newslash_web/lib/Newslash/Eventd.pm
src/newslash_web/lib/Newslash/Plugin/Sendmail.pm
src/newslash_web/lib/Newslash/Web.pm
test-container/Dockerfile

index 7737718..a9beae5 100644 (file)
@@ -9,7 +9,7 @@ Uploaders: Hiromichi MATSUSHIMA <hirom@osdn.jp>
 Package: newslash-core
 Architecture: any
 Depends:
- libmojolicious-perl, libjs-bootstrap, libdbd-mysql, libdbi-perl, libdbd-mysql-perl, libtemplate-perl, node-less, libcrypt-openssl-random-perl, libemail-valid-perl, tinymce, libredis-perl, libhtml-tidy-perl, liburi-perl, libdate-calc-perl, libdatetime-perl, liburi-perl, libjson-perl, libcache-memcached-fast-perl, libdata-javascript-anon-perl, libwww-perl, libyaml-perl, libdatetime-format-mysql-perl, libdatetime-format-iso8601-perl, libxml-feed-perl, libcarp-always-perl, libcrypt-eksblowfish-perl, ${shlibs:Depends}, ${misc:Depends}, ${perl:Depends}
+ libmojolicious-perl, libjs-bootstrap, libdbd-mysql, libdbi-perl, libdbd-mysql-perl, libtemplate-perl, node-less, libcrypt-openssl-random-perl, libemail-valid-perl, tinymce, libredis-perl, libhtml-tidy-perl, liburi-perl, libdate-calc-perl, libdatetime-perl, liburi-perl, libjson-perl, libcache-memcached-fast-perl, libdata-javascript-anon-perl, libwww-perl, libyaml-perl, libdatetime-format-mysql-perl, libdatetime-format-iso8601-perl, libxml-feed-perl, libcarp-always-perl, libcrypt-eksblowfish-perl, libemail-sender-perl, ${shlibs:Depends}, ${misc:Depends}, ${perl:Depends}
 Description: Newslash core files
   Newsslash is 'news lashing discussion platform'. This package include core files to use Newslash.
 
index bd6d2a3..3f876ff 100644 (file)
@@ -25,20 +25,14 @@ sub startup {
     # secret key for hasing
     $app->secrets([$app->config->{System}->{secret_key},]);
 
-    # stash for plugins
-    $app->config->{_Plugins} = {};
-
-    # use BasicAuth?
-    if ($app->config->{BasicAuth} && $app->config->{BasicAuth}->{enable}) {
-        $app->plugin('Newslash::Plugin::BasicAuth');
-    }
+    # support BasicAuth
+    $app->plugin('Newslash::Plugin::BasicAuth');
 
     # use TimeLimitedCache ($app->cache)
     $app->plugin('Newslash::Plugin::TimeLimitedCache');
 
     # use KeyValue Store ($app->kvs)
-    my $kvs_opts = $app->config->{KeyValueStore};
-    $app->plugin('Newslash::Plugin::KeyValueStore', options => $kvs_opts);
+    $app->plugin('Newslash::Plugin::KeyValueStore');
 
     # add Model Loader
     $app->plugin('Newslash::Plugin::Model');
@@ -46,6 +40,9 @@ sub startup {
     # use Template::Toolkit 2 render
     $app->plugin('Newslash::Plugin::TT2Renderer');
 
+    # Sendmail helper
+    $app->plugin('Newslash::Plugin::Sendmail');
+
     # Router
     my $r = $app->routes;
 
index 864b118..d83848e 100644 (file)
@@ -1,6 +1,10 @@
 package Newslash::Plugin::Sendmail;
 use Mojo::Base 'Mojolicious::Plugin';
 use Email::Valid;
+use Email::Simple;
+use Email::Sender::Simple;
+use Email::Sender::Transport::SMTP;
+use Encode;
 
 has 'last_error';
 has 'app';
@@ -8,10 +12,51 @@ has 'app';
 sub register {
     my ($self, $app, $conf) = @_;
     $self->app($app);
-    $app->helper(sendmail => sub { $self->sendmail(@_); });
+    $app->helper(sendmail => sub { shift; $self->sendmail(@_); });
 
     # default config values
-    #my $cnf = $app->config->{Sendmail} ||= {};
+    my $cnf = $app->config->{Sendmail} ||= {};
+
+    $cnf->{default_from} ||= 'admin@example.com';
+    $cnf->{smtp_host} ||= 'example.com';
+    $cnf->{smtp_port} ||= '25';
+}
+
+sub _send {
+    my ($self, $from, $to, $title, $body) = @_;
+
+    # create header and body
+    my $header = [
+                  From    => encode('MIME-Header-ISO_2022_JP', $from),
+                  To      => encode('MIME-Header-ISO_2022_JP', $to),
+                  Subject => encode('MIME-Header-ISO_2022_JP', $title),
+                  'Content-Type' => 'text/plain; charset=ISO-2022-JP',
+                  'Content-Transfer-Encoding' => '7bit',
+                 ];
+
+    my $encoded_body = encode('iso-2022-jp', $body);
+
+    # create message
+    my $email = Email::Simple->create(header => $header,
+                                      body => $encoded_body);
+
+    # create transport
+    my $smtp_host = $self->app->config->{Sendmail}->{smtp_host};
+    my $smtp_port = $self->app->config->{Sendmail}->{smtp_port};
+    my $smtp_timeout = $self->app->config->{Sendmail}->{smtp_timeout};
+    my $transport = Email::Sender::Transport::SMTP->new({ host => $smtp_host,
+                                                          port => $smtp_port,
+                                                          timeout => $smtp_timeout,
+                                                        });
+
+    # send message
+    my $rs = eval { Email::Sender::Simple->send($email, {transport => $transport}) };
+    if (!$rs) {
+        my ($msg, $code) = ($@->message, $@->code);
+        $self->app->log->error("Sendmail: sendmail error. $msg ($code)");
+        return;
+    }
+    return 1;
 }
 
 sub _render {
@@ -43,15 +88,18 @@ sub sendmail {
         $recipient_user = $args->{to};
     }
     elsif (ref($args->{to}) eq "ARRAY") {
-        # 複数ターゲットに送信
+        # TODO
     }
     elsif (ref($args->{to}) eq "") {
         if ($args->{to} =~ m/^\d+$/) {
             # uid is given
             $recipient_user = $self->app->model('users')->select(uid => $args->{to});
         }
-        elsif (Email::Valid->address($args->{to})) {
-            $to_address = $args->{to};
+        else {
+            my $encoded_to = encode('MIME-Header-ISO_2022_JP', $args->{to});
+            if (Email::Valid->address($encoded_to)) {
+                $to_address = $args->{to};
+            }
         }
     }
 
@@ -64,18 +112,41 @@ sub sendmail {
         return;
     }
 
+
+    # check from address
+    my $from_address = $args->{from} || $self->app->config->{Sendmail}->{default_from};
+
+    # create title and body
+    my $title = $args->{title};
+    my $body = $args->{body};
+
     if ($args->{template}) {
         # render template
-        my ($title, $body) = eval { $self->_render($args->{template},
+        ($title, $body) = eval { $self->_render($args->{template},
                                                    $args->{params}); };
         if (!$title) {
             $self->app->log->error("Sendmail: template error: $@");
             return;
         }
+    }
+
+    if (!$title) {
+        $self->app->log->error("Sendmail: no title given. to: $to_address");
+        return;
+    }
 
+    if (!$body) {
+        $self->app->log->error("Sendmail: no body given. title: $title, to: $to_address");
+        return;
     }
 
     # send
+    my $rs = $self->_send($from_address, $to_address, $title, $body);
+
+    if (!$rs) {
+        $self->app->log->error("Sendmail: fail to send mail. title: $title, to: $to_address");
+        return;
+    }
 
     return 1;
 }
index 3bd1a39..70faa23 100644 (file)
@@ -217,6 +217,9 @@ sub startup {
     # Users helper
     $app->plugin('Newslash::Plugin::Users');
 
+    # Sendmail helper
+    $app->plugin('Newslash::Plugin::Sendmail');
+
     ############################################################
     #
     # Routing Settings
index d7ff349..a675391 100644 (file)
@@ -12,7 +12,8 @@ RUN apt-get install -y libmojolicious-perl libjs-bootstrap libdbd-mysql libdbi-p
     libdbd-mysql-perl libtemplate-perl node-less libcrypt-openssl-random-perl \
     libemail-valid-perl tinymce libredis-perl libhtml-tidy-perl liburi-perl \
     libyaml-perl libdatetime-format-mysql-perl libdatetime-format-iso8601-perl \
-    libxml-feed-perl libcarp-always-perl libcrypt-eksblowfish-perl
+    libxml-feed-perl libcarp-always-perl libcrypt-eksblowfish-perl \
+    libemail-sender-perl
 
 # install slash's dependency
 RUN apt-get install -y libdate-calc-perl libdatetime-perl liburi-perl libjson-perl \