Home > Net >  NodeMailer error when sending email. On production only
NodeMailer error when sending email. On production only

Time:04-06

I'm using the following code to create a SMTPtransporter that will be used to send emails. The code works perfectly fine on my computer. With Yarn version '1.22.17'.

import * as nodemailer from 'nodemailer';
import * as SMTPTransport from "nodemailer/lib/smtp-transport";

const poolOptions = {
      pool: true, 
      maxConnections: 1, 
      maxMessages: 5
}

const smtpOptions = {
host: 'smtp.gmail.com',
  port: 587,
  secure: false,
  auth: {
    user: SMTP_USER,
    pass: SMTP_PASSWORD
  },
  tls:{
        ciphers: 'SSLv3',
        rejectUnauthorized: false
      }


}


const nodemailerOptions: SMTPTransport.Options = {
      ...poolOptions,
      ...smtpOptions
    }

const transport = nodemailer.createTransport(nodemailerOptions);

The send email function :

export function sendMail(
  to: string, subject: string, text: string, html: string) {
  const mailOptions = {
    from: 'Bobo <[email protected]>', to, subject, text, html
  };

  return new Promise((resolve, reject) => {
    transport.sendMail(mailOptions, (error, info) => {
      if (error) {
        console.error(
          `Failed to send email to ${to} with body [${text}]`, error);
        reject(new Error('Email sending failed'));
      } else {
        console.log(
          `Email sent to ${to} with subject [${subject}]`, info.response);
        resolve();
      }
    });
  });
}

Meanwhile in the server i got the following error each time i try to send an email :

{ Error: read ECONNRESET
    at TCP.onStreamRead (internal/stream_base_commons.js:111:27)
  errno: 'ECONNRESET',
  code: 'ECONNECTION',
  syscall: 'read',
  command: 'CONN' }

It's the same app deployed in ubuntu server with same Yarn version. If anyone can help i would be very grateful.

NOTE :: For the deployment on the server, i used Nginx to forward all the requests on port 80 to 3000 (App running). And the ports for SMTP are open (25,587)

CodePudding user response:

It seems to be some kind of problem related to the TLS node library, there is an already question addressing this problem, you can find it here

CodePudding user response:

The problem was due to the restriction in the network put by the hosting company. I adjusted the smtp host to one they allow trafic with.

  • Related