AdonisJS Mail Channel
The Mail channel allows you to send email notifications through the AdonisJS Mail service. This channel integrates seamlessly with your existing AdonisJS mail configuration and supports all mail drivers (SMTP, Mailgun, Resend, etc.).
Batching
This channel does not support batching. Each email is sent individually.
Configuration
import { defineConfig, channels } from '@facteurjs/adonisjs'
export default defineConfig({
channels: {
mail: channels.mail(),
},
})
Configuration Options
channels.mail() resolves your AdonisJS Mail service automatically. The lower-level mailChannel({ mailer }) factory from @facteurjs/adonisjs/channels/mail accepts a mail service instance for manual core configuration.
The mail channel uses your existing AdonisJS mail configuration. Make sure you have @adonisjs/mail configured in your application.
Targets
The Mail channel requires an email address as the target:
await facteur
.notification(MyNotification)
.via({
mail: {
email: 'user@example.com',
},
})
.send()
Target Properties
email(required): The recipient's email address
Message Features
You can create mail notifications in two ways:
Using MailMessage (Simple)
import { Notification } from '@facteurjs/core/types'
import { MailMessage } from '@facteurjs/adonisjs/channels/mail'
export default class OrderShippedNotification extends Notification<
undefined,
{ orderId: number; trackingNumber: string }
> {
asMailMessage() {
return MailMessage.create()
.from('noreply@example.com')
.subject('Your order has been shipped!')
.htmlView('emails/order_shipped', {
orderId: this.params.orderId,
trackingNumber: this.params.trackingNumber,
})
}
}
Using BaseMail (Full AdonisJS Mail Features)
For more complex emails, you can use AdonisJS Mail classes:
import { BaseMail } from '@adonisjs/mail'
import { Notification } from '@facteurjs/core/types'
class OrderShippedMail extends BaseMail {
subject = 'Your order has been shipped!'
constructor(private orderId: number) {
super()
}
prepare() {
this.message.from('noreply@example.com').htmlView('emails/order_shipped', {
orderId: this.orderId,
})
}
}
export default class OrderShippedNotification extends Notification<undefined, { orderId: number }> {
asMailMessage() {
return new OrderShippedMail(this.params.orderId)
}
}
Both examples are anonymous notifications: use .via({ mail: { email: 'user@example.com' } }) and supply the required .params(...) before .send().
Available Methods (MailMessage)
The MailMessage class extends AdonisJS Message class and supports all its methods:
from(email, name?): Set the senderto(email, name?): Set the recipient (automatically set from targets)cc(email, name?): Add CC recipientbcc(email, name?): Add BCC recipientreplyTo(email, name?): Set reply-to addresssubject(subject): Set the email subjecthtml(content): Set HTML content directlytext(content): Set plain text contenthtmlView(template, data): Use an Edge template for HTML contenttextView(template, data): Use an Edge template for text contentattach(filePath, options?): Attach a fileattachData(content, options): Attach content as a fileembed(filePath, cid): Embed an image for inline use