Bug 29587: Test mail option in SMTP servers

Edit: Added CSRF protection.

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: Baptiste Wojtkowski <baptiste.wojtkowski@biblibre.com>
Signed-off-by: David Nind <david@davidnind.com>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Saiful Amin 2025-11-24 09:22:47 +00:00 committed by Lucas Gass
parent b9d6d16f8e
commit 10fc3f3c64
No known key found for this signature in database
GPG Key ID: 60B447398B0D36CA
2 changed files with 56 additions and 0 deletions

View File

@ -107,6 +107,51 @@ if ( $op eq 'cud-add' ) {
reason => 'invalid_id'
};
}
} elsif ( $op eq 'cud-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'
};
};
} else {
push @messages,
{
type => 'alert',
code => 'error_on_test',
reason => 'invalid_id'
};
}
$op = 'list';
} elsif ( $op eq 'cud-edit_save' ) {
my $smtp_server_id = $input->param('smtp_server_id');

View File

@ -60,10 +60,14 @@
[% SWITCH m.code %]
[% CASE 'error_on_update' %]
<span>An error occurred trying to open the server for editing. The passed ID is invalid.</span>
[% CASE 'error_on_test' %]
<span>Failed to send test email.</span>
[% CASE 'error_on_insert' %]
<span>An error occurred when adding the server. The library already has an SMTP server set.</span>
[% CASE 'success_on_update' %]
<span>Server updated successfully.</span>
[% CASE 'success_on_test' %]
<span>Test email sent successfully to <em>KohaAdminEmailAddress</em>.</span>
[% CASE 'success_on_insert' %]
<span>Server added successfully.</span>
[% CASE %]
@ -435,6 +439,13 @@
encodeURIComponent(row.name.escapeHtml()) +
'"><i class="fa fa-trash-can" aria-hidden="true"></i> ' +
_("Delete") +
"</a>" +
"\n";
result +=
'<a class="btn btn-default btn-xs submit-form-link" role="button" href="#" data-action="/cgi-bin/koha/admin/smtp_servers.pl" data-method="post" data-op="cud-test_email" data-smtp_server_id="' +
encodeURIComponent(row.smtp_server_id) +
'"><i class="fa fa-check-square" aria-hidden="true"></i> ' +
_("Send test email") +
"</a>";
return result;
},