Skip to content

Add an SMTP email driver - #253

Open
AirLoren wants to merge 4 commits into
NYLLON-SOFTWARE:mainfrom
AirLoren:feat/smtp-email-driver
Open

AirLoren wants to merge 4 commits into
NYLLON-SOFTWARE:mainfrom
AirLoren:feat/smtp-email-driver

Conversation

@AirLoren

@AirLoren AirLoren commented Nov 22, 2026 •

Copy link
Copy Markdown

Requested by Romain · project thread

What

Self-hosted installs can send email through their own SMTP server instead of Resend:

EMAIL_DRIVER=smtp
SMTP_HOST=smtp.example.com
SMTP_PORT=465        # default; other ports must offer STARTTLS
SMTP_USER=sign@example.com
SMTP_PASSWORD=...
# SMTP_SECURE=true|false overrides the TLS choice made from the port

Why

Off Vercel, Resend was the only way to send mail, so an operator who already has a mail host (or a relay such as Postmark, Mailgun or SES) needed a Resend account just for SignuDoc. The self-hosting docs and #82 both call out "There is no SMTP option".

How

  • src/lib/services/email.ts: a third driver, smtp, built on nodemailer. It uses the same sender and reply-to resolution as Resend (RESEND_FROM_EMAIL, then Settings, Organization), sends attachments, and sends batches one message at a time. Port 465 uses implicit TLS; any other port sets requireTLS, so the password is never sent in the clear. The transport can be injected (smtpTransport) for tests, like resendClient.
  • src/lib/email-status.ts and the settings page: SMTP has its own smtp status (host, sender address or null), with its own text: mail goes through your SMTP server, SPF/DKIM/DMARC are set up with your mail host, "Send me a test email" checks delivery. No Resend button or DNS records are shown. Email counts as set up (clearing the "email needs attention" banner) once a sender at a real domain is chosen.
  • Idempotency: Resend refuses a repeated idempotency key for 24 hours; SMTP servers do not. Keyed SMTP sends claim their key in a new email_idempotency_keys table (migrations 20261123100000_smtp_email_idempotency.sql and 20261123110000_smtp_email_idempotency_state.sql) for 24 hours, so a notice whose follow-up write failed is not delivered twice. A key stays sending until the server accepts the message, then becomes sent: a retry skips only sent keys, takes over a sending key after a 10-minute lease (interrupted attempt), and fails while another attempt may still be sending. A failed send releases its key.
  • Dependencies: nodemailer and @types/nodemailer. I know CONTRIBUTING asks to keep dependencies minimal; SMTP with TLS, STARTTLS, auth and MIME attachments is more than a few lines to get right, and nodemailer has no runtime dependencies.
  • Docs: .env.example and a new "Email over SMTP" section in docs/self-hosting.md.

The Resend and capture paths are unchanged.

Tests

  • New unit tests: driver selection, required settings (canSendEmail), TLS/STARTTLS selection from the port, a send and a batch through a fake transport, idempotency keys (repeat, failed send, batch, send still in flight), and the SMTP email status.
  • Both migrations applied on Postgres 18.7, from empty and from the first one with an existing key, and rerun safely; the claim query checked there for a new key, a key in flight, an interrupted key past its lease, and a sent key.
  • Not run: npm run test:db (no local Docker here).
  • npm run typecheck, npm run lint, npm run format:check pass.
  • npm test: the new tests pass; 13 tests in src/lib/update-workflow.test.ts fail the same way on main on my machine (macOS), unrelated to this change.
  • An earlier revision of this driver (same sending code, no injectable transport) runs on a self-hosted install (Podman, Postgres 18, Caddy) sending through Infomaniak on port 465: the SMTP login and "Send a test email" work.
  • Not tested: a STARTTLS server on port 587.

Note

Add an SMTP email driver backed by Nodemailer with database idempotency

  • Adds SMTP as an email driver alongside Resend, selected via the EMAIL_DRIVER setting in email.ts. Uses Nodemailer for sending single messages and sequential batches, including attachments, sender, and reply-to addresses.
  • Config comes from SMTP environment variables in .env.example. Port defaults to 465 with implicit TLS; other ports require STARTTLS unless secure mode is overridden. Invalid ports are rejected.
  • Keyed sends are deduplicated through a new database idempotency store (migration). Keys persist for 24 hours, sending claims can be reclaimed after a 10-minute lease, and failed sends release their key for retry.
  • Email status inspection and organization settings now report SMTP-specific status and readiness based on a configured sender address (email-status.ts).
  • Documents configuration, TLS requirements, and idempotency behavior in self-hosting.md.
  • Risk: two new migrations add the smtp_email_idempotency table with a non-null state column constrained to sending and sent; SMTP driver selection changes createEmailService behavior when SMTP is configured.

Macroscope summarized d2b2e9a.

Self-hosted installs can now send through their own SMTP server instead
of Resend: EMAIL_DRIVER=smtp with SMTP_HOST, SMTP_PORT, SMTP_USER,
SMTP_PASSWORD and SMTP_SECURE. Port 465 uses TLS from the start; other
ports must upgrade with STARTTLS before the password is sent.

The sender address is resolved as for Resend (RESEND_FROM_EMAIL, then the
organization settings). Settings shows email as ready once a sender at a
real domain is set, since there is no provider API to ask; "Send a test
email" checks delivery.
@vercel

vercel Bot commented Nov 22, 2026

Copy link
Copy Markdown

@AirLoren is attempting to deploy a commit to the Basic Intelligence Team on Vercel.

A member of the Team first needs to authorize it.

@greptile-apps

greptile-apps Bot commented Nov 22, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the separate migration fixes upgrades for existing SMTP tables.

Summary

Adds SMTP email delivery with TLS, attachments, saved delivery keys, and SMTP-specific setup text.

  • Self-hosted installs can send mail through their SMTP server.
  • Organization settings show whether SMTP email is ready.

Reviews (4) · Last reviewed commit: "Add the idempotency key state in a migra..." · Reviewed by Greptile

Comment thread src/lib/services/email.ts Resolved
Comment thread src/lib/email-status.ts Resolved
Address review: the settings page showed Resend setup text for SMTP
installs, and the SMTP driver ignored idempotency keys. SMTP now has an
'smtp' status with its own text, and keyed sends are claimed in a new
email_idempotency_keys table (24-hour window, as with Resend) so a retry
is not delivered twice; a failed send releases its key.
Comment thread src/lib/services/email.ts Resolved
Address review: a key saved before an interrupted send was taken as proof
of delivery, so a retry could report a warning as sent without sending it.
Keys are now 'sending' until the server accepts the message, then 'sent'.
A retry skips only 'sent' keys, takes over a 'sending' key after a
10-minute lease (the earlier attempt was interrupted), and fails while
another attempt may still be sending, so callers retry instead of
recording a message that never went out.
Address review: the state column had been added to the already published
20261123100000 migration, which a database that ran its first copy would
skip. That file is back to its published content, and the column and its
check go in 20261123110000, as the repository asks for published
migrations.

@adryserage adryserage left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found no exploitable security issue in this PR. We also ran it on port 587 with STARTTLS, which the description lists as untested, and it works: with EMAIL_DRIVER=smtp and SMTP_PORT=587 against a STARTTLS relay, "Send me a test email" in Settings delivered the message from the sender address set there. Since the driver sets requireTLS on that port, the delivery also shows the STARTTLS upgrade succeeded.

What I checked:

  • SQL. Every query in createDatabaseIdempotencyStore passes the key as $1. The only interpolated values are the module constants IDEMPOTENCY_WINDOW and SENDING_LEASE, and the keys are built by the server (completion/<envelopeId>/<i>, expiry-notice/...), never by a user.
  • Migrations. Both only create and extend the new email_idempotency_keys table. No existing table or row is touched, and db/migrate.mjs runs each file in its own transaction.
  • Header injection. Subject, recipient and Reply-To go through nodemailer 10.6.1, which folds CR/LF out of header values and rejects them in the SMTP envelope. Addresses are validated with z.string().email(), so a signer address cannot smuggle in a second recipient.
  • TLS. Port 465 uses implicit TLS. Every other port sets requireTLS, so a server that refuses STARTTLS fails the send instead of falling back to plain text, even with SMTP_SECURE=false. Certificates are verified (no tls override).
  • Secrets. Nodemailer's logger is off and its auth errors mask the credentials. The settings page and GET /api/admin/email expose only SMTP_HOST, behind requireAdminSession.
  • Dependencies. npm audit --omit=dev reports 0 vulnerabilities, OSV has no advisory for nodemailer 10.6.1, and nodemailer has no dependencies of its own.

We also ran the deterministic checks of Aetheris 5.0.2 (security, dependency, privacy, type safety, performance and API agents) on the changed files. It reported two security hits, both false positives:

  • SEC-001 at src/lib/services/email.ts:330 ("SQL built by concatenation") is the interpolated interval constant above. Passing it as a parameter (now() - $2::interval) would keep scanners quiet and costs nothing.
  • SEC-005 at .env.example:7 is the local development DATABASE_URL.

Two small notes, neither a security issue:

  • release() deletes the key without checking which attempt holds it. If an attempt stalls past the 10-minute lease (nodemailer's default socketTimeout is also 10 minutes), a second attempt takes the key and sends, then the first one fails and deletes the row, so a later retry could send a duplicate. Matching on claimed_at in the DELETE would close that.
  • The SMTP status counts email as set up as soon as a sender address exists, so a wrong host or password only shows up when someone sends a test email. #269 builds on this PR: it adds a connection check (transport.verify()) and lets an administrator enter the SMTP settings in Settings, Organization when they aren't set in the environment.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants