Tak pomocí ChatGPT se mi to podařilo opravit pokud by někdo chtěl opravu včetně novinky že je přidano tlačítko na ověření SMTP funkčnosti kod:
<?php
namespace SunlightExtend\Phpmailer;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use Sunlight\Plugin\Action\ConfigAction as BaseConfigAction;
use Sunlight\Router;
use Sunlight\Settings;
use Sunlight\Util\ConfigurationFile;
class ConfigAction extends BaseConfigAction
{
private const SMTP_SECURE_MODE = [
'none' => 'none',
PHPMailer::ENCRYPTION_STARTTLS => 'TLS',
PHPMailer::ENCRYPTION_SMTPS => 'SSL',
];
protected function getFields(): array
{
$config = $this->plugin->getConfig();
// Test výstupu
$testResult = '';
if (isset($_POST['test_smtp'])) {
$mailer = new PHPMailer(true);
try {
$mailer->isSMTP();
$mailer->SMTPDebug = SMTP::DEBUG_OFF;
$mailer->Host = $_POST['config']['smtp_host'] ?? '';
$mailer->Port = (int)($_POST['config']['smtp_port'] ?? 25);
$mailer->SMTPAuth = !empty($_POST['config']['smtp_auth']);
$mailer->Username = $_POST['config']['smtp_user'] ?? '';
$mailer->Password = $_POST['config']['smtp_pass'] ?? '';
$secure = $_POST['config']['smtp_secure'] ?? 'none';
if ($secure !== 'none') {
$mailer->SMTPSecure = $secure;
}
$mailer->SMTPAutoTLS = !empty($_POST['config']['smtp_auto_tls']);
$mailer->setFrom('test@example.com', 'SMTP Test');
$mailer->addAddress('test@example.com');
$mailer->Subject = 'SMTP test';
$mailer->Body = 'This is a test email.';
$mailer->send();
$testResult = '<div class="message message-ok">✅ Připojení k SMTP serveru bylo úspěšné.</div>';
} catch (\Exception $e) {
$testResult = '<div class="message message-error">❌ Chyba: ' . _e($mailer->ErrorInfo ?? $e->getMessage()) . '</div>';
}
}
$emailSetupLink = ' <a href="' . _e(Router::admin('settings', ['fragment' => 'settings_emails'])) . '" target="_blank" class="button"><img src="' . Router::path('admin/public/images/icons/action.png') . '" alt="setting" class="icon">' . _lang('phpmailer.config.email_setting') . '</a>';
return [
'smtp_test_result' => [
'label' => '',
'input' => $testResult,
],
'use_smtp' => [
'label' => _lang('phpmailer.config.use_smtp'),
'input' => '<input type="checkbox" name="config[use_smtp]" value="1"' . (!empty($config['use_smtp']) ? ' checked' : '') . '>',
'type' => 'checkbox'
],
'smtp_secure' => [
'label' => _lang('phpmailer.config.smtp_secure'),
'input' => _buffer(function () use ($config) { ?>
<select name="config[smtp_secure]" class="inputsmall">
<?php foreach (self::SMTP_SECURE_MODE as $k => $v): ?>
<option value="<?= _e($k) ?>"<?= (($config['smtp_secure'] ?? '') === $k ? ' selected' : '') ?>><?= _e($v) ?></option>
<?php endforeach; ?>
</select>
<?php }),
],
'smtp_auth' => [
'label' => _lang('phpmailer.config.smtp_auth'),
'input' => '<input type="checkbox" name="config[smtp_auth]" value="1"' . (!empty($config['smtp_auth']) ? ' checked' : '') . '>',
'type' => 'checkbox'
],
'smtp_port' => [
'label' => _lang('phpmailer.config.smtp_port'),
'input' => '<input type="number" name="config[smtp_port]" value="' . _e($_POST['config']['smtp_port'] ?? $config['smtp_port'] ?? '') . '" class="inputmedium">',
'type' => 'text'
],
'smtp_host' => [
'label' => _lang('phpmailer.config.smtp_host'),
'input' => '<input type="text" name="config[smtp_host]" value="' . _e($_POST['config']['smtp_host'] ?? $config['smtp_host'] ?? '') . '" class="inputmedium">',
'type' => 'text'
],
'smtp_user' => [
'label' => _lang('phpmailer.config.smtp_user'),
'input' => '<input type="text" name="config[smtp_user]" value="' . _e($_POST['config']['smtp_user'] ?? $config['smtp_user'] ?? '') . '" class="inputmedium">',
'type' => 'text'
],
'smtp_pass' => [
'label' => _lang('phpmailer.config.smtp_pass'),
'input' => '<input type="password" name="config[smtp_pass]" value="' . _e($_POST['config']['smtp_pass'] ?? $config['smtp_pass'] ?? '') . '" class="inputmedium">',
'type' => 'text'
],
'sender_email' => [
'label' => _lang('phpmailer.config.sender_email'),
'input' => '<input type="text" name="config[sender_email]" value="' . _e($_POST['config']['sender_email'] ?? Settings::get('sysmail') ?? '') . '" class="inputmedium" disabled>' . $emailSetupLink,
],
'sender_name' => [
'label' => _lang('phpmailer.config.sender_name'),
'input' => '<input type="text" name="config[sender_name]" value="' . _e($_POST['config']['sender_name'] ?? $config['sender_name'] ?? '') . '" class="inputmedium">',
'type' => 'text'
],
'smtp_auto_tls' => [
'label' => _lang('phpmailer.config.smtp_auto_tls'),
'input' => '<input type="checkbox" name="config[smtp_auto_tls]" value="1"' . (!empty($config['smtp_auto_tls']) ? ' checked' : '') . '>',
'type' => 'checkbox'
],
'test_smtp' => [
'label' => '',
'input' => '<button class="button" type="submit" name="test_smtp" value="1">🔍 Testovat SMTP připojení</button>',
]
];
}
protected function mapSubmittedValue(ConfigurationFile $config, string $key, array $field, $value): ?string
{
switch ($key) {
case 'smtp_secure':
$config[$key] = $value;
return null;
case 'sender_email':
$config[$key] = null;
return null;
case 'test_smtp':
return null;
case 'smtp_test_result': // toto pole taky ignoruj, pokud přijde
return null;
default:
// Pokud je klíč nastavený v konfiguraci, uložíme ho
if (isset($config[$key])) {
$config[$key] = $value;
return null;
}
// jinak ignoruj bez chyby
return null;
}
}
}