Roman Savka
Roman Savka

Reputation: 11

How to sent mail from nestjs using nodemailer?

I am trying to send mail using nodemailer in nestjs from my personal gmail, but receive nothing as result(expecting to get mail in inbox). Below I attached my code:

 `app.module.ts`

 
 @Module({
     imports: [
         MulterModule.register({
         dest: "./uploads"
     }),
         MailerModule.forRoot({
             transport: {
                 host: 'smtp.googlemail.com',
                 port: 465,
                 ignoreTLS: true,
                 secure: true,
                 auth: {
                     user: process.env.EMAIL_ID,
                     pass: process.env.EMAIL_PASS
                 },
             },
             defaults: {
                 from: '"No Reply" <[email protected]>',
             },
             preview: false,
             template: {
                 dir: process.cwd() + '/template/',
                 adapter: new HandlebarsAdapter(),
                 options: {
                     strict: true,
                 },
             },
         }),
     ],
     controllers: [AppController],
     providers: [AppService]
 
 })
 
 export class AppModule {}
 
 `app.controller.ts`
 
 @Controller()
 export class AppController {
     constructor(private appservice: AppService) {}
 
  @Get()
     sendMail(): void {
        return this.appservice.sendMail()
     }
 }
 
 
 `app.service.ts`
 
 @Injectable()
 export class AppService {
     constructor(private readonly mailerService: MailerService) {}
 
       sendMail(): void {
         this.mailerService.sendMail({
             to: '[email protected]',
             from: '[email protected]',
             subject: 'Testing Nest MailerModule ✔', 
             text: 'welcome', 
             html: '<b>welcome</b>',
 
         });
     }

When I'm checking this code in postman - I received 200 status code, but when checking my inbox - don't find new mail there.

Upvotes: 0

Views: 6538

Answers (1)

pryme0
pryme0

Reputation: 140

You will need to turn on access for less secure app if you are using gmail as your smtp host

use this link less secure app

Also your transport configuration should look like this

 transport: {
             host: 'smtp.gmail.com',
             port: 465,
             ignoreTLS: true,
             secure: true,
             auth: {
                 user: process.env.EMAIL_ID,
                 pass: process.env.EMAIL_PASS
             },
         }

smtp.gmail.com works fine for me

Upvotes: 2

Related Questions