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.- Failures don’t throw. An exception (for example
ECONNREFUSEDwhen Mailpit isn’t running) or an error returned by Resend is logged withlogger.errorand returned as{ error: <message> }. - Addresses are masked in logs.
maskEmailkeeps the first two characters of the local part:jan.kowalski@example.combecomesja**********@example.com. - HTML only. Messages have no plain-text part. The sender is
EMAIL_FROM. - Clients are created once per process, and
envis read once. Restart the server after changingSMTP_URLorRESEND_API_KEY.
Environment variables
Sender address, for example
Acme <noreply@acme.com>. Required in every environment. The format is not validated, so check it against the domain you verified in your mail provider.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”).SMTP connection URL for nodemailer, for example
smtp://localhost:1025 or smtps://user:pass@smtp.example.com:465. Takes precedence over Resend. The format is not validated: a malformed URL passes startup and fails on the first send.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.
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, insrc/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.
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 },inviteUserCommandstill returns success but withwarning: "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.
Related
Auth and roles
Invitations, sign-up and password flows that trigger these emails.
Deploy on Railway
Production variables, including mail settings.