Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

254
Visualizações
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?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

  1. This code means
MailerModule.forRoot({
      transport: {
        host: 'localhost',
        port: 3000,
        secure: false,
      },

nodemailer try to find SMTP mail transfer agent (relay) listening on localhost:3000. Its unlikely there is SMTP server on your machine on 3000 port, so most likely nodemailer cannot receive confirmation from anything on your 3000 port that it is SMTP server and throws error you mentioned.

  1. Try using this transport for nodemailer, it can work without SMTP relay https://www.npmjs.com/package/nodemailer-direct-transport

Code will be something like this:


import {directTransport} from 'nodemailer-direct-transport';

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

about 4 years ago · Juan Pablo Isaza Relatório

0

I'm not sure if you just wanna send an email using local SMTP or send an email in any SMTP available

if it's the latter you can use google SMTP or mailtrap.No need to set up SMTP server. The configuration will be

    MailerModule.forRoot({
   transport: {
        host: "smtp.gmail.com",
        port: "465",
        secure: true,
        auth: {
            user: "your_gmail_email",
            pass: "your_gmail_app_password"
          }
           }
         })
      // or for mailtrap.io 
       const nodemailer = require('nodemailer');
  let transporter = nodemailer.createTransport({
         host: 'smtp.mailtrap.io',
         port: 2525,
         auth: {
             user: "<user>",
             pass: "<pass>"
         }
 })

In the case of a local SMTP server, you can check smtp-server to create an SMTP server or MaiDev will help you to set up an SMTP server locally. MailDev has also available via docker that would be easier too.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda