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.
# 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');
# use Template::Toolkit 2 render
$app->plugin('Newslash::Plugin::TT2Renderer');
+ # Sendmail helper
+ $app->plugin('Newslash::Plugin::Sendmail');
+
# Router
my $r = $app->routes;
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';
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 {
$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};
+ }
}
}
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;
}