Wednesday, 29 December 2021

Nodemailer is not sending emails in NestJs

I have the next configuration for nodemailer package:

//App module 
@Module({
  imports: [
    MailerModule.forRoot({
      transport: {
        host: 'localhost',
        port: 3000,
        secure: false,
      },
      defaults: {
        from: '"nest-modules" <modules@nestjs.com>',
      },
      template: {
        dir: __dirname + '/templates',
        adapter: new HandlebarsAdapter(),
        options: {
          strict: true,
        },
      },
    }),
   ...
})
export class AppModule {}

And

//Email service 
export class EmailService {
  constructor(private readonly mailerService: MailerService) {}

  public example(): void {
    this.mailerService
      .sendMail({
        to: 'email@gmail.com', // list of receivers
        from: 'test@nestjs.com', // sender address
        subject: 'Testing Nest MailerModule ✔', // Subject line
        text: 'welcome', // plaintext body
        html: '<b>welcome</b>', // HTML body content
      })
      .then((r) => {
        console.log(r, 'email is sent');
      })
      .catch((e) => {
        console.log(e, 'error sending email');
      });
  }
}

I am using my local environement. Tring the code above i get an error in catch block: Error: Greeting never received. Why i get that error and how to send the email without any issue?



from Nodemailer is not sending emails in NestJs

No comments:

Post a Comment