0

I am using AWS SES for SMTP credentials and this nestjs module @nestjs-modules/mailerit was working 4/5 days ago but suddenly, what happened

I am pretty sure that my credentials are right.

Error: Unexpected socket close
    at Timeout._onTimeout
    node_modules/nodemailer/lib/smtp-transport/index.js:189:31)
    at listOnTimeout (internal/timers.js:557:17)
    at processTimers (internal/timers.js:500:7)
      transport: {
        host: process.env.EMAIL_SERVER_HOST,
        secure: false,
        port: +process.env.EMAIL_SERVER_PORT,
        auth: {
          user: process.env.EMAIL_SERVER_USER,
          pass: process.env.EMAIL_SERVER_PASSWORD,
        },
      },
      defaults: {
        from: `${process.env.EMAIL_FROM}`,
      },
      template: {
        dir: join(__dirname, 'templates'),
        adapter: new HandlebarsAdapter(),
        options: {
          strict: true,
        },
      },
    }),

Edit 1: it is working on the production environment, then why it is not working on my local machine, app is hosted on cloud run :(

2 Answers2

1

It works on the local environment and production environment as well. I hope it helps. It requires the AWS SES key and secret, the SES SMTP user and password and the correct region.

import { Module, Global } from '@nestjs/common';
import { MailerModule } from '@nestjs-modules/mailer';
import { HandlebarsAdapter } from '@nestjs-modules/mailer/dist/adapters/handlebars.adapter';
import { MailService } from './mail.service';
import { join } from 'path';
import { ConfigService } from '@nestjs/config';
import * as AWS from 'aws-sdk';

const upperCaseFn = (name: string) => {
  return name.toUpperCase();
};


@Global()
@Module({
  imports: [
    MailerModule.forRootAsync({
      useFactory: async (config: ConfigService) => ({
        transport: {
          SES: new AWS.SES({
            region: config.get('AWS_SES_REGION'),
            accessKeyId: config.get('AWS_SES_ACCESS_KEY'),
            secretAccessKey: config.get('AWS_SES_KEY_SECRET'),
          }),
          host: config.get('MAIL_HOST'),
          port: config.get('MAIL_PORT'),
          secure: false,
          ignoreTLS:true,
          requireTLS:false,
          auth: {
            user: config.get('MAIL_USERNAME'),
            pass: config.get('MAIL_PASSWORD'),
          },
          debug: true
        },
        defaults: {
          from: `"${config.get('MAIL_FROM_NAME')}" <${config.get(
            'MAIL_FROM_ADDRESS',
          )}>`,
        },
        template: {
          dir: join(__dirname, '/templates'),
          adapter: new HandlebarsAdapter({ upperCase: upperCaseFn }), // or new PugAdapter() or new EjsAdapter()
          options: {
            strict: true,
          },
        },
        options: {
          partials: {
            dir: join(__dirname, '/templates/partials'),
            options: {
              strict: true,
            },
          },
        },
      }),
      inject: [ConfigService],
    }),
  ],
  providers: [MailService],
  exports: [MailService], 
})
export class MailModule {}

Anis Momin
  • 11
  • 2
0

I got it; it is because of the wifi I am using. If I use my mobile network, it works properly.