Pular para o conteúdo principal
Versão: main (dev) 🚧

ADR-028 — Per-tenant e-mail sender

Date: 2026-11-16 Status: Accepted Extends: ADR-023 (hierarchical settings). Library side: DevPack ADR-009 (e-mail contracts; senders live in the consumer), DevPack 10.28.0 SettingDefinition.ConfigurationKey. Origin: Azure DevOps US 10334; plan docs/plans/2026-11-16-feat-remetente-email-por-tenant-plano.md.

Context​

Every e-mail left from EmailOptions:DefaultFromAddress/DefaultSenderName, one sender for the whole deployment. A customer (tenant) could not send invitations and notices under its own identity. Two flaws sat next to it: the invitation carried the identity of the tenant the inviting administrator was acting in — the system administrator creating a user only in tenant B sent it as the default tenant — and the password reset carried no tenant at all.

Decision​

The sender is three settings in EmailSettingDefinitions: Email.Sender.Address, Email.Sender.Name and Email.ReplyTo (levels Global and Tenant, EditableBy = TenantAdmin, group email). The first two read their configuration layer from the existing EmailOptions keys (ConfigurationKey), so a deployment without overrides sends exactly as before and the settings screens show the value with origin Servidor. Email.ReplyTo has no configuration layer; empty means no header. The provider, host, credentials and API key stay infrastructure (appsettings/Key Vault).

  • The send names the tenant; nothing reads it from the ambient context. IEmailSenderResolver reads the three settings with an explicit SettingContext — Tenant(id) for SendFromTemplateAsync(tenantId), Global for a null tenant and for the SendAsync overloads, which take none. The ambient tenant is exactly what made the invitation wrong, and the Worker has none.
  • The account's own mail carries its invitation tenant. AppUser.InvitationTenantId records the tenant the account was created in — the creator's active tenant when the account joins it, otherwise the smallest tenant it joins. UserMailTenant resolves the tenant for the invitation and the password reset: the invitation tenant while the user is still a member there and the tenant is active; else the oldest membership in an active tenant (accounts created before the column, the seeded admin, one that left or whose tenant was deactivated); else none — the platform. A resend does not rewrite it. The tenant also selects the per-tenant templates, so the reset now uses them too.
  • One validation, wherever the sender is written. A bare address (no Name <address> form, no blanks) and a single-line name of at most 100 characters. The same rules guard EmailOptions at boot (ValidateOnStart): a value the settings would refuse stops the host naming the key, instead of the configuration layer silently falling back and every message leaving from the placeholder. A stored value that stops being valid falls back to the next layer (FallBackToNextLayer), logged and listed in the settings health tab.
  • The defaults are the shipped placeholders (no-reply@example.com, Template). The DevPack catalog holds a default to its definition's rules, and an empty default would make an empty override valid — a tenant with no sender. The resolver never sends from them: an address or name that comes from the default layer fails the send with Missing configuration, as the providers did before. A blank EmailOptions therefore passes the boot check (a host that sends no e-mail needs no sender) and fails each send, until the platform or the tenant sets a sender.

Consequences​

  • A tenant administrator sets the sender of their tenant in the settings screens; nobody writes code or a new screen.
  • The address must be one the provider accepts for that domain (SendGrid sender authentication; SPF/DKIM/DMARC and the relay's MAIL FROM policy for SMTP). The application does not check it when the override is written: a refused send shows as inviteSent = false on the invitation and only in the log for the reset (which never reveals an account). An open SMTP relay lets a tenant use a domain that is not its own — restrict it at the relay (runbook 04).
  • EmailOptions:DefaultFromAddress in the Name <address> form, accepted before, now stops the host. Runbook 02 says to check each environment before the deploy.
  • A new override reaches other replicas within the settings cache window (30 s).

Alternatives considered​

  • Copy EmailOptions:* to Settings:* at boot and turn on the global configuration layer. No DevPack change, but every definition would start consulting the configuration. ConfigurationKey (DevPack 10.28.0) points one definition at an existing key instead.
  • Fall back to EmailOptions inside the sender when no override exists. The screens would show origin Padrão with an empty value instead of the server's.
  • Derive the invitation tenant without a column (the creator's tenant, else the oldest membership), or reuse the sign-in rule (host tenant, else the smallest tenant id). Neither says which tenant the account was invited in once it belongs to several; recording it does, and the fallback still covers accounts that predate it.
  • EditableBy = System. Safer against spoofing through an open relay, but the story asks for the tenant's own administrator; the risk is handled at the relay and documented.