So I have this one problem with an app I am building using Nodemailer to send email to an SMTP server (or gmail) and I am getting a weird problem.
TL;DR: The email does get sent but it’s rare. Other times it just hangs at Resolved then eventually times out (I added a time out).
Here is a video of what the console is doing: Here
As you can see in the video, eventually the email gets sent. Then it times out.
I get an ‘ETIMEDOUT’ error on most of the times it attempts to send.
Not that it’s needed but I’m sending an email by pushing a button on a site built with ReactJS. I also tried directly accessing the API function using Thunder Client. That has nothing to do with it.
Here is the transporter:
`// Define which service to use
// gmail = Use gmail’s service (must have GmailSMTPUser and GmailSMTPAppPass setup)
// ll = Use LumiereLabs’s service
const emailService = serviceSettings.use
// Configure the transporter
switch (emailService) {
case "gmail":
return nodemailer.createTransport({
service: 'gmail',
auth: {
user: serviceSettings.services.gmail.user,
pass: serviceSettings.services.gmail.pass
},
logger: true,
debug: true,
maxConnections: 5, // Maximum number of connections to keep in the pool
maxMessages: 100, // Maximum number of messages to send per connection
rateLimit: 10, // Maximum number of messages to send per second
pool: true, // disable connection pooling
connectionTimeout: 10000, // 10 seconds
socketTimeout: 10000, // 10 seconds
greetingTimeout: 5000 // 5 seconds
})
break
case "ll":
return nodemailer.createTransport({
host: 'smtp.lumierelabs.org',
port: 587,
secure: false, // true for 465, false for others
requireTLS: true,
auth: {
user: serviceSettings.services.ll.user,
pass: serviceSettings.services.ll.pass
},
tls: {
rejectUnauthorized: false // if using self-signed certificates
},
logger: true,
debug: true,
maxConnections: 5, // Maximum number of connections to keep in the pool
maxMessages: 100, // Maximum number of messages to send per connection
rateLimit: 10, // Maximum number of messages to send per second
pool: true, // disable connection pooling
connectionTimeout: 10000, // 10 seconds
socketTimeout: 10000, // 10 seconds
greetingTimeout: 5000 // 5 seconds
})
break
}`
Here is the function I created to handle the email:
`const sendEmail = async (mailOptions, successMessage, res, serviceSettings) => {
let toasts = []
const transporter = createTransporter(serviceSettings)
try {
let info = await transporter.sendMail(mailOptions)
// Send to notification
toasts.push({ message: successMessage, object: { sentEmail: true }, type: 'success' })
// Send to console
console.log('Email sent: ' + info.response)
} catch (error) {
// Send to notification
toasts.push({ message: `Error sending mail: ${error}`, object: { sentEmail: false }, type: 'error'})
// Send to console
console.error('Error sending email: ', error)
// Rethrow error to handle it upstream
throw error
} finally {
transporter.close() // close the connection pool
// return something
if(toasts.length > 0) {
// Toasts found!
// Check for type
if (toasts[0].type === "error") {
// Error
if (!res.headersSent) {
return res.status(400).json(toasts)
} else {
console.warn('Attempted to send a response after headers were already sent.');
}
} if (toasts[0].type === "unauthorized") {
// Unauthorized
if (!res.headersSent) {
return res.status(401).json(toasts)
} else {
console.warn('Attempted to send a response after headers were already sent.');
}
} else {
if (!res.headersSent) {
return res.status(200).json(toasts)
} else {
console.warn('Attempted to send a response after headers were already sent.');
}
}
}
}
}`
The function is called from a userController.js on a express JS backend:
await sendEmail(mailOptions, successMessage, res, serviceSettings)
Node Version: 22.2.0
NPM Version: 10.7.0
Development Operating System: Windows 11
SMTP Server OS: Ubuntu 22.04 using Postfix and Dovecot
Any help would be greatly appreciated.
What have I done to mitigate this?
-
Changed from a self-hosted SMTP server (smtp.lumierelabs.org) to a gmail service. Same result. So it’s not an SMTP problem.
-
Created a separate app aside from the project that has the nodemailer and still got the result.
-
Restarted computer. Same result.
-
Checked telnet of smtp.lumierelabs.org on Command Prompt, smtp server works. So I know it’s not an SMTP problem.
-
Re-installed nodemailer
-
Googled the problem and can’t find something similiar.
-
Turned off Windows Defender Firewall, no change.
FinuxFirit is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.