Skip to content

Saving the new user email template discards unsaved mail server settings #1666

Open

Description

@joshunrau

Saving the New User Email Template on /admin/mail throws away any unsaved edits in the Mail Server Configuration form above it. Both sections save through the same save helper in MailSettings. After any successful PATCH, that helper resets the server form from the response with setValues(initialValues(next.config)), including when the PATCH only carried newUserEmailTemplate. Host, port, username, a newly typed password and sender fields all go back to their stored values while a success toast is shown. On first-time setup (config is null) it is worse: initialValues(null) sets enabled: false, so the whole server form disappears and everything the admin typed into it is gone.

Where

apps/web/src/components/MailSettings/MailSettings.tsx:126-132:

const save = async (data: Parameters<typeof updateMutation.mutateAsync>[0]) => {
  try {
    const next = await updateMutation.mutateAsync(data);
    // Re-seed from what the server actually stored, ...
    setValues(initialValues(next.config));
    addNotification({ type: 'success' });

MailSettings.tsx:204-208: the template section saves through the same helper:

<NewUserTemplateSection
  isSaving={updateMutation.isPending}
  template={newUserEmailTemplate}
  onSave={(next) => void save({ newUserEmailTemplate: next })}
/>

Reproduce

  1. Sign in as an admin on an instance with a saved mail configuration and open /admin/mail.
  2. Change Host to smtp2.example.org and do not click the server form's Save.
  3. Edit the New User Email Template and click its Save.

Actual: the success toast shows, and Host is back to the stored value. The edit is gone without warning.
Expected: saving the template leaves the server form exactly as the admin left it.

Variant: on an instance with no mail configuration, tick "Enable email sending", fill in the server fields, then save the template. The server form collapses and every field typed into it is lost.

Tests

apps/web/src/components/MailSettings/__tests__/MailSettings.test.tsx:

  • it('should keep unsaved server edits when the template is saved, so one section cannot discard the other')
  • it('should keep the server form open when the template is saved before any configuration exists')

testing/src/specs/mail.spec.ts: test('should keep an unsaved host edit after saving the welcome template').

Suggested fix

Re-seed values only when the payload included config. Either pass a flag to save, or move the setValues call into handleSaveConfig and handleToggleEnabled, so a template-only save never touches the server form state.

Activity

  1. added
    Bug
    Priority: Medium
    Difficulty: Low
    Area: Web
    on Oct 7, 2026
  2. joshunrau commented on Oct 26, 2026

    @joshunrau
    CollaboratorAuthor

    Still reproduces on main (d86740e). The cited lines have not moved.

    The variant is wider than first-time setup

    initialValues takes enabled from the stored config (MailSettings.tsx:23), and the template section only renders under values.enabled (MailSettings.tsx:191). Ticking "Enable email sending" never saves (MailSettings.tsx:160). So the collapse happens on any instance where mail is configured but switched off, not only when config is null. There, every template save hides the card that was just saved, even with no server edits. The e2e mail settings block runs in this state: its beforeAll calls api.setMailEnabled(false) (mail.spec.ts:17), which stores E2E_MAIL_CONFIG with enabled: false.

    Fix

    Decide from the payload. data.config already says whether the server form was saved, so a separate flag would be a second source for the same fact:

    const next = await updateMutation.mutateAsync(data);
    if (data.config) {
      setValues(initialValues(next.config));
    }

    This keeps the re-seed for handleSaveConfig and for the disable in handleToggleEnabled, the only other callers of save. The other direction is already safe: NewUserTemplateSection seeds its state once (NewUserTemplateSection.tsx:28-31) and never re-reads the template prop, so a server save cannot discard template edits.

    Tests

    Separate bug in the same helper

    handleToggleEnabled sets enabled to false locally before the PATCH (MailSettings.tsx:157), and the catch in save never puts it back. When a disable fails, the checkbox stays unticked and the form stays hidden while mail is still on server-side. No issue tracks this. It needs its own issue rather than a fix in this one.

    Triage note written with Claude Code. Code references point at main @ d86740e.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area: WebBugDifficulty: LowPriority: Medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions