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

Cannot destructure property 'templateName' of 'precompile(...)' #570

Closed hddananjaya closed 9 months ago

hddananjaya commented 3 years ago

Hi all, 🦄

Config:

    "@nestjs-modules/mailer": "^1.6.0",
    "nodemailer": "^6.5.0",
    "handlebars": "^4.7.7",

I'm trying to use with handlebars.

App Module:

@Module({
  imports: [
    UserModule,
    AdminModule,
    HotelModule,
    TypeOrmModule.forRoot(),
    ConfigModule.forRoot({ isGlobal: true }),
    MailerModule.forRootAsync({
      useFactory: () => ({
        transport:
          'smtps://no-reply@XXX:XXX@XXX.com',
        defaults: {
          from: '"XXX" <no-reply@XXX.com>',
        },
        template: {
          dir: path.resolve(__dirname, 'templates'),
          adapter: new HandlebarsAdapter(),
          options: {
            strict: true,
          },
        },
      }),
    }),
  ],
  controllers: [AppController],
  providers: [AppService],
})

Service:

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

@Injectable()
export class UserMailService {
  constructor(private readonly mailerService: MailerService) {}

  sendEmailConfirmation(): void {
    this.mailerService
      .sendMail({
        to: 'test@nestjs.com',
        from: 'noreply@nestjs.com',
        subject: 'Testing Nest Mailermodule with template ✔',
        template: '/welcome',
      })
      .then((success) => {
        console.log(success);
      })
      .catch((err) => {
        console.log(err);
      });
  }
}

I get this error when I use templates.

TypeError: Cannot destructure property 'templateName' of 'precompile(...)' as it is undefined.
    at HandlebarsAdapter.compile (/mnt/c/Users/user/Desktop/web/api/node_modules/@nestjs-modules/mailer/dist/adapters/handlebars.adapter.js:52:17)
    at /mnt/c/Users/user/Desktop/web/api/node_modules/@nestjs-modules/mailer/dist/mailer.service.js:48:40
    at processPlugins (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:279:13)
    at /mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:283:17
    at Mail._convertDataImages (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:387:20)
    at Mail._defaultPlugins.compile (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:31:41)
    at processPlugins (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:279:13)
    at Mail._processPlugins (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:287:9)
    at Mail.sendMail (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:164:14)
    at MailerService.<anonymous> (/mnt/c/Users/user/Desktop/web/api/node_modules/@nestjs-modules/mailer/dist/mailer.service.js:74:51)
(node:8093) UnhandledPromiseRejectionWarning: Error: ENOENT: no such file or directory, open '/welcome.hbs'
    at Object.openSync (fs.js:476:3)
    at Object.readFileSync (fs.js:377:35)
    at precompile (/mnt/c/Users/user/Desktop/web/api/node_modules/@nestjs-modules/mailer/dist/adapters/handlebars.adapter.js:34:41)
    at HandlebarsAdapter.compile (/mnt/c/Users/user/Desktop/web/api/node_modules/@nestjs-modules/mailer/dist/adapters/handlebars.adapter.js:52:34)
    at /mnt/c/Users/user/Desktop/web/api/node_modules/@nestjs-modules/mailer/dist/mailer.service.js:48:40
    at processPlugins (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:279:13)
    at /mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:283:17
    at Mail._convertDataImages (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:387:20)
    at Mail._defaultPlugins.compile (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:31:41)
    at processPlugins (/mnt/c/Users/user/Desktop/web/api/node_modules/nodemailer/lib/mailer/index.js:279:13)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:8093) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:8093) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I manually copied templates folder to dist to check whether the problem is with paths. But it asks for templateName which I didn't provide.

Thanks!

hddananjaya commented 3 years ago

I was able to fix this by adding a ./ to template; as mentioned in #569 Please close this issue if not needed. Thanks.

subalee commented 2 years ago

That is actually not a complete solution, at least not for me.

What you actually have to do is update nest-cli.json to actually copy the templates to the dist folder

nest-cli.json

"sourceRoot": "src",
"root": "./",
"compilerOptions": {
    "assets": [
      {
        "include": "notification/email-templates/**/*.hbs",
        "outDir": "dist/src"
      }
    ],
    "watchAssets": true
  }

where notification is the directory of my module and and my templates are inside the email-templates directory. The path is actually copied that's why the outDir only has dist/src there.

however you still need to include the ./[template-name] when trying to send an email.

I actually figured this out thanks to this discussion

cadmax commented 2 years ago

tks @Subalee

emilenfc commented 1 year ago

Thank you @subalee

joler023 commented 7 months ago

thank you @subalee

RashJrEdmund commented 2 months ago

thank you @subalee

IT-WIBRC commented 2 months ago

@RashJrEdmund I encountered a problem with your config. It takes only the templates folder and files with the .hbs extension inside it, and not the images folder and other one at the same level as the templates.

Here is my config: include: mail/templates/** outDir: dist/src