From 01311c8414b2613fc68131cd6312e1a7ecce94bb 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 Note: New patch with rebased to main. 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. --- admin/smtp_servers.pl | 49 +++++++++++++++++++ .../prog/en/modules/admin/smtp_servers.tt | 13 ++++- 2 files changed, 61 insertions(+), 1 deletion(-) 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 bf4c1e371c..78b1f1ae05 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' %]
New SMTP server
@@ -435,6 +439,13 @@ encodeURIComponent(row.name.escapeHtml()) + '"> ' + _("Delete") + + "" + + "\n"; + result += + ' ' + + _("Send test email") + ""; return result; }, -- 2.39.5