'Authentication failed for nodemailer w/ privateemail smtp server

I am using a privateemail SMTP server to send password resets to users. Their website instructs to use port 465 and set secure to true in nodemailer. I seem to be connecting fine but I cannot authenticate. I have double-checked my username and password, so I know the problem has to be with my nodemailer configuration:

var transporter = nodemailer.createTransport({ 
  host: 'mail.privateemail.com', 
  port: 465, 
  secure: true, 
  auth: { 
    user: process.env.SMTP_USERNAME, 
    pass: process.env.SMTP_PASSWORD 
  } 
});
var mailOptions = { 
  from: 'no-reply@****.com', 
  to: user.email, 
  subject: 'Reset Password', 
  text: 'Hello,\n\n' 
    + 'Please reset your password by clicking the link: \nhttp:\/\/' 
    + req.headers.host + '\/confirmation\/' + token.token + '.\n' 
};
transporter.sendMail(mailOptions, function (err) {
if (err) { return res.status(500).send({ msg: err.message }); }
  res.status(200).send('A password reset email has been sent to ' + user.email + '.');
});

Any thoughts?



Solution 1:[1]

You can debug nodemailer by adding the following parameters to your nodemailer createTransport function:

  debug: true, // show debug output
  logger: true // log information in console

In doing so, I discovered that the username and password environment variables had quotation marks and a semicolon as part of their string. The solution was to set password and username as follows without quotation marks or a semicolon:

EMAIL_USERNAME = no-reply@****.com
EMAIL_PASSWORD = password

Solution 2:[2]

I think you have to pass secureConnection:true instead of secure:true also the port is 587 not 465 check here namecheap docs so this is your code after modification

var transporter = nodemailer.createTransport({ 
  host: 'mail.privateemail.com', 
  port: 587, 
  secureConnection: true, 
  auth: { 
    user: process.env.SMTP_USERNAME, 
    pass: process.env.SMTP_PASSWORD 
  } 
});
var mailOptions = { 
  from: 'no-reply@****.com', 
  to: user.email, 
  subject: 'Reset Password', 
  text: 'Hello,\n\n' 
    + 'Please reset your password by clicking the link: \nhttp:\/\/' 
    + req.headers.host + '\/confirmation\/' + token.token + '.\n' 
};
transporter.sendMail(mailOptions, function (err) {
if (err) { return res.status(500).send({ msg: err.message }); }
  res.status(200).send('A password reset email has been sent to ' + user.email + '.');
});

Solution 3:[3]

you have to add tls: {rejectUnauthorized: false } property remove auth object

nodemailer.createTransport({

host: 'smtp1.mailserver.com',

port: 25,

tls: { rejectUnauthorized: false}

});

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Julian Porter
Solution 2 Saeed
Solution 3 plabon