From 0791377da3c5b2ab349bb9c82c437de1b2d23e04 Mon Sep 17 00:00:00 2001 From: KnowSky404 Date: Thu, 8 Oct 2026 11:32:53 +0200 Subject: [PATCH 01/10] feat(mail): add verified domain onboarding and identity settings --- README.en.md | 2 +- README.md | 2 +- docs/API.md | 30 ++++- docs/PREVIEW.md | 57 +++++++++ ...wner-managed-mail-identities-and-access.md | 9 ++ package.json | 4 +- .../components/mail/MailDomainSetup.svelte | 58 +++++++++ .../mail/MailIdentityManager.svelte | 116 +++++++++++++++++- src/lib/i18n/messages.ts | 48 +++++++- src/lib/server/cloudflare.ts | 3 + .../server/mail-identities/domains.test.ts | 92 ++++++++++++++ src/lib/server/mail-identities/domains.ts | 97 +++++++++++++++ .../api/workspace/mail-identities/+server.ts | 3 + .../mail-identities/[addressId]/+server.ts | 28 +++++ .../[addressId]/server.test.ts | 16 ++- .../mail-identities/domains/+server.ts | 17 +++ .../domains/[domainId]/+server.ts | 21 ++++ .../mail-identities/domains/server.test.ts | 57 +++++++++ tests/e2e/workspace.spec.ts | 108 ++++++++++++++++ wrangler.preview.deploy.toml.example | 42 +++++++ 20 files changed, 792 insertions(+), 18 deletions(-) create mode 100644 docs/PREVIEW.md create mode 100644 src/lib/components/mail/MailDomainSetup.svelte create mode 100644 src/lib/server/mail-identities/domains.test.ts create mode 100644 src/lib/server/mail-identities/domains.ts create mode 100644 src/routes/api/workspace/mail-identities/domains/+server.ts create mode 100644 src/routes/api/workspace/mail-identities/domains/[domainId]/+server.ts create mode 100644 src/routes/api/workspace/mail-identities/domains/server.test.ts create mode 100644 wrangler.preview.deploy.toml.example diff --git a/README.en.md b/README.en.md index f2a2779..160925c 100644 --- a/README.en.md +++ b/README.en.md @@ -14,7 +14,7 @@ The repository is pinned to Bun `1.4.0`. The application preserves the existing - D1/R2 persistence for inbound mail, attachments, ownership, read/star state, archive/trash actions, drafts, sent mail, delivery state, and event timelines. Downloads verify ownership, size, and checksum before returning bytes. - Resend outbound delivery with stable idempotency keys, reply/RFC headers, streamed R2 attachments, error classification, retries, and separate submitted/delivered semantics. - Explicit `AUTH_MODE=local|cloudflare-access` authentication: a non-email local username and PBKDF2 password, or a verified Cloudflare Access identity mapped to the same stable Owner. Both modes retain revocable D1 sessions, secure cookies, Origin/CSRF validation, login rate limiting, and security headers. When Access expires, the browser keeps the active compose buffer in that tab and refreshes only safe GETs after reauthentication; it never blindly replays writes. -- Multi-domain mail identities: domains enter through a controlled configuration flow, and address routing uses exact Cloudflare Email Routing Rules in explicitly selected zones. Inbound ownership is resolved from the envelope recipient before body parsing or R2 writes. Inbox filters apply server-side to mail, drafts, search, counts, and pagination. Bulk actions default to selected loaded messages; filtered-thread and Owner-wide thread expansion require an explicit choice. Trash is always an Owner-wide view. +- Multi-domain mail identities: domains can be verified and connected in the browser, with enable/disable and unknown-recipient settings plus editable address names/signatures ([Preview setup](./docs/PREVIEW.md)). Address routing uses exact Cloudflare Email Routing Rules in explicitly selected zones. Inbound ownership is resolved from the envelope recipient before body parsing or R2 writes. Inbox filters apply server-side to mail, drafts, search, counts, and pagination. Bulk actions default to selected loaded messages; filtered-thread and Owner-wide thread expansion require an explicit choice. Trash is always an Owner-wide view. - Multiple sender identities: new mail prefers a ready exact address selected by the current address filter, while other views use the ready global default; replies and drafts keep their original identity. The server checks Owner, address state, and the domain's Resend sending verification, then freezes the From/signature snapshot. The compose UI explains disabled, unverified, stale, and failed sender states. Legacy `OUTBOUND_FROM_EMAIL` / `MAIL_FROM` values are only for system auto-replies and notifications. - Compose UI: the desktop floating window supports dragging, keyboard resizing, minimizing, and maximizing; phones retain full-screen compose. To offers recent contacts from messages already loaded in this browser session; Cc/Bcc chips, autosave, and attachments remain available. The optional HTML source starts collapsed. See [the UI/UX review](./docs/UI_UE_REVIEW.md) for the current scope and pending acceptance work. - The compose editor loads on first use; if its download fails, the workspace remains available and prompts a page refresh before retrying. diff --git a/README.md b/README.md index f47e573..5a72cc8 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ FlareMail 是一个部署在 Cloudflare Workers 上的个人自托管邮件工 - Resend 出站:稳定幂等键、`reply_to`/RFC headers、R2 流式附件上传与完整性校验、错误分类、重试,以及 `submitted` 与 `delivered` 的严格语义区分。 - Resend webhook:Svix 签名与时间窗口校验、事件去重、乱序保护、未知事件保留,以及退信/投诉/抑制等终态。 - 明确的 `AUTH_MODE=local|cloudflare-access` 认证:本地用户名(无需邮箱)和 PBKDF2 密码,或验签后的 Cloudflare Access 身份映射到相同稳定 Owner;两种模式共用 D1 会话吊销、Cookie、Origin/CSRF、登录限速和安全响应头。Access 过期时,浏览器保留当前标签中的写信内容,重新认证后只刷新安全 GET,不盲重放写请求。 -- 多域名邮件身份:域名通过受控配置显式加入;地址路由只使用限定 zone 的 Cloudflare Email Routing Rules API 精确规则。收件在读取正文/R2 前按信封收件人解析地址,统一邮箱支持服务端域名/地址筛选,搜索、计数、分页和草稿使用同一筛选范围。批量操作默认只修改已选的已加载邮件;筛选内会话和跨 Owner 地址的完整会话均需明确选择,回收站始终是 Owner 全局视图。 +- 多域名邮件身份:支持在网页核验并接入域名、启停域名和设置未知地址策略;地址支持名称与签名编辑,详见 [Preview 与接入配置](./docs/PREVIEW.md)。地址路由只使用限定 zone 的 Cloudflare Email Routing Rules API 精确规则。收件在读取正文/R2 前按信封收件人解析地址,统一邮箱支持服务端域名/地址筛选,搜索、计数、分页和草稿使用同一筛选范围。批量操作默认只修改已选的已加载邮件;筛选内会话和跨 Owner 地址的完整会话均需明确选择,回收站始终是 Owner 全局视图。 - 多发件身份:新邮件优先采用当前精确地址筛选中的可发信地址,其他视图使用可发信的全局默认;回复和草稿保留各自原有身份。服务端检查 Owner、地址状态和域级 Resend 发信验证,再固定 From/签名快照;编辑器分别说明地址/域停用、验证状态、检查过期和失败。旧 `OUTBOUND_FROM_EMAIL` / `MAIL_FROM` 仅供系统自动回复和通知使用。 - 响应式阅读工作台:桌面三栏、可折叠侧栏、默认 440 px 且可在 280–480 px 拖拽/键盘调整的邮件列表、标准/紧凑显示密度、平板/手机 drill-in、近全屏专注阅读和 `/messages/[id]` 独立阅读地址;全局星标按 Owner 范围跨收件/归档/已发送/草稿服务端分页,域名概览、邮箱地址管理和设置可从侧栏分别直达,域名卡片可输入地址前缀快速创建并对收信规则未完成的状态给出提示;Owner 标签可创建、重命名、删除并应用到邮件,标签视图跨收件、归档、已发送、草稿分页;收件、已发送、归档、星标和标签视图支持仅对当前页已选邮件批量添加或移除标签,不扩展到整个会话;星标/标签视图对明确选中的非草稿邮件还支持跨文件夹已读、星标和删除操作,归档仅作用于全为收件的选择,含草稿时只保留标签操作;列表宽度会在详情区可用空间不足时临时收窄,按 Enter 可恢复 440 px 默认值;同一用户打开的窗口只同步邮件状态信号,不传播正文、地址或凭据。 - 写信界面:桌面浮动窗口支持拖动、键盘缩放、最小化与最大化,手机保持全屏;多发件身份选择、To 收件人最近联系人建议、Cc/Bcc chip、草稿自动保存及附件上传;建议只来自当前会话已加载的邮件,HTML 源码选项默认收起。当前 UI 审查和后续验收范围见 [docs/UI_UE_REVIEW.md](./docs/UI_UE_REVIEW.md)。 diff --git a/docs/API.md b/docs/API.md index f066b0f..c2ff4aa 100644 --- a/docs/API.md +++ b/docs/API.md @@ -81,11 +81,31 @@ the actual envelope destination independently from the message's To header. ## Managed domains and addresses `GET /api/workspace/mail-identities` returns only the authenticated Owner's -configured domains and addresses. Domain enrollment is an operator-controlled -configuration step (`bun run mail:domain:configure`) that records the exact -domain-to-zone and Worker mapping; the browser cannot choose an arbitrary zone, -Worker, API origin, or Cloudflare endpoint. The response also includes boolean -provider-configuration flags; it never returns a token or key. +configured domains and addresses. `domainOnboarding` reports whether browser +enrollment is available and whether route management credentials are configured. +The deployment fixes `MAIL_IDENTITY_WORKER_NAME` and `MAIL_IDENTITY_ACCOUNT_ID`; +the browser supplies a domain and Zone ID, which the server verifies against +Cloudflare before recording the mapping. The browser cannot choose a Worker, +account, API origin, or Cloudflare endpoint. The operator command +`bun run mail:domain:configure` remains available. Provider-configuration flags +never return a token or key; `cloudflareManagement` separately reports whether +the management credential is present. + +- `POST /api/workspace/mail-identities/domains` accepts only `domainName` and + `zoneId`. It requires Zone Read permission, checks the zone ID, domain suffix + boundary and deployment account, and enrolls with unknown recipients rejected. + A repeated identical enrollment returns the existing domain; existing mappings + and ownership are never overwritten. Enrollment does not modify DNS or create + Email Routing rules, and does not claim receiving or sending readiness. +- `PATCH /api/workspace/mail-identities/domains/:domainId` accepts `enabled` + and/or `unknownRecipientPolicy` (`reject` or `collect`). It preserves the zone, + Worker mapping, addresses and historical mail. Switching to collect requires + a fresh successful observation of catch-all targeting the configured Worker. + Disabling a domain stops application receiving/sending without deleting + external rules. Concurrent settings or health updates cause a refresh/retry. +- `PATCH /api/workspace/mail-identities/:addressId` accepts `displayName` and + `signature`. It edits only the authenticated Owner's non-deleted address and + preserves the email, routing, and receiving/sending preferences. - `GET /api/workspace/mail-identities/:addressId/delete-preview` is an authenticated, read-only snapshot of the exact-address rule, catch-all target diff --git a/docs/PREVIEW.md b/docs/PREVIEW.md new file mode 100644 index 0000000..b712d7e --- /dev/null +++ b/docs/PREVIEW.md @@ -0,0 +1,57 @@ +# Preview 域名与邮件身份管理 + +使用 Cloudflare 原生 Worker Previews 部署,要求 Wrangler >= 4.135.0。 +已有较新全局 Wrangler 时可直接使用全局命令。仓库本地构建、迁移与测试继续遵循 +`package.json` 的 Bun 和 Wrangler 版本;不要使用本地旧版 Wrangler 执行新 Preview 命令。 + +1. 将 `wrangler.preview.deploy.toml.example` 复制为被忽略的 + `wrangler.preview.deploy.toml`,填写独立 Preview D1/R2 绑定及应用地址。 +2. Preview 专用域名配置为 `enabled=false`、`previews_enabled=true`。 + 地址格式是 `.`,不是 Preview 域名本身。 +3. 使用单独的迁移配置,将顶层 D1 绑定指向与 `previews.d1_databases` 相同的 + Preview 数据库;先列出待应用迁移,再应用并核对版本。禁止指向生产数据库。 +4. 在 Preview 数据库初始化独立 Owner 及本地登录凭据。不要复制生产邮件、 + 凭据或其他生产配置;临时账号信息应保存为权限 `0600` 的本机文件。 +5. 完成 `bun test`、`bun run check`、`bun run build` 和 Chromium 桌面/手机 QA, + 提交后执行: + + ```sh + wrangler preview --config wrangler.preview.deploy.toml --name mail-identities --message "git " + ``` + +6. 核对 Preview 部署 ID、D1/R2 隔离、应用版本、登录、身份管理页面及 + `/api/health`,再确认生产 deployment ID 没有变化。不要执行 `wrangler deploy` + 来发布原生 Preview,它会部署生产。 + +## 从网页接入邮件域名 + +管理员为目标环境配置 `MAIL_IDENTITY_WORKER_NAME` 和 +`MAIL_IDENTITY_ACCOUNT_ID`,并添加限于测试 Zone 的 Cloudflare Token: + +- `CLOUDFLARE_EMAIL_ROUTING_READ_TOKEN`:Zone Read、Email Routing Rules Read。 +- `CLOUDFLARE_EMAIL_ROUTING_TOKEN`:创建/删除规则需要 Email Routing Rules Edit。 + 若不提供单独读取 Token,管理 Token 还需包含 Zone Read 和规则读取权限。 +- `RESEND_API_KEY`:域名发信状态检查需要 Resend 域名读取权限;使用专用测试 Key。 + +向一个原生 Preview 添加 secret 使用: + +```sh +wrangler preview secret put CLOUDFLARE_EMAIL_ROUTING_READ_TOKEN --name mail-identities --config wrangler.preview.deploy.toml +``` + +通过交互式输入传递 Token,不写入配置、命令参数或版本控制。 + +网页中的“添加邮件域名”核验域名及所属 Zone,不自动修改 MX/SPF/DKIM 等 DNS。 +接入后检查域名状态,再创建邮箱地址。缺少 Token 或部署映射时,页面会显示 +配置提示;已有域名/地址的本地设置仍可以编辑。域名设置可启停域名及切换未知 +地址策略,地址可编辑名称和签名;已有的地址启停、删除、恢复及发信操作继续可用。 + +**原生 Preview 的 HTTP 测试不代表真实收信。** Email Routing 与 Cron 目标是 +Worker 的生产 handler,不能选择某个原生 Preview。要测试真实邮件,使用单独的 +接收 Worker,并绑定 Preview D1/R2;`MAIL_IDENTITY_WORKER_NAME` 应指向它。 +不要把 Preview 创建的收信规则指向生产 FlareMail,也不要把 Wrangler OAuth +登录凭据当成应用的长期管理 Token。 + +参考:[Worker Previews](https://developers.cloudflare.com/workers/previews/)、 +[自定义域名](https://developers.cloudflare.com/workers/previews/custom-domains/)、 +[资源隔离](https://developers.cloudflare.com/workers/previews/resources/)。 diff --git a/docs/adr/0014-owner-managed-mail-identities-and-access.md b/docs/adr/0014-owner-managed-mail-identities-and-access.md index 6eb32b3..0bcd41e 100644 --- a/docs/adr/0014-owner-managed-mail-identities-and-access.md +++ b/docs/adr/0014-owner-managed-mail-identities-and-access.md @@ -63,6 +63,15 @@ zone/account and Worker target, plus independent Cloudflare-routing and Resend-sending check state. A subdomain must be configured as its own explicit mapping. Zone visibility from an API token never enrolls a domain. +The Owner may explicitly enroll a domain through the workspace. The deployment +fixes the receiving Worker and account; the server verifies the submitted Zone +ID and domain boundary before inserting a mapping, with unknown recipients +rejected by default. The browser cannot choose the Worker or account, alter an +existing mapping, or supply credentials. Zone Read permission is required. +Domain enable/disable and unknown-recipient settings preserve addresses and mail +history; switching to collect requires fresh verified catch-all targeting this +Worker. Address names and signatures are editable without changing routing. + `mail_addresses` stores a stable address ID, Owner/domain, receive/send switches, lifecycle and routing state, exact remote rule ID, display name, signature, and optional default-sender flag. Creating addresses never creates diff --git a/package.json b/package.json index 58139b9..5be69e8 100644 --- a/package.json +++ b/package.json @@ -13,8 +13,8 @@ "preview": "bun run build && bun ./scripts/wrangler-local.ts dev worker/index.ts --local", "check": "svelte-kit sync && svelte-check --tsconfig ./tsconfig.json", "test": "bun test src scripts", - "test:unit": "bun test src/lib/client src/lib/components/mail/domain-capabilities.test.ts src/lib/components/ui src/lib/domain/mail src/lib/domain/utf8.test.ts src/lib/i18n src/lib/server/auth src/lib/server/config src/lib/server/body.test.ts src/lib/server/cloudflare-email-routing.test.ts src/lib/server/db/attachments.test.ts src/lib/server/db/capabilities.test.ts src/lib/server/db/deliveries.test.ts src/lib/server/db/mail-identities.test.ts src/lib/server/db/messages.test.ts src/lib/server/email.test.ts src/lib/server/inbound/parser.test.ts src/lib/server/mail-identities/check.test.ts src/lib/server/mail-identities/health-refresh.test.ts src/lib/server/mail-identities/routing.test.ts src/lib/server/mail-identities/sending.test.ts src/lib/server/mail-identities/validation.test.ts src/lib/server/mail/html-sanitize.test.ts src/lib/server/outbound/gateway.test.ts src/lib/server/outbound/rate-limit.test.ts src/lib/server/outbound/system.test.ts src/lib/server/resend-domains.test.ts src/lib/server/search/fts.test.ts src/lib/server/workspace/compose-body.test.ts src/lib/server/telegram/api.test.ts src/lib/server/telegram/config.test.ts src/lib/server/telegram/message.test.ts src/lib/server/scheduled.test.ts", - "test:integration": "bun test src/lib/server/email.integration.test.ts src/lib/server/workspace/attachment.integration.test.ts src/lib/server/workspace/outbound.integration.test.ts src/lib/server/workspace/delivery.integration.test.ts src/lib/server/workspace/draft.integration.test.ts src/lib/server/workspace/mailbox.integration.test.ts src/lib/server/workspace/mutation-query-counter.integration.test.ts src/lib/server/workspace/trash.integration.test.ts src/lib/server/db/migrations.test.ts src/lib/server/db/telegram.test.ts src/lib/server/telegram/dispatcher.test.ts src/routes/api/health/health.test.ts src/routes/api/readiness/readiness.test.ts src/routes/api/workspace/mailbox/mutate/server.test.ts src/routes/api/workspace/mailbox/categories/server.test.ts 'src/routes/api/workspace/mail-identities/[addressId]/server.test.ts'", + "test:unit": "bun test src/lib/client src/lib/components/mail/domain-capabilities.test.ts src/lib/components/ui src/lib/domain/mail src/lib/domain/utf8.test.ts src/lib/i18n src/lib/server/auth src/lib/server/config src/lib/server/body.test.ts src/lib/server/cloudflare-email-routing.test.ts src/lib/server/db/attachments.test.ts src/lib/server/db/capabilities.test.ts src/lib/server/db/deliveries.test.ts src/lib/server/db/mail-identities.test.ts src/lib/server/db/messages.test.ts src/lib/server/email.test.ts src/lib/server/inbound/parser.test.ts src/lib/server/mail-identities/check.test.ts src/lib/server/mail-identities/domains.test.ts src/lib/server/mail-identities/health-refresh.test.ts src/lib/server/mail-identities/routing.test.ts src/lib/server/mail-identities/sending.test.ts src/lib/server/mail-identities/validation.test.ts src/lib/server/mail/html-sanitize.test.ts src/lib/server/outbound/gateway.test.ts src/lib/server/outbound/rate-limit.test.ts src/lib/server/outbound/system.test.ts src/lib/server/resend-domains.test.ts src/lib/server/search/fts.test.ts src/lib/server/workspace/compose-body.test.ts src/lib/server/telegram/api.test.ts src/lib/server/telegram/config.test.ts src/lib/server/telegram/message.test.ts src/lib/server/scheduled.test.ts", + "test:integration": "bun test src/lib/server/email.integration.test.ts src/lib/server/workspace/attachment.integration.test.ts src/lib/server/workspace/outbound.integration.test.ts src/lib/server/workspace/delivery.integration.test.ts src/lib/server/workspace/draft.integration.test.ts src/lib/server/workspace/mailbox.integration.test.ts src/lib/server/workspace/mutation-query-counter.integration.test.ts src/lib/server/workspace/trash.integration.test.ts src/lib/server/db/migrations.test.ts src/lib/server/db/telegram.test.ts src/lib/server/telegram/dispatcher.test.ts src/routes/api/health/health.test.ts src/routes/api/readiness/readiness.test.ts src/routes/api/workspace/mail-identities/domains/server.test.ts src/routes/api/workspace/mailbox/mutate/server.test.ts src/routes/api/workspace/mailbox/categories/server.test.ts 'src/routes/api/workspace/mail-identities/[addressId]/server.test.ts'", "test:remaining": "bun test scripts src/hooks.server.test.ts src/lib/server/attachment-download.test.ts src/lib/server/attachment-integrity.test.ts src/lib/server/http/api.test.ts src/lib/server/resend-webhook.test.ts src/lib/server/workspace/mailbox-query.test.ts src/lib/server/workspace/reader.test.ts src/lib/server/workspace/r2-cleanup.test.ts src/lib/server/workspace/session.test.ts src/lib/server/workspace/shared.test.ts src/routes/api/send/server.test.ts src/routes/api/webhooks/resend/server.test.ts src/routes/api/webhooks/telegram/server.test.ts src/routes/api/workspace/notifications/telegram/settings/server.test.ts 'src/routes/api/workspace/messages/[id]/html/html.test.ts' 'src/routes/api/workspace/messages/[id]/server.test.ts' src/routes/api/workspace/session/server.test.ts src/routes/api/workspace/trash/trash.test.ts", "test:e2e": "bun scripts/playwright-projects.ts chromium", "test:e2e:webkit": "bun scripts/playwright-projects.ts webkit", diff --git a/src/lib/components/mail/MailDomainSetup.svelte b/src/lib/components/mail/MailDomainSetup.svelte new file mode 100644 index 0000000..a8bf5a0 --- /dev/null +++ b/src/lib/components/mail/MailDomainSetup.svelte @@ -0,0 +1,58 @@ + + +
+ {t('settings.addMailDomain')} +

