I am using smtp.emailcloud.smartping.io as my SMTP service provider. The emails are sent on my localhost successfully. But not sending on my server getting below error :
SendMail error: ==>> Error: read ECONNRESET
at TLSWrap.onStreamRead (node:internal/stream_base_commons:217:20) {
errno: -104,
code: 'ESOCKET',
syscall: 'read',
command: 'CONN'
}
my code is as below :
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
secure: false,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS
},
tls: {
rejectUnauthorized: false
},
dnsTimeout: 180000, //3 minutes timeout
debug: true,
logger: true,
})
const sendEmail = async (emailData) => {
try {
const { senderEmail, sendTo, subject, emailBody, attachmentName } = emailData;
const attachmentPath = path.join(process.cwd(), 'uploads/attachments', attachmentName);
const mailOptions = {
from: senderEmail,
to: sendTo,
subject: subject,
html: emailBody,
attachments: attachmentName !== 'null'
? [
{
filename: attachmentName,
path: attachmentPath
}
]
: []
}
if (emailData.cc) {
mailOptions['cc'] = emailData.cc;
}
console.log('mailOptions', mailOptions);
// transporter.sendMail(mailOptions)
transporter.sendMail(mailOptions)
.then(info => console.log('Email sent: ==>>', info))
.catch(error => console.error('SendMail error: ==>>', error));
} catch (error) {
console.log('error -->>', error);
Sentry.captureMessage("Something went wrong in sendEmail util function");
Sentry.captureException(error);
}
};
I have tried below changes in config :
const transporter = nodemailer.createTransport({
host: process.env.SMTP_HOST,
port: 465,
secure: true,
auth: {
user: process.env.SMTP_USER,
pass: process.env.SMTP_PASS
},
tls: {
rejectUnauthorized: false
},
dnsTimeout: 180000, //3 minutes timeout
debug: true,
logger: true,
})
I have tried several solutions from google but did not work.
It would be greate if you help me to resolve it.
Thank you
After trying many solutions from Google and StackOverflow. I came to know that my code is correct the issue was with SMTP credentials. I have used other SMTP credentials and it worked.