Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hook doesn't run when using custom configure transport #1234

Open
tms-hoangnguyen3 opened this issue Nov 2, 2024 · 0 comments
Open

Hook doesn't run when using custom configure transport #1234

tms-hoangnguyen3 opened this issue Nov 2, 2024 · 0 comments

Comments

@tms-hoangnguyen3
Copy link

import * as nodemailer from 'nodemailer';
import { EmailValidator } from './emailValidator';
import { TransportOptions } from './transportOptions.type';

export const createTransportWithEmailFiltered = (
  transportOptions: TransportOptions,
  domainList: string[],
) => {
  try {
    const transport = nodemailer.createTransport(transportOptions);
    const validator = new EmailValidator(domainList);
    console.log(domainList);

    transport.use('compile', (mail, callback) => {
      console.log('compile middleware invoked'); // Initial log
      console.log('mail', mail);

      if (!mail.data.to) {
        console.log('No email addresses to send to');
        return callback(new Error('No email addresses to send to'));
      }

      if (!Array.isArray(mail.data.to)) {
        mail.data.to = [mail.data.to];
      }

      console.log('in here', domainList);
      console.log('mail.data.to', mail.data.to);

      const filteredEmails = validator.emailFiltered(mail.data.to);
      console.log('filteredEmails', filteredEmails);

      if (filteredEmails.length === 0) {
        console.log('No valid email addresses to send to');
        return callback(new Error('No valid email addresses to send to.'));
      }

      mail.data.to = filteredEmails;
      callback();
    });

    return transport.transporter;
  } catch (err) {
    console.log(err);
  }
};
export const mailerConfig: MailerAsyncOptions = {
  useFactory: (config: ConfigService) => {
    try {
      const mailerConfig = config.get<MailerConfig>('mailer');
      if (!mailerConfig) throw new Error('Mailer Config Not Found!');
      const mailFrom = process.env.EMAIL_FROM || mailerConfig?.from;

      let transportConfig: TransportType = {
        host: mailerConfig.host,
        port: mailerConfig.port,
        auth: {
          user: process.env.EMAIL_USER as string,
          pass: process.env.EMAIL_PASSWORD as string,
        },
      };

      if (process.env.NODE_ENV !== 'development')
        transportConfig = {
          host: mailerConfig.host,
          port: mailerConfig.port,
          SES: {
            ses: new aws.SES({
              apiVersion: '2010-12-01',
              region: process.env.AWS_REGION as string,
              credentials: {
                accessKeyId: process.env.AWS_ACCESS_KEY as string,
                secretAccessKey: process.env.AWS_SECRET_KEY as string,
              },
            }),
            aws,
            disableUrlAccess: false,
          },
        };

      if (process.env.NODE_ENV === 'production') {
        return {
          transport: transportConfig,
          defaults: {
            from: mailFrom,
          },
        };
      } else {
        const transport = createTransportWithEmailFiltered(transportConfig, [
          'example.com',
        ]);

        console.log(transport);
        return {
          transport,
          defaults: {
            from: mailFrom,
          },
        };
      }
    } catch (err) {
      console.log('Mailer Config Error:', err);
      throw new Error('Mailer Error');
    }
  },
  inject: [ConfigService],
};

I have created a function to custom transport config and added it into module like that.
And I have logged the transport and I got a custom plugin added in here
Screenshot 2024-11-02 at 19 16 33

However, after the email was sent successfully, I could not get a log and the email doesn't filter

Could you give it a look? Thank You!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant