From 40a72f5c7f6d8e9ca746eed870239ef810915efb Mon Sep 17 00:00:00 2001 From: Saiful Amin Date: Mon, 24 Nov 2025 09:22:47 +0000 Subject: [PATCH] Bug 29587: Test mail option in SMTP servers MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This patch adds a "Send test email" action to the SMTP servers administration page. It sends a test message to the address in the KohaAdminEmailAddress system preference and reports success or failure to the user. Test plan: 1. Add a valid SMTP server in Administration → SMTP servers. 2. Set KohaAdminEmailAddress to a valid email address. 3. Apply the patch. 4. Click "Send test email" in the Actions column. 5. Confirm that Koha reports "Test email sent successfully". 6. Edit the SMTP server and change the domain to an invalid one. 7. Click "Send test email" again. 8. Confirm that Koha reports failure. 9. Sign off. Signed-off-by: David Nind --- admin/smtp_servers.pl | 49 +++++++++++++++++++ .../prog/en/modules/admin/smtp_servers.tt | 9 +++- 2 files changed, 56 insertions(+), 2 deletions(-) diff --git a/admin/smtp_servers.pl b/admin/smtp_servers.pl index c01290f227..0194a87912 100755 --- a/admin/smtp_servers.pl +++ b/admin/smtp_servers.pl @@ -107,6 +107,55 @@ if ( $op eq 'cud-add' ) { reason => 'invalid_id' }; } +} elsif ( $op eq 'test_email' ) { + my $smtp_server_id = $input->param('smtp_server_id'); + my $smtp_server; + + $smtp_server = Koha::SMTP::Servers->find($smtp_server_id) + unless !$smtp_server_id; + + if ($smtp_server) { + my $test_email_address = C4::Context->preference('KohaAdminEmailAddress'); + my $smtp_transport = $smtp_server->transport; + + my $params = { + to => $test_email_address, + from => $test_email_address, + sender => $test_email_address, + subject => "Koha SMTP Test", + text_body => "Email is working." + }; + + my $email = Koha::Email->create($params); + + try { + $email->send_or_die( { transport => $smtp_transport } ); + push @messages, + { + type => 'message', + code => 'success_on_test' + }; + } catch { + push @messages, + { + type => 'alert', + code => 'error_on_test' + }; + }; + + $template->param( + servers_count => $smtp_servers->count, + default_config => $smtp_servers->get_default, + ); + + } else { + push @messages, + { + type => 'alert', + code => 'error_on_test', + reason => 'invalid_id' + }; + } } elsif ( $op eq 'cud-edit_save' ) { my $smtp_server_id = $input->param('smtp_server_id'); diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smtp_servers.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smtp_servers.tt index 9e6e742f92..f00fdab5a1 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smtp_servers.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/smtp_servers.tt @@ -60,10 +60,14 @@ [% SWITCH m.code %] [% CASE 'error_on_update' %] An error occurred trying to open the server for editing. The passed ID is invalid. + [% CASE 'error_on_test' %] + Failed to send test email. [% CASE 'error_on_insert' %] An error occurred when adding the server. The library already has an SMTP server set. [% CASE 'success_on_update' %] Server updated successfully. + [% CASE 'success_on_test' %] + Test email sent successfully to KohaAdminEmailAddress. [% CASE 'success_on_insert' %] Server added successfully. [% CASE %] @@ -241,7 +245,7 @@ [% END %] - [% IF op == 'list' %] + [% IF op == 'list' || op == 'test_email' %] @@ -421,7 +425,8 @@ { "data": function( row, type, val, meta ) { var result = ' '+_("Edit")+''+"\n"; - result += ' '+_("Delete")+''; + result += ' '+_("Delete")+''+"\n"; + result += ' '+_("Send test email")+''; return result; }, "searchable": false, -- 2.39.5