testversand

This commit is contained in:
2026-02-24 01:33:52 +01:00
parent 6a8d5720c5
commit 6f7ac43a0b
5 changed files with 102 additions and 4 deletions

View File

@@ -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 [