3 # Copyright 2020 Theke Solutions
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use Scalar::Util qw( blessed );
24 use Try::Tiny qw( catch try );
26 use C4::Auth qw( get_template_and_user );
27 use C4::Output qw( output_html_with_http_headers );
30 use Koha::SMTP::Servers;
33 my $op = $input->param('op') || 'list';
35 my ( $template, $borrowernumber, $cookie ) = get_template_and_user(
36 { template_name => "admin/smtp_servers.tt",
39 flagsrequired => { parameters => 'manage_smtp_servers' },
45 my $smtp_servers = Koha::SMTP::Servers->search;
49 my $name = $input->param('smtp_name');
50 my $host = $input->param('smtp_host');
51 my $port = $input->param('smtp_port') || 25;
52 my $timeout = $input->param('smtp_timeout') || 120;
53 my $ssl_mode = $input->param('smtp_ssl_mode');
54 my $user_name = $input->param('smtp_user_name') || undef;
55 my $password = $input->param('smtp_password') || undef;
56 my $debug = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0;
57 my $is_default = ( scalar $input->param('smtp_default') ) ? 1 : 0;
61 Koha::SMTP::Server->new(
67 ssl_mode => $ssl_mode,
68 user_name => $user_name,
69 password => $password,
71 is_default => $is_default,
75 push @messages, { type => 'message', code => 'success_on_insert' };
78 if ( blessed $_ and $_->isa('Koha::Exceptions::Object::DuplicateID') ) {
82 code => 'error_on_insert',
83 reason => 'duplicate_id'
88 # list servers after adding
91 elsif ( $op eq 'edit_form' ) {
92 my $smtp_server_id = $input->param('smtp_server_id');
95 $smtp_server = Koha::SMTP::Servers->find($smtp_server_id)
96 unless !$smtp_server_id;
100 smtp_server => $smtp_server,
101 default_config => $smtp_servers->get_default,
108 code => 'error_on_edit',
109 reason => 'invalid_id'
113 elsif ( $op eq 'edit_save' ) {
115 my $smtp_server_id = $input->param('smtp_server_id');
118 $smtp_server = Koha::SMTP::Servers->find($smtp_server_id)
119 unless !$smtp_server_id;
121 if ( $smtp_server ) {
123 my $name = $input->param('smtp_name');
124 my $host = $input->param('smtp_host');
125 my $port = $input->param('smtp_port') || 25;
126 my $timeout = $input->param('smtp_timeout') || 120;
127 my $ssl_mode = $input->param('smtp_ssl_mode');
128 my $user_name = $input->param('smtp_user_name') || undef;
129 my $password = $input->param('smtp_password') || undef;
130 my $debug = ( scalar $input->param('smtp_debug_mode') ) ? 1 : 0;
131 my $is_default = ( scalar $input->param('smtp_default') ) ? 1 : 0;
135 $smtp_server->password( $password )
136 if defined $password and $password ne '****'
137 or not defined $password;
145 ssl_mode => $ssl_mode,
146 user_name => $user_name,
148 is_default => $is_default,
155 code => 'success_on_update'
162 code => 'error_on_update'
166 # list servers after adding
173 code => 'error_on_update',
174 reason => 'invalid_id'
179 if ( $op eq 'list' ) {
181 servers_count => $smtp_servers->count,
182 default_config => $smtp_servers->get_default,
188 messages => \@messages,
191 output_html_with_http_headers $input, $cookie, $template->output;