Add beforeSend hook for email customization

- Add BeforeSendHook type and BeforeSendMailOptions interface
- Implement hook execution in MailingService before sending emails
- Hook allows adding attachments, headers, and modifying email options
- Add comprehensive documentation with examples
- Bump version to 0.1.20
This commit is contained in:
2025-09-14 12:19:52 +02:00
parent 347cd33e13
commit 0d6d07de85
5 changed files with 96 additions and 8 deletions

View File

@@ -270,7 +270,7 @@ export class MailingService implements IMailingService {
fromField = this.getDefaultFrom()
}
const mailOptions = {
let mailOptions: any = {
from: fromField,
to: email.to,
cc: email.cc || undefined,
@@ -281,6 +281,16 @@ export class MailingService implements IMailingService {
text: email.text || undefined,
}
// Call beforeSend hook if configured
if (this.config.beforeSend) {
try {
mailOptions = await this.config.beforeSend(mailOptions, email)
} catch (error) {
console.error('Error in beforeSend hook:', error)
throw new Error(`beforeSend hook failed: ${error instanceof Error ? error.message : 'Unknown error'}`)
}
}
await this.transporter.sendMail(mailOptions)
await this.payload.update({