{t('settings.domainSetupDescription')}

+ {#if !available} +

{t(workerName ? 'settings.domainReadPermissionMissing' : 'settings.domainWorkerMissing')}

+ + {/if} +
+ domainName = event.currentTarget.value} /> + zoneId = event.currentTarget.value} /> +
+

{t('settings.domainZoneHelp')}

+ {#if errorMessage}{/if} + +
+ + diff --git a/src/lib/components/mail/MailIdentityManager.svelte b/src/lib/components/mail/MailIdentityManager.svelte index 819b24a..3a68691 100644 --- a/src/lib/components/mail/MailIdentityManager.svelte +++ b/src/lib/components/mail/MailIdentityManager.svelte @@ -6,6 +6,7 @@ import TextField from '$lib/components/ui/TextField.svelte'; import TextArea from '$lib/components/ui/TextArea.svelte'; import Dialog from '$lib/components/ui/Dialog.svelte'; + import MailDomainSetup from './MailDomainSetup.svelte'; import { requestJson } from '$lib/client/api'; import { mailHealthState } from '$lib/domain/mail/health'; import { mailSenderSendBlockReason } from '$lib/domain/mail/sender-readiness'; @@ -17,6 +18,8 @@ id: string; domain_name: string; enabled: number; + cloudflare_zone_id: string; + worker_name: string; unknown_recipient_policy: 'reject' | 'collect'; catch_all_target: 'unknown' | 'this_worker' | 'external' | 'drop' | 'none'; resend_status: 'unknown' | 'pending' | 'verified' | 'failed'; @@ -114,6 +117,14 @@ let errorMessage = $state(''); let notice = $state(''); let noticeNeedsAttention = $state(false); + let onboarding = $state<{ available: boolean; workerName: string | null; canManageRouting: boolean }>({ available: false, workerName: null, canManageRouting: false }); + let domainSettingsTarget = $state(null); + let domainEnabled = $state(true); + let unknownPolicy = $state<'reject' | 'collect'>('reject'); + let settingsError = $state(''); + let editAddressTarget = $state(null); + let editDisplayName = $state(''); + let editSignature = $state(''); let deleteTarget = $state(null); let deletePreview = $state(null); let deletePreviewLoading = $state(false); @@ -153,7 +164,9 @@ domains: Omit[]; addresses: MailAddress[]; providerConfiguration: { cloudflare: boolean; resend: boolean }; + domainOnboarding?: typeof onboarding; }>('/api/workspace/mail-identities'); + onboarding = loaded.domainOnboarding ?? { available: false, workerName: null, canManageRouting: false }; domains = loaded.domains.map((domain) => ({ ...domain, cloudflare_configured: loaded.providerConfiguration.cloudflare, @@ -202,6 +215,61 @@ void load(); }); + async function domainCreated(domainId: string) { + selectedDomainId = domainId; + notice = t('settings.mailDomainConnected'); + noticeNeedsAttention = false; + await load(); + } + + function openDomainSettings(domain: MailDomain) { + domainSettingsTarget = domain; + domainEnabled = Boolean(domain.enabled); + unknownPolicy = domain.unknown_recipient_policy; + settingsError = ''; + } + + async function saveDomainSettings(event: SubmitEvent) { + event.preventDefault(); + if (!domainSettingsTarget || pendingAction) return; + pendingAction = 'domain-settings'; + settingsError = ''; + try { + await requestJson(`/api/workspace/mail-identities/domains/${encodeURIComponent(domainSettingsTarget.id)}`, { + method: 'PATCH', body: JSON.stringify({ enabled: domainEnabled, unknownRecipientPolicy: unknownPolicy }) + }); + domainSettingsTarget = null; + notice = t('settings.domainSettingsSaved'); + noticeNeedsAttention = false; + await load(); + } catch (error) { settingsError = error instanceof Error ? error.message : t('settings.mailIdentityActionFailed'); } + finally { pendingAction = ''; } + } + + function openAddressSettings(address: MailAddress) { + editAddressTarget = address; + editDisplayName = address.display_name; + editSignature = address.signature; + settingsError = ''; + } + + async function saveAddressSettings(event: SubmitEvent) { + event.preventDefault(); + if (!editAddressTarget || pendingAction) return; + pendingAction = 'address-settings'; + settingsError = ''; + try { + await requestJson(`/api/workspace/mail-identities/${encodeURIComponent(editAddressTarget.id)}`, { + method: 'PATCH', body: JSON.stringify({ displayName: editDisplayName, signature: editSignature }) + }); + editAddressTarget = null; + notice = t('settings.addressSettingsSaved'); + noticeNeedsAttention = false; + await load(); + } catch (error) { settingsError = error instanceof Error ? error.message : t('settings.mailIdentityActionFailed'); } + finally { pendingAction = ''; } + } + function checkFor(domain: MailDomain, addressId: string): RouteStatus | undefined { return checks[domain.id]?.cloudflare.addresses.find((entry) => entry.addressId === addressId)?.status; } @@ -565,10 +633,16 @@ {#if loading}

{t('common.loading')}

- {:else if domains.length === 0} -

{t('settings.noMailDomains')}

-

{t('settings.mailDomainSetupHint')} bun run mail:domain:configure

{:else} + {#if view === 'domains' || domains.length === 0} + + {/if} + {#if !onboarding.canManageRouting} +

{t('settings.routingManagementMissing')}

+ {/if} + {#if domains.length === 0} +

{t('settings.noMailDomains')}

+ {:else} {#if view === 'addresses'}
@@ -600,6 +674,7 @@
{#if view === 'domains'} +
{#if view === 'addresses'}
+ {#if address.lifecycle_status !== 'deleted'} + + {/if} {#if route === 'importable' && address.lifecycle_status !== 'deleted'} {/if} @@ -716,9 +794,39 @@ {/each}
+ {/if} {/if} +{#if domainSettingsTarget} + domainSettingsTarget = null}> + +

{t('settings.domainMappingLocked')}

+ +

{t('settings.domainDisableHelp')}

+ + +

{t('settings.collectRequiresWorkerCatchAll')}

+ {#if settingsError}{/if} + + +
+{/if} + +{#if editAddressTarget} + editAddressTarget = null}> +
+ editDisplayName = event.currentTarget.value} /> +