query( 'SELECT ea.*, d.name AS department_name, p.name AS priority_name, h.name AS topic_name FROM email_addresses ea JOIN departments d ON d.id = ea.department_id LEFT JOIN priorities p ON p.id = ea.priority_id LEFT JOIN help_topics h ON h.id = ea.help_topic_id ORDER BY d.name, ea.email' )->fetchAll(); $departments = departments_for_select($pdo, array_map('intval', $pdo->query('SELECT department_id FROM email_addresses')->fetchAll(PDO::FETCH_COLUMN))); $priorities = $pdo->query('SELECT id, name FROM priorities ORDER BY sort_order')->fetchAll(); $topics = help_topic_options($pdo, false, false); $imapByAddress = []; foreach ($pdo->query('SELECT * FROM imap_accounts WHERE email_address_id IS NOT NULL ORDER BY id')->fetchAll() as $row) { $imapByAddress[(int) $row['email_address_id']] ??= $row; } api_json([ 'departments' => array_map(fn(array $d) => ['id' => (int) $d['id'], 'name' => $d['name']], $departments), 'priorities' => array_map(fn(array $p) => ['id' => (int) $p['id'], 'name' => $p['name']], $priorities), 'helpTopics' => array_map(fn(array $t) => ['id' => (int) $t['id'], 'name' => $t['path']], $topics), 'items' => array_map(function (array $a) use ($imapByAddress) { $imap = $imapByAddress[(int) $a['id']] ?? null; return [ 'id' => (int) $a['id'], 'email' => $a['email'], 'label' => $a['label'], 'departmentId' => (int) $a['department_id'], 'departmentName' => $a['department_name'], 'priorityId' => $a['priority_id'] !== null ? (int) $a['priority_id'] : null, 'priorityName' => $a['priority_name'], 'helpTopicId' => $a['help_topic_id'] !== null ? (int) $a['help_topic_id'] : null, 'helpTopicName' => $a['topic_name'], 'isDefault' => (bool) $a['is_default'], 'isActive' => (bool) ($a['is_active'] ?? 1), 'notes' => $a['notes'] ?? null, 'updatedAt' => $a['updated_at'] ?? null, 'imapEnabled' => $imap !== null && (bool) $imap['is_active'], 'imapHost' => $imap['host'] ?? '', 'imapPort' => (int) ($imap['port'] ?? 993), 'imapEncryption' => $imap['encryption'] ?? 'ssl', 'imapUsername' => $imap['username'] ?? '', 'imapFolder' => $imap['folder'] ?? 'INBOX', 'imapAfterFetch' => $imap['after_fetch'] ?? 'seen', 'imapArchiveFolder' => $imap['archive_folder'] ?? 'Processed', 'imapSpamFolder' => $imap['spam_folder'] ?? 'Junk', 'imapHasPassword' => $imap !== null && ($imap['password_enc'] ?? '') !== '', 'imapLastRunAt' => $imap['last_run_at'] ?? null, 'smtpEnabled' => (bool) ($a['smtp_enabled'] ?? 0), 'smtpHost' => $a['smtp_host'] ?? '', 'smtpPort' => (int) ($a['smtp_port'] ?? 587), 'smtpEncryption' => $a['smtp_encryption'] ?? 'tls', 'smtpUsername' => $a['smtp_username'] ?? '', 'smtpHasPassword' => ($a['smtp_password_enc'] ?? '') !== '', ]; }, $addresses), ]); } $body = api_request_body(); $action = $body['action'] ?? ''; $id = (int) ($body['id'] ?? 0); try { if ($action === 'save') { $b = fn(string $k, $d = null) => $body[$k] ?? $d; $in = [ 'email' => $b('email', ''), 'label' => $b('label', ''), 'department_id' => $b('departmentId', 0), 'priority_id' => $b('priorityId'), 'help_topic_id' => $b('helpTopicId'), 'is_default' => !empty($body['isDefault']), 'notes' => $b('notes', ''), 'imap_enabled' => !empty($body['imapEnabled']), 'imap_host' => $b('imapHost', ''), 'imap_port' => $b('imapPort', 993), 'imap_encryption' => $b('imapEncryption', 'ssl'), 'imap_username' => $b('imapUsername', ''), 'imap_password' => $b('imapPassword', ''), 'imap_folder' => $b('imapFolder', ''), 'imap_after_fetch' => $b('imapAfterFetch', 'seen'), 'imap_archive_folder' => $b('imapArchiveFolder', ''), 'imap_spam_folder' => $b('imapSpamFolder', ''), 'smtp_enabled' => !empty($body['smtpEnabled']), 'smtp_host' => $b('smtpHost', ''), 'smtp_port' => $b('smtpPort', 587), 'smtp_encryption' => $b('smtpEncryption', 'tls'), 'smtp_username' => $b('smtpUsername', ''), 'smtp_password' => $b('smtpPassword', ''), ]; if (array_key_exists('isActive', $body)) $in['is_active'] = !empty($body['isActive']); $newId = email_address_save($pdo, $id, $in); audit_log($staff['id'], ($id === 0 ? 'E-Mail-Adresse erstellt: ' : 'E-Mail-Adresse aktualisiert: ') . strtolower(trim((string) ($body['email'] ?? ''))) . ' (Mobile App)'); api_json(['id' => $newId]); } if ($action === 'delete') { $deleted = email_address_delete($pdo, $id); audit_log($staff['id'], "E-Mail-Adresse gelöscht: {$deleted} (Mobile App)"); api_json(['ok' => true]); } // Tests: Test-Mail an die eigene Adresse, IMAP-Verbindung, SMTP-Erreichbarkeit if ($action === 'sendTest') api_json(['ok' => true, 'message' => email_address_send_test($pdo, $id, (string) $staff['email'])]); if ($action === 'testImap') api_json(['ok' => true, 'message' => email_address_test_imap($pdo, $id)]); if ($action === 'testSmtp') api_json(['ok' => true, 'message' => email_address_test_smtp($pdo, $id)]); } catch (EmailAddressException $e) { api_error($e->httpStatus(), $e->errorCode(), $e->getMessage()); } api_error(422, 'INVALID_ACTION', 'Unbekannte Aktion.');