Error: Invalid login: 535 Authentication failed: Bad username / password with node mailer

Viewed 8586

I am getting "Error: Invalid login: 535 Authentication failed: Bad username / password" I have correct username and password in there but still get this error. What might be the issue.. enter image description here

  const sendForgotPasswordEmail = (user) => {
    if (!user) { return; }
    const token = user.passwordResetToken;
    const transporter = nodemailer.createTransport({
      service: 'SendGrid',
      auth: {
        user: process.env.SENDGRID_USER,
        pass: process.env.SENDGRID_PASSWORD
      }
    });
    const mailOptions = {
      to: user.email,
      from: 'mail.mail@com',
      subject: 'Reset your password',
      text: `You are receiving this email because you (or someone else) have requested the reset of the password for your account.\n\n
        Please click on the following link, or paste this into your browser to complete the process:\n\n
        http://${req.headers.host}/reset/${token}\n\n
        If you did not request this, please ignore this email and your password will remain unchanged.\n`
    };
    return transporter.sendMail(mailOptions)
      .then(() => {
        req.flash('info', { msg: `An e-mail has been sent to ${user.email} with further instructions.` });
      });
  };

  createRandomToken
    .then(setRandomToken)
    .then(sendForgotPasswordEmail)
    .then(() => res.redirect('/forgot'))
    .catch(next);
1 Answers

Although you didn't specify, from your cross-posting on Github and the screenshot of the error, your issue is more specifically that you are getting a username/password error when using Hackathon Starter (with SendGrid setup) ; and the project under its hood uses nodemailer.

I have tested Hackathon Starter with SendGrid and a valid username/password, and it worked as intended, so it is not an issue with Hackathon Starter or Nodemailer. So in your specific case, according to the error that you posted SendGrid is responding that the username and password they received doesn't correspond to a valid username password in their system. I would suggest:

  1. Login to SendGrid on their website and re-verify the username and password. Note that the API keys that SendGrid provides are NOT the username and password; they are intended for a different purpose than SMTP mail authentication with SendGrid. You need to use the Username/Password under Account Details in SendGrid.
  2. Check your environment variables to verify that they are set properly with your SendGrid "username" and "password". Note that most likely you will not need to add any single or double quotes around your username and password (at least that is the way it is working for me). You can temporarily add debug code like the following in your code for checking that your environment variables are set properly.

    console.log "SENDGRID_USER env variable is set as:", process.env.SENDGRID_USER console.log "SENDGRID_PASSWORD env variable is set as:", process.env.SENDGRID_PASSWORD

Make sure to remove this debug code after verification, since having the password going into the console log as clear text can become a security issue.

By the way, in your mailOptions you may want to change the from: field to a valid email address that corresponds to one of your project's email addresses/aliases.

Related