Major Changes
-
#1353
c8c0423Thanks @juandav! - v4: everything the official@nestjs/mailoffers and more, without dropping anything v3 supports. Every capability is optional: use nodemailer, the built-in HTTP providers or@nestjs/mail's transports; send directly, through BullMQ or through@nestjs/outbox; callsendMail, mail classes or the officialMailerover this module.Breaking changes (see the migration guide):
- Transient failures are retried by default (3 attempts with backoff);
retry: falserestores v3. A timeout or a dropped connection while the message was being handed over is retried only when the email has a stable Message-ID (idempotencyKey), so nothing is sent twice.sendTimeoutapplies per attempt. - Errors from transports are typed (
MailerSmtpError,MailerTimeoutError, … allMailerError), keeping nodemailer's fields and the original ascause;wrapErrors: falserestores v3. - Emails without a text part get one derived from the HTML (
textFromHtml: falseto opt out), and messages are validated before sending (validate: falseto opt out). MailerTestModuledelivers to aTestMailbox(no stream transport;info.messageuses CRLF line endings) and defaultsretrytofalse.MailerPreviewModuleis on by default only whenNODE_ENVisdevelopmentortest.- Event payloads carry
attempt,permanent,transporterNameanddurationMs;MailerEventServicetakes its emitter throughMAILER_EVENT_EMITTER, andEventEmitter2is found automatically (in v3 it was never injected). MailableContext.tois{ name?, address }[], the same shape as@nestjs/mail.
New
- Mail classes (
mailerService.send(WelcomeMail, { to, data })) compatible with@nestjs/mail's, with dependencies resolved from the whole app, andrender()to render without sending. - Retries with backoff and error classification (nodemailer, HTTP and AWS SDK errors), failover between transporters,
AbortSignal,idempotencyKey, and graceful shutdown that waits for sends in flight (shutdownTimeout). - Zero-dependency HTTP transports: Resend, Postmark, SendGrid, SES (SigV4), Mailgun and Brevo.
- The
htmltagged template (unsafeHtml()for trusted markup),htmlToText()and message validation. intercept(redirect recipients in staging),suppress(suppression lists) and one-clickunsubscribe(RFC 8058).TestMailboxwith assertions,FileTransportandLogTransport(with a warning when used in production) andMailerPreviewModuleto preview emails in the browser.events$,node:diagnostics_channelpublishing (credentials redacted), andRETRY,FAILOVERandSUPPRESSEDevents.- BullMQ: permanent failures fail the job at once, a stable idempotency key per job,
schedule()andconcurrency. @nestjs-modules/mailer/nestjs-mail:NodemailerMailTransport,MailerServiceTransportandTemplateAdapterEnginefor@nestjs/mail.@nestjs-modules/mailer/outbox: transactional sending with@nestjs/outbox.- i18n falls back from a region to its language (
es-CO→es) and searches every template directory. - Tested on NestJS 11 and 12.
Fixed
- Text-only emails no longer fail when a template adapter is configured.
interceptandsuppresscover the whole custom envelope (to,ccandbcc).
- Transient failures are retried by default (3 attempts with backoff);