Repository navigation
Conversation
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.
|
@AirLoren is attempting to deploy a commit to the Basic Intelligence Team on Vercel. A member of the Team first needs to authorize it. |
[Medium impact] The PR appears safe to merge; the separate migration fixes upgrades for existing SMTP tables. SummaryAdds SMTP email delivery with TLS, attachments, saved delivery keys, and SMTP-specific setup text.
Reviews (4) · Last reviewed commit: "Add the idempotency key state in a migra..." · Reviewed by Greptile |
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.
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
left a comment
There was a problem hiding this comment.
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
createDatabaseIdempotencyStorepasses the key as$1. The only interpolated values are the module constantsIDEMPOTENCY_WINDOWandSENDING_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_keystable. No existing table or row is touched, anddb/migrate.mjsruns 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 withSMTP_SECURE=false. Certificates are verified (notlsoverride). - Secrets. Nodemailer's logger is off and its auth errors mask the credentials. The settings page and
GET /api/admin/emailexpose onlySMTP_HOST, behindrequireAdminSession. - Dependencies.
npm audit --omit=devreports 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-001atsrc/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-005at.env.example:7is the local developmentDATABASE_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 defaultsocketTimeoutis 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 onclaimed_atin theDELETEwould 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.
Requested by Romain · project thread
What
Self-hosted installs can send email through their own SMTP server instead of Resend:
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 setsrequireTLS, so the password is never sent in the clear. The transport can be injected (smtpTransport) for tests, likeresendClient.src/lib/email-status.tsand the settings page: SMTP has its ownsmtpstatus (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.email_idempotency_keystable (migrations20261123100000_smtp_email_idempotency.sqland20261123110000_smtp_email_idempotency_state.sql) for 24 hours, so a notice whose follow-up write failed is not delivered twice. A key stayssendinguntil the server accepts the message, then becomessent: a retry skips onlysentkeys, takes over asendingkey after a 10-minute lease (interrupted attempt), and fails while another attempt may still be sending. A failed send releases its key.nodemailerand@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..env.exampleand a new "Email over SMTP" section indocs/self-hosting.md.The Resend and capture paths are unchanged.
Tests
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.npm run test:db(no local Docker here).npm run typecheck,npm run lint,npm run format:checkpass.npm test: the new tests pass; 13 tests insrc/lib/update-workflow.test.tsfail the same way onmainon my machine (macOS), unrelated to this change.Note
Add an SMTP email driver backed by Nodemailer with database idempotency
EMAIL_DRIVERsetting in email.ts. Uses Nodemailer for sending single messages and sequential batches, including attachments, sender, and reply-to addresses.smtp_email_idempotencytable with a non-nullstatecolumn constrained tosendingandsent; SMTP driver selection changescreateEmailServicebehavior when SMTP is configured.Macroscope summarized d2b2e9a.