nest-modules / mailer

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

Is it working template on send mail? #267

Closed JayStevency closed 9 months ago

JayStevency commented 4 years ago

I set up mail module and use MailerService on mail service.

/src/config/mailer.config.ts

import { Injectable } from '@nestjs/common';
import { MailerOptionsFactory } from '@nestjs-modules/mailer';
import { ConfigService } from '@nestjs/config';
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';

@Injectable()
export class MailerConfig implements MailerOptionsFactory {
  constructor(private configService: ConfigService) {}
  createMailerOptions() {
    return {
      transport: {
        host: 'smtp.office365.com',
        port: 587,
        secure: false,
        auth: {
          user: this.configService.get('mailAccount'),
          pass: this.configService.get('mailAccountPassword'),
        },
        defaults: {
          from: 'dev@energyx.co.kr',
        },
        template: {
          dir: process.cwd() + '/src/template/',
          adapter: new HandlebarsAdapter(),
          options: {
            strict: true,
          },
        },
      },
    };
  }
}

src/mail/mail.module.ts

import { Module } from '@nestjs/common';
import { MailService } from './mail.service';
import { ConfigModule } from '@nestjs/config';
import { MailerModule } from '@nestjs-modules/mailer';
import { MailerConfig } from '../config/mailer.config';

@Module({
  imports: [
    MailerModule.forRootAsync({
      imports: [ConfigModule],
      useClass: MailerConfig,
    }),
  ],
  providers: [MailService],
  exports: [MailService],
})
export class MailModule {}

src/mail/mail.service.ts

import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { MailerService } from '@nestjs-modules/mailer';

@Injectable()
export class MailService {
  constructor(
    private readonly mailerService: MailerService,
    private readonly configService: ConfigService,
  ) {}

  async sendPasswordMail(email, verificationPath) {
    const link =
      this.configService.get('clientHostname') +
      '/resetPass/' +
      verificationPath;
    const logoWithColor =
      this.configService.get('imageHostname') + '/logo-dark.png';
    const logoWithOutColor =
      this.configService.get('imageHostname') + '/logo-light.png';

    return this.mailerService.sendMail({
      to: email,
      from: ',
      subject: '',
      template: 'password',
      context: {
        logoWithColor: logoWithColor,
        logoWithOutColor: logoWithOutColor,
        link: link,
      },
    });
  }
}

src/template |- password.hbs

I checked MailerOptions on mailService on mail.service.ts

It was fine and mail sent as well.

But i couldn't get any mail contents by password.hbs

How can i do something more?

Thanks for reading my trouble. plz help me.

issue-label-bot[bot] commented 4 years ago

Issue-Label Bot is automatically applying the label question to this issue, with a confidence of 0.77. Please mark this comment with :thumbsup: or :thumbsdown: to give our bot feedback!

Links: app homepage, dashboard and code for this bot.

uncle-tee commented 4 years ago

were u able to resolve this?

uncle-tee commented 4 years ago

@JayStevency Whilst I was almost giving up on the library. I realized the documentation is misguiding.

Please use this instead.

createMailerOptions() : MailerOptions {
    return {
      transport: {
        host: 'smtp.office365.com',
        port: 587,
        secure: false,
        auth: {
          user: this.configService.get('mailAccount'),
          pass: this.configService.get('mailAccountPassword'),
        },
      },
      defaults: {
        from: 'dev@energyx.co.kr',
      },
      template: {
        dir: process.cwd() + '/src/template/',
        adapter: new HandlebarsAdapter(),
        options: {
          strict: true,
        },
      },
    };
  }
JayStevency commented 4 years ago

Thanks @uncle-tee .

I tried that you were mentioned MailerOptions , but I couldn't get any changes...

So I just read on file .hbs not use adapter .

I hope it works well later.