Skip to main content
A GatoJS app sends three kinds of transactional email: password reset, welcome with a set-password link, and organization invitation. All three go through one private function, sendTransactionalEmail in src/shared/lib/mailer.ts, which picks the transport based on your environment variables.

Choosing the transport

src/shared/lib/mailer.ts
An empty value counts as unset, because the env schema turns blank strings into undefined.
SMTP_URL wins over Resend, also in production. If both are set, mail goes through SMTP. In production, leave SMTP_URL empty unless you deliberately send through your own SMTP server.
Behavior shared by both transports:
  • Failures don’t throw. An exception (for example ECONNREFUSED when Mailpit isn’t running) or an error returned by Resend is logged with logger.error and returned as { error: <message> }.
  • Addresses are masked in logs. maskEmail keeps the first two characters of the local part: [email protected] becomes ja**********@example.com.
  • HTML only. Messages have no plain-text part. The sender is EMAIL_FROM.
  • Clients are created once per process, and env is read once. Restart the server after changing SMTP_URL or RESEND_API_KEY.

Environment variables

string
required
Sender address, for example Acme <[email protected]>. Required in every environment. The format is not validated, so check it against the domain you verified in your mail provider.
string
Resend API key. Optional in development; required when NODE_ENV=production, even if SMTP_URL is set. Without it the server refuses to start with RESEND_API_KEY: wymagana w produkcji (“required in production”).
string
SMTP connection URL for nodemailer, for example smtp://localhost:1025 or smtps://user:[email protected]:465. Takes precedence over Resend. The format is not validated: a malformed URL passes startup and fails on the first send.
See Environment variables for the full reference.

Local preview with Mailpit

docker-compose.yml runs Mailpit as the mail service, and .env.example sets SMTP_URL=smtp://localhost:1025. Every email the app sends locally lands in Mailpit, with the web UI at http://localhost:8025.
If the ports are taken, change MAILPIT_UI_PORT and MAILPIT_SMTP_PORT (defaults 8025 and 1025) and update SMTP_URL in .env to match. There is no separate template preview server. To see a template after a change, trigger the action that sends it: The end-to-end tests read mail through the Mailpit API as well (see Testing).

Templates

Templates are React components built with React Email, in src/app/emails/. The shared EmailLayout (src/app/emails/components/email-layout.tsx) provides the inbox preview text, a header and footer with project.name, and Tailwind styles. Links are built from APP_URL, so a wrong APP_URL in production sends people to the wrong host.
Templates and subjects are written in Polish and don’t receive a locale, so English-speaking users get the same Polish email. To localize them, edit the templates and subjects in the core files. See Translations.

Editing a template

Templates are ordinary components. Change the copy or layout directly:
src/app/emails/password-reset-email.tsx

Adding a new kind of email

sendTransactionalEmail is not exported. A new email needs a template in src/app/emails/ and a new exported function in mailer.ts, built the same way as the existing three. Both files belong to the core module, so this is a local change to the core in your project; a kit module has no way to send its own email without editing the core.

How results are handled

  • Password reset and organization invitation are sent by better-auth, which ignores the result. The user sees success even when the email didn’t go out; the failure appears only in logs.
  • Welcome email: on { error }, inviteUserCommand still returns success but with warning: "mail-failed", and the invite dialog shows a warning asking you to send a password reset instead. With neither transport configured the result is { data: null }, not an error, so the invite succeeds without a warning even though nothing was sent.

Production checklist

1

Verify a sender domain

Add and verify your domain in Resend (or your SMTP provider), then set EMAIL_FROM to an address on it.
2

Set the API key

Set RESEND_API_KEY and leave SMTP_URL unset.
3

Set up log export

Set OTEL_EXPORTER_OTLP_ENDPOINT. In production the logger doesn’t write to the console, so without an OTLP endpoint failed sends leave no trace. See Logging and observability.
4

Send a test email

After deploying, request a password reset for your own account and confirm it arrives.

Auth and roles

Invitations, sign-up and password flows that trigger these emails.

Deploy on Railway

Production variables, including mail settings.