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

Nodemailer - Cannot send message - smtp connection socket is already destroyed. #10329

Open
wguerram opened this issue Apr 17, 2024 Discussed in #10277 · 0 comments
Open

Nodemailer - Cannot send message - smtp connection socket is already destroyed. #10329

wguerram opened this issue Apr 17, 2024 Discussed in #10277 · 0 comments

Comments

@wguerram
Copy link

Discussed in #10277

Originally posted by wguerram April 15, 2024
Hello, I created a class to send emails, I instantiante this class when the application starts. My assumption is that the instance will keep the transport open but sometimes I get the error Cannot send message - smtp connection socket is already destroyed.

I'm using ElysiaJS, so I don't know if I'm doing it wrong. I'm using pooling config in Nodemailer.

I run this in my entry file

/**
 * Connect to Mail
 */
initializeMail().catch((error) => {
  console.error('Failed to connect to mail: ' + error.message);
});

My initialize code in a separate file:

export const initializeMail = async () => {
  const mailService = MailService.getInstance();

  await mailService.createConnection();

  mailService
    .verifyConnection()
    ?.then(() => {
      console.log('Mail verified.');
    })
    .catch((err) => {
      console.error('Failed to verify mail: ' + err.message);
    });
};

The class below:

export class MailService {
  private static instance: MailService;
  private transporter?: nodemailer.Transporter;

  private constructor() {}
  //INTSTANCE CREATE FOR MAIL
  static getInstance() {
    if (!MailService.instance) {
      MailService.instance = new MailService();
    }
    return MailService.instance;
  }

  //CREATE CONNECTION FOR LOCAL
  async createLocalConnection() {
    let account = await nodemailer.createTestAccount();
    this.transporter = nodemailer.createTransport({
      host: account.smtp.host,
      port: account.smtp.port,
      secure: account.smtp.secure,
      auth: {
        user: account.user,
        pass: account.pass,
      },
    });
  }

  //CREATE CONNECTION FOR LIVE
  async createConnection() {
    const transportOptions = {
      pool: process.env.SMTP_POOL === 'true' ? true : false,
      maxConnections: Number(process.env.SMTP_MAX_CONNECTIONS) || 5,
      host: process.env.SMTP_HOST,
      port: Number(process.env.SMTP_PORT) || 0,
      secure: process.env.SMTP_TLS === 'true' ? true : false,
      auth: {
        user: process.env.SMTP_USERNAME,
        pass: process.env.SMTP_PASSWORD,
      },
      tls: {
        // do not fail on invalid certs
        rejectUnauthorized: false,
      },
    };

    this.transporter = nodemailer.createTransport(transportOptions);
  }

  //SEND MAIL
  async sendMail(options: MailInterface) {    
    return this.transporter?.sendMail({
      from: options.from || process.env.SMTP_SENDER,
      to: options.to,
      cc: options.cc,
      bcc: options.bcc,
      subject: options.subject,
      text: options.text,
      html: options.html,
    });
  }
  //VERIFY CONNECTION
  verifyConnection() {
    return this.transporter?.verify();
  }

  //CREATE TRANSPOTER
  getTransporter() {
    return this.transporter;
  }
}
```</div>
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