testversand
This commit is contained in:
@@ -59,6 +59,9 @@ require dirname(__DIR__) . '/../structure/layout_start.php';
|
||||
<label class="block text-sm text-slate-600">Bezeichnung
|
||||
<input type="text" name="label" class="input mt-1" placeholder="Profil-Name">
|
||||
</label>
|
||||
<label class="inline-flex items-center gap-2 text-sm text-slate-600">
|
||||
<input type="checkbox" name="is_default" value="1"> Als Standard-Testprofil verwenden
|
||||
</label>
|
||||
<div class="grid md:grid-cols-2 gap-3">
|
||||
<label class="block text-sm text-slate-600">SMTP-Server
|
||||
<input type="text" name="smtp_host" class="input mt-1" placeholder="smtp.example.com">
|
||||
|
||||
@@ -949,13 +949,15 @@ export function initEditor() {
|
||||
let html = '<option value="">Standard (System)</option>';
|
||||
smtpProfileOptions.forEach(opt => {
|
||||
const label = opt.label || opt.smtp_host || 'Profil';
|
||||
html += `<option value="${opt.id}">${escapeHtml(label)}</option>`;
|
||||
const suffix = opt.is_default ? ' (Standard)' : '';
|
||||
html += `<option value="${opt.id}">${escapeHtml(label + suffix)}</option>`;
|
||||
});
|
||||
sendSmtpProfile.innerHTML = html;
|
||||
if (previous && smtpProfileOptions.some(opt => String(opt.id) === previous)) {
|
||||
sendSmtpProfile.value = previous;
|
||||
} else {
|
||||
sendSmtpProfile.value = '';
|
||||
const def = smtpProfileOptions.find(opt => opt.is_default);
|
||||
sendSmtpProfile.value = def ? String(def.id) : '';
|
||||
}
|
||||
if (sendSmtpProfileHint) {
|
||||
sendSmtpProfileHint.classList.toggle('hidden', smtpProfileOptions.length > 0);
|
||||
|
||||
@@ -921,7 +921,7 @@ function renderSmtpProfileList() {
|
||||
const sender = profile.from_email || '—';
|
||||
return `
|
||||
<tr>
|
||||
<td>${escapeHtml(label)}</td>
|
||||
<td>${escapeHtml(label)}${profile.is_default ? ' <span class="text-xs text-slate-500">(Standard)</span>' : ''}</td>
|
||||
<td>${escapeHtml(host)}</td>
|
||||
<td>${escapeHtml(user)}</td>
|
||||
<td>${escapeHtml(sender)}</td>
|
||||
@@ -930,6 +930,7 @@ function renderSmtpProfileList() {
|
||||
<button class="btn" data-smtp-action="edit" data-smtp-id="${profile.id}">Bearbeiten</button>
|
||||
<button class="btn" data-smtp-action="copy" data-smtp-id="${profile.id}">Kopieren</button>
|
||||
<button class="btn btn-danger" data-smtp-action="delete" data-smtp-id="${profile.id}">Löschen</button>
|
||||
${profile.is_default ? '' : `<button class="btn" data-smtp-action="set-default" data-smtp-id="${profile.id}">Standard</button>`}
|
||||
</td>
|
||||
</tr>`;
|
||||
}).join('');
|
||||
@@ -946,6 +947,8 @@ function handleSmtpProfileTableClick(ev) {
|
||||
openSmtpProfileForm(profile);
|
||||
} else if (action === 'test') {
|
||||
runSmtpProfileTest(profile.id);
|
||||
} else if (action === 'set-default') {
|
||||
setDefaultSmtpProfile(profile.id);
|
||||
} else if (action === 'copy') {
|
||||
if (confirm(`Versandprofil "${profile.label || profile.smtp_host}" kopieren?`)) {
|
||||
copySmtpProfile(id);
|
||||
@@ -971,6 +974,7 @@ function openSmtpProfileForm(profile = null) {
|
||||
smtpProfileForm.from_email.value = profile?.from_email || '';
|
||||
smtpProfileForm.from_name.value = profile?.from_name || '';
|
||||
smtpProfileForm.reply_to.value = profile?.reply_to || '';
|
||||
smtpProfileForm.is_default.checked = !!profile?.is_default;
|
||||
if (smtpProfileForm.smtp_pass_clear) smtpProfileForm.smtp_pass_clear.checked = false;
|
||||
}
|
||||
|
||||
@@ -996,6 +1000,7 @@ async function submitSmtpProfileForm(ev) {
|
||||
from_name: smtpProfileForm.from_name.value.trim(),
|
||||
reply_to: smtpProfileForm.reply_to.value.trim(),
|
||||
smtp_pass_clear: smtpProfileForm.smtp_pass_clear?.checked ? 1 : 0,
|
||||
is_default: smtpProfileForm.is_default?.checked ? 1 : 0,
|
||||
};
|
||||
if (!payload.smtp_host) {
|
||||
toast('Bitte einen SMTP-Server angeben', false);
|
||||
@@ -1046,6 +1051,17 @@ async function runSmtpProfileTest(profileId) {
|
||||
}
|
||||
}
|
||||
|
||||
async function setDefaultSmtpProfile(profileId) {
|
||||
try {
|
||||
const res = await apiAction('account.smtp_profiles.save', { method: 'POST', data: { profile_id: profileId, is_default: 1 } });
|
||||
if (!res?.ok) throw new Error(res?.error || 'Standard konnte nicht gesetzt werden');
|
||||
await loadSmtpProfiles();
|
||||
toast('Standard-Testprofil gesetzt', true);
|
||||
} catch (err) {
|
||||
toast(err.message || 'Standard konnte nicht gesetzt werden', false);
|
||||
}
|
||||
}
|
||||
|
||||
function escapeHtml(str) {
|
||||
return String(str || '')
|
||||
.replace(/&/g, '&')
|
||||
|
||||
@@ -225,6 +225,7 @@ CREATE TABLE IF NOT EXISTS `emailtemplate_smtp_profiles` (
|
||||
`from_email` varchar(255) DEFAULT NULL,
|
||||
`from_name` varchar(255) DEFAULT NULL,
|
||||
`reply_to` varchar(255) DEFAULT NULL,
|
||||
`is_default` tinyint(1) DEFAULT 0,
|
||||
`created_at` timestamp NOT NULL DEFAULT current_timestamp(),
|
||||
`updated_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
|
||||
PRIMARY KEY (`id`),
|
||||
|
||||
@@ -2248,6 +2248,9 @@ class ApiKernel
|
||||
}
|
||||
$senderId = (int)$this->val($this->in, ['sender_id'], 0);
|
||||
$smtpProfileId = (int)$this->val($this->in, ['smtp_profile_id'], 0);
|
||||
if ($smtpProfileId <= 0 && $customerId > 0) {
|
||||
$smtpProfileId = $this->getDefaultSmtpProfileId($customerId);
|
||||
}
|
||||
|
||||
$row = null;
|
||||
$html = '';
|
||||
@@ -4369,6 +4372,32 @@ class ApiKernel
|
||||
$fromName = trim((string)($this->in['from_name'] ?? ''));
|
||||
$replyTo = trim((string)($this->in['reply_to'] ?? ''));
|
||||
$passClear = !empty($this->in['smtp_pass_clear']);
|
||||
$isDefault = !empty($this->in['is_default']);
|
||||
|
||||
$defaultOnly = $profileId > 0
|
||||
&& $isDefault
|
||||
&& $label === ''
|
||||
&& $host === ''
|
||||
&& $port === 0
|
||||
&& $userName === ''
|
||||
&& $pass === ''
|
||||
&& $secure === ''
|
||||
&& $fromEmail === ''
|
||||
&& $fromName === ''
|
||||
&& $replyTo === ''
|
||||
&& !$passClear;
|
||||
|
||||
if ($defaultOnly) {
|
||||
$this->ensureSmtpProfilesTableExists();
|
||||
$table = $this->smtpProfilesTable();
|
||||
$stmt = $this->pdo->prepare("UPDATE `$table` SET `is_default` = 0 WHERE `customer_id` = :cid AND `id` != :id");
|
||||
$stmt->execute([':cid' => $customerId, ':id' => $profileId]);
|
||||
$stmt = $this->pdo->prepare("UPDATE `$table` SET `is_default` = 1 WHERE `id` = :id AND `customer_id` = :cid");
|
||||
$stmt->execute([':id' => $profileId, ':cid' => $customerId]);
|
||||
$profile = $this->fetchSmtpProfileRow($customerId, $profileId);
|
||||
$this->respond(['ok' => true, 'profile' => $profile]);
|
||||
return;
|
||||
}
|
||||
|
||||
if ($label === '') $label = $fromEmail ?: $host;
|
||||
if ($host === '') $this->fail('SMTP-Host erforderlich', null, 422);
|
||||
@@ -4397,6 +4426,7 @@ class ApiKernel
|
||||
'from_email' => $fromEmail ?: null,
|
||||
'from_name' => $fromName ?: null,
|
||||
'reply_to' => $replyTo ?: null,
|
||||
'is_default' => $isDefault ? 1 : 0,
|
||||
];
|
||||
if ($passClear) {
|
||||
$fields['smtp_pass'] = null;
|
||||
@@ -4415,7 +4445,7 @@ class ApiKernel
|
||||
$stmt->execute($params);
|
||||
if ($stmt->rowCount() === 0) $this->fail('Versandprofil nicht gefunden', null, 404);
|
||||
} else {
|
||||
$stmt = $this->pdo->prepare("INSERT INTO `$table` (`customer_id`,`label`,`smtp_host`,`smtp_port`,`smtp_user`,`smtp_pass`,`smtp_secure`,`from_email`,`from_name`,`reply_to`,`created_at`,`updated_at`) VALUES (:cid,:label,:host,:port,:user,:pass,:secure,:fmail,:fname,:reply,NOW(),NOW())");
|
||||
$stmt = $this->pdo->prepare("INSERT INTO `$table` (`customer_id`,`label`,`smtp_host`,`smtp_port`,`smtp_user`,`smtp_pass`,`smtp_secure`,`from_email`,`from_name`,`reply_to`,`is_default`,`created_at`,`updated_at`) VALUES (:cid,:label,:host,:port,:user,:pass,:secure,:fmail,:fname,:reply,:is_default,NOW(),NOW())");
|
||||
$stmt->execute([
|
||||
':cid' => $customerId,
|
||||
':label' => $label,
|
||||
@@ -4427,8 +4457,20 @@ class ApiKernel
|
||||
':fmail' => $fromEmail ?: null,
|
||||
':fname' => $fromName ?: null,
|
||||
':reply' => $replyTo ?: null,
|
||||
':is_default' => $isDefault ? 1 : 0,
|
||||
]);
|
||||
$profileId = (int)$this->pdo->lastInsertId();
|
||||
if ($isDefault) {
|
||||
$stmt = $this->pdo->prepare("UPDATE `$table` SET `is_default` = 0 WHERE `customer_id` = :cid AND `id` != :id");
|
||||
$stmt->execute([':cid' => $customerId, ':id' => $profileId]);
|
||||
$stmt = $this->pdo->prepare("UPDATE `$table` SET `is_default` = 1 WHERE `id` = :id AND `customer_id` = :cid");
|
||||
$stmt->execute([':id' => $profileId, ':cid' => $customerId]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($profileId > 0 && $isDefault) {
|
||||
$stmt = $this->pdo->prepare("UPDATE `$table` SET `is_default` = 0 WHERE `customer_id` = :cid AND `id` != :id");
|
||||
$stmt->execute([':cid' => $customerId, ':id' => $profileId]);
|
||||
}
|
||||
|
||||
$profile = $this->fetchSmtpProfileRow($customerId, $profileId);
|
||||
@@ -5502,6 +5544,7 @@ SQL;
|
||||
{
|
||||
$table = $this->smtpProfilesTable();
|
||||
if ($this->tableExists($table)) {
|
||||
$this->ensureSmtpProfilesColumns($table);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
@@ -5518,6 +5561,7 @@ CREATE TABLE IF NOT EXISTS `$table` (
|
||||
`from_email` varchar(255) DEFAULT NULL,
|
||||
`from_name` varchar(255) DEFAULT NULL,
|
||||
`reply_to` varchar(255) DEFAULT NULL,
|
||||
`is_default` tinyint(1) DEFAULT 0,
|
||||
`created_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
`updated_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
PRIMARY KEY (`id`),
|
||||
@@ -5533,6 +5577,27 @@ SQL;
|
||||
}
|
||||
}
|
||||
|
||||
private function ensureSmtpProfilesColumns(string $table): void
|
||||
{
|
||||
try {
|
||||
$columns = $this->tableColumns($table);
|
||||
} catch (Throwable $e) {
|
||||
$this->fail('SMTP-Profil-Tabelle konnte nicht gelesen werden', $e->getMessage(), 500);
|
||||
return;
|
||||
}
|
||||
$missing = [];
|
||||
if (!in_array('is_default', $columns, true)) {
|
||||
$missing[] = 'ADD COLUMN `is_default` tinyint(1) DEFAULT 0';
|
||||
}
|
||||
if (!$missing) return;
|
||||
try {
|
||||
$sql = 'ALTER TABLE `' . $table . '` ' . implode(', ', $missing);
|
||||
$this->pdo->exec($sql);
|
||||
} catch (Throwable $e) {
|
||||
$this->fail('SMTP-Profil-Tabelle konnte nicht aktualisiert werden', $e->getMessage(), 500);
|
||||
}
|
||||
}
|
||||
|
||||
private function fetchSenderRow(int $customerId, int $senderId): array
|
||||
{
|
||||
if ($customerId <= 0 || $senderId <= 0) {
|
||||
@@ -5599,12 +5664,23 @@ SQL;
|
||||
'from_email' => $row['from_email'] ?? '',
|
||||
'from_name' => $row['from_name'] ?? '',
|
||||
'reply_to' => $row['reply_to'] ?? '',
|
||||
'is_default' => !empty($row['is_default']) ? 1 : 0,
|
||||
'smtp_pass_set' => $pass !== '',
|
||||
'created_at' => $row['created_at'] ?? null,
|
||||
'updated_at' => $row['updated_at'] ?? null,
|
||||
];
|
||||
}
|
||||
|
||||
private function getDefaultSmtpProfileId(int $customerId): int
|
||||
{
|
||||
if ($customerId <= 0) return 0;
|
||||
$this->ensureSmtpProfilesTableExists();
|
||||
$table = $this->smtpProfilesTable();
|
||||
$stmt = $this->pdo->prepare("SELECT `id` FROM `$table` WHERE `customer_id` = :cid AND `is_default` = 1 ORDER BY `id` ASC LIMIT 1");
|
||||
$stmt->execute([':cid' => $customerId]);
|
||||
return (int)($stmt->fetchColumn() ?: 0);
|
||||
}
|
||||
|
||||
private function formatUserOutput(array $row): array
|
||||
{
|
||||
return [
|
||||
|
||||
Reference in New Issue
Block a user