           alert('Error sending test email: ' + data.error);
        } else {
            alert('Test email sent successfully to ' + data.sentTo + '!');
            $('#testEmailModal').modal('hide');
        }
    })
    .catch(error => {
        console.error('Error:', error);
        alert('Failed to send test email');
    })
    .finally(() => {
        // Restore button state
        sendButton.innerHTML = originalText;
        sendButton.disabled = false;
    });
}
</script>
{{end}}