@wisemen/nestjs-mail
v3.0.0
Published
Reusable NestJS mail infrastructure for Wisemen services.
Maintainers
Keywords
Readme
@wisemen/nestjs-mail
Reusable NestJS mail infrastructure for Wisemen services.
What it provides
MailModule.forRoot()andMailModule.forRootAsync()for DI-friendly mail client wiringHandlebarsRendererintegration through@wisemen/nestjs-handlebars- Mail providers for MailPit, Scaleway, SendGrid, and Mailgun
- Named sender support (
fromas a string or{ email, name? }) - Application-defined delivery metadata
- Delivery success and failure hooks without replacing packaged clients or queue handlers
- Shared mail errors, enums, and message types
Usage
import { Module } from '@nestjs/common'
import { MailModule } from '@wisemen/nestjs-mail'
@Module({
imports: [
MailModule.forRoot({
templateRootPath: process.cwd() + '/dist/src/modules',
client: {
type: 'mailpit',
url: 'http://127.0.0.1:8025',
defaultFrom: {
email: '[email protected]',
name: 'Example Team'
}
},
queueName: 'mail'
})
]
})
export class AppModule {}Project-specific policy belongs outside the package. In larger apps, use forRootAsync() and resolve the client options from environment variables, feature flags, secrets, or test doubles in your own factory.
Install @wisemen/nestjs-handlebars alongside this package when you want to inject HandlebarsRenderer for template-based mail rendering.
Named senders and metadata
Both configured default senders and per-message from values accept an email
string or an object with an optional display name. metadata is opaque
application context exposed to delivery hooks; it is never sent to MailPit,
Scaleway, or SendGrid.
await mailClient.sendMail({
from: {
email: '[email protected]',
name: 'Example Tickets'
},
to: '[email protected]',
subject: 'Your tickets',
html: '<p>Your tickets are ready.</p>',
metadata: {
deliveryId: 'delivery-123',
kind: 'ticket-confirmation'
}
})Metadata placed in SendHtmlMailJobData or SendTemplateMailJobData must be
JSON-serializable because it passes through the queue.
Mailgun
MailModule.forRoot({
client: {
type: 'mailgun',
domain: 'mail.example.com',
apiKey: 'key-...',
defaultFrom: {
email: '[email protected]',
name: 'Example Team'
},
// Use 'https://api.eu.mailgun.net' for EU-region domains. Defaults to 'https://api.mailgun.net'.
baseUrl: 'https://api.eu.mailgun.net'
},
queueName: 'mail'
})Delivery hooks
Configure one hooks object on the module. Hooks receive the configured client
type, the original send options (including metadata), and either the exact
SentMail[] result or the original provider error.
MailModule.forRoot({
client: {
type: 'mailpit',
url: 'http://127.0.0.1:8025'
},
queueName: 'mail',
deliveryHooks: {
async onSuccess({ clientType, options, sentMails }) {
await deliveries.markSent({
clientType,
metadata: options.metadata,
providerIds: sentMails.map((mail) => mail.id)
})
},
async onFailure({ clientType, options, error }) {
await deliveries.markFailed({ clientType, metadata: options.metadata, error })
}
}
})Hook failures log only the hook phase, client type, and error stack. Recipients, bodies, subjects, and metadata are never added to the log. Hook failures never turn a successful provider delivery into a failed send or replace a provider error, so they cannot trigger a duplicate-email queue retry.
