2011-03-10 15:19:34 +00:00
|
|
|
package Explain::Plugin::MailSender;
|
|
|
|
|
|
|
|
use Mojo::Base 'Mojolicious::Plugin';
|
2019-05-06 12:41:07 +02:00
|
|
|
use English -no_match_vars;
|
|
|
|
use Email::Sender::Simple qw(sendmail);
|
|
|
|
use Email::Simple;
|
2011-03-10 15:19:34 +00:00
|
|
|
|
|
|
|
__PACKAGE__->attr( config => sub { {} } );
|
|
|
|
|
|
|
|
sub register {
|
|
|
|
my ( $self, $app, $config ) = @_;
|
|
|
|
|
|
|
|
# save settings
|
|
|
|
$self->config( $config );
|
|
|
|
|
|
|
|
# register helper
|
|
|
|
$app->helper(
|
|
|
|
send_mail => sub {
|
|
|
|
my ( $controller, $mail ) = @_;
|
|
|
|
|
2019-05-06 12:41:07 +02:00
|
|
|
eval {
|
|
|
|
my $email = Email::Simple->create(
|
|
|
|
header => [
|
|
|
|
To => $self->config->{'to'},
|
|
|
|
From => $self->config->{'from'},
|
|
|
|
Subject => $self->config->{'subject'},
|
|
|
|
],
|
|
|
|
body => $mail->{'msg'},
|
|
|
|
);
|
2011-03-10 15:19:34 +00:00
|
|
|
|
2019-05-06 12:41:07 +02:00
|
|
|
# log debug message
|
|
|
|
$controller->app->log->debug( sprintf "Sending mail:\n%s", $controller->dumper( $email ) );
|
2011-03-10 15:19:34 +00:00
|
|
|
|
2019-05-06 12:41:07 +02:00
|
|
|
sendmail($email);
|
|
|
|
};
|
2011-03-10 15:19:34 +00:00
|
|
|
|
2019-05-06 12:41:07 +02:00
|
|
|
if ( $EVAL_ERROR ) {
|
|
|
|
my $message = "Mail send failed, reason: " . $EVAL_ERROR;
|
2011-03-10 15:19:34 +00:00
|
|
|
$controller->app->log->fatal( $message );
|
|
|
|
die $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
1;
|