nest-modules / mailer

📨 A mailer module for Nest framework (node.js)
https://nest-modules.github.io/mailer/
MIT License
846 stars 177 forks source link

how to include nodemailer plugins into mailer? #734

Closed Simpey closed 10 months ago

Simpey commented 2 years ago

It says transport is the transport configuration object, connection url or a transport plugin instance https://www.npmjs.com/package/@yops/nest-mailer/v/0.1.21 But how to include plugins, nodemailer juice in MailerModule.forRoot, for example? how to write transporter.use('compile', inLineCss()); in

    MailerModule.forRoot({
      transport: {
            ...
      },
      template: {
          ...
      },
    }),

can't find any documentation about it

gcherem commented 2 years ago

Any update on this issue?

janrsilva commented 2 years ago

Hi @Simpey,

I had been with the same problem, unfortunately I also didn't find ways to put nodemailer plugins. So I solved creating a custom adapter.

In my case i need to convert the base64 image src to cids to embed the image content inside the emails.

So I created:

import { MailerOptions } from '@nestjs-modules/mailer';
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
import * as inlineBase64 from 'nodemailer-plugin-inline-base64';

export class MyCustomHandlebarsAdapter extends HandlebarsAdapter {
  /**
   * Override compile method to use inlineBase64 nodemailer plugin
   * @param mail
   * @param callback
   * @param mailerOptions
   */
  compile(mail: any, callback: any, mailerOptions: MailerOptions): void {
    return super.compile(
      mail,
      () => {
        const resolveContent = inlineBase64();
        resolveContent(mail, (err: any) => {
          if (err) {
            return callback(err);
          }
          return callback();
        });
      },
      mailerOptions,
    );
  }
}

and put that in my MailModule.ts

@Module({
  imports: [
    MailerModule.forRoot({
      transport: {
        host: configuration.mail.host,
        port: configuration.mail.port,
        auth: {
          user: configuration.mail.user,
          pass: configuration.mail.pass,
        },
      },
      defaults: {
        from: configuration.mail.from,
      },
      preview: true,
      template: {
        dir: dir,
        adapter: new MyCustomHandlebarsAdapter(), // <- here
        options: {
          strict: true,
        },
      },
    }),
  ],
  providers: [MailService],
  exports: [MailService],
})
export class MailModule { }

I believe its the same solution to your problem, or you can use Maizzle to build your templates with tailwind and the @maizzle/framework will convert everything to inline css.