import * as path from 'path'; import * as fs from 'fs'; import * as handlebars from 'handlebars'; import { Injectable } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { MailerOptions, MailerOptionsFactory } from '@nestjs-modules/mailer'; import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter'; @Injectable() export class MailConfigService implements MailerOptionsFactory { constructor(private configService: ConfigService) {} createMailerOptions(): MailerOptions { // __dirname resolves to src/mail in dev (ts-node) and dist/mail in the // built image, so this stays correct without depending on whether the // process was started from a source or compiled checkout. const templatesDir = path.join(__dirname, 'mail-templates'); handlebars.registerPartial( 'layout', fs.readFileSync( path.join(templatesDir, 'partials', 'layout.hbs'), 'utf-8', ), ); return { transport: { host: this.configService.get('mail.host'), port: this.configService.get('mail.port'), ignoreTLS: this.configService.get('mail.ignoreTLS'), secure: this.configService.get('mail.secure'), requireTLS: this.configService.get('mail.requireTLS'), auth: { user: this.configService.get('mail.user'), pass: this.configService.get('mail.password'), }, }, defaults: { from: `"${this.configService.get( 'mail.defaultName', )}" <${this.configService.get('mail.defaultEmail')}>`, }, template: { dir: templatesDir, adapter: new HandlebarsAdapter(), options: { strict: true, }, }, } as MailerOptions; } }