From 40806a2e72ece1166d3ca4b7e39bbb084de46ee1 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Wed, 5 Aug 2020 16:22:32 -0300 Subject: [PATCH] Bug 22343: Add ->transport method This patch adds a Koha::SMTP::Server->transport method that returns an Email::Sender::Transport::SMTP object generated with the current object's settings. Special case is handled for disabled SSL. This is all covered by the introduced tests. 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/SMTP/Server.t => SUCCESS: Tests pass! Signed-off-by: Martin Renvoize --- Koha/SMTP/Server.pm | 38 +++++++++++++++++++++++++++++++ cpanfile | 1 + t/db_dependent/Koha/SMTP/Server.t | 29 ++++++++++++++++++++++- 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/Koha/SMTP/Server.pm b/Koha/SMTP/Server.pm index 5a078941c1..874c6624d1 100644 --- a/Koha/SMTP/Server.pm +++ b/Koha/SMTP/Server.pm @@ -21,6 +21,8 @@ use Koha::Database; use Koha::Exceptions::Object; use Koha::SMTP::Servers; +use Email::Sender::Transport::SMTP; + use base qw(Koha::Object); =head1 NAME @@ -100,6 +102,42 @@ sub library { return Koha::Library->_new_from_dbic($self->_result->library); } +=head3 transport + + my $transport = $smtp_server->transport; + sendmail( $message, { transport => $transport } ); + +Returns an I object that can be used directly +with Email::Sender. + +=cut + +sub transport { + my ($self) = @_; + + my $params = { + host => $self->host, + port => $self->port, + }; + + $params->{ssl} = $self->ssl_mode + unless $self->ssl_mode eq 'disabled'; + + $params->{timeout} = $self->timeout + if $self->timeout; + + $params->{sasl_username} = $self->user_name + if $self->user_name; + + $params->{sasl_password} = $self->password + if $self->password; + + + my $transport = Email::Sender::Transport::SMTP->new( $params ); + + return $transport; +} + =head3 to_api_mapping This method returns the mapping for representing a Koha::SMTP::Server object diff --git a/cpanfile b/cpanfile index 7e5eeaa1d8..05ae2aa495 100644 --- a/cpanfile +++ b/cpanfile @@ -36,6 +36,7 @@ requires 'Digest::MD5', '2.36'; requires 'Digest::SHA', '5.43'; requires 'Email::Date', '1.103'; requires 'Email::MessageID', '1.406'; +requires 'Email::Sender', '1.300030'; requires 'Email::Valid', '0.190'; requires 'Exception::Class', '1.38'; requires 'File::Slurp', '9999.13'; diff --git a/t/db_dependent/Koha/SMTP/Server.t b/t/db_dependent/Koha/SMTP/Server.t index db7b1f65af..9b8beb9a06 100644 --- a/t/db_dependent/Koha/SMTP/Server.t +++ b/t/db_dependent/Koha/SMTP/Server.t @@ -17,7 +17,7 @@ use Modern::Perl; -use Test::More tests => 2; +use Test::More tests => 3; use Test::Exception; use Test::Warn; @@ -118,3 +118,30 @@ subtest 'store() tests' => sub { $schema->storage->txn_rollback; }; + +subtest 'transport() tests' => sub { + + plan tests => 4; + + $schema->storage->txn_begin; + + my $server = $builder->build_object( + { + class => 'Koha::SMTP::Servers', + value => { ssl_mode => 'disabled' } + } + ); + + my $transport = $server->transport; + + is( ref($transport), 'Email::Sender::Transport::SMTP', 'Type is correct' ); + is( $transport->ssl, 0, 'SSL is not set' ); + + $server->set({ ssl_mode => 'ssl' })->store; + $transport = $server->transport; + + is( ref($transport), 'Email::Sender::Transport::SMTP', 'Type is correct' ); + is( $transport->ssl, 'ssl', 'SSL is set' ); + + $schema->storage->txn_rollback; +}; -- 2.20.1