Search code examples
node.jsreactjsgmailnodemailer

How to solve code: 'EAUTH', response: '535-5.7.8 Username and Password not accepted.responseCode: 535, command: 'AUTH PLAIN'


I have a contact me form in my website and I'm trying to send an email using gmail service and it's working flawlessly in my local dev environment, but I'm having this issue:

{code: 'EAUTH', response: '535-5.7.8 Username and Password not accepted. Lear…mail/?p=BadCredentials e19sm4851511qty.16 - gsmtp', responseCode: 535, command: 'AUTH PLAIN'}

I googled a lot and looked in the docs but all I found was to make the "Less secure app access" option in Google accounts turned on, and I did that, I tried turning it off and on and I made sure it was working in my local machine, but unfortunately it's not working in when I send the request to my API in Vercel endpoint.

my React code

const sendContactForm = (e) => {
    e.preventDefault()

    Axios.post(
      `${
        process.env.NODE_ENV === 'development'
          ? process.env.REACT_APP_API_LOCAL_URL
          : process.env.REACT_APP_API_URL
      }/contact`,
      {
        theName,
        email,
        subject,
        msg
      }
    )
      .then(({ data }) => {
        setSendStatus(data.sendStatus)
        setSendStatusMsg(data.sendStatusMsg)
        console.log(data)
      })
      .catch((err) => console.error(err))
  }

And my NodeJS code is:

const nodemailer = require('nodemailer')
const smtpTransport = require('nodemailer-smtp-transport')

module.exports = (req, res) => {
  const { theName, email, msg, subject } = req.body
  // const { MAILER_EMAIL, MAILER_PASS } = process.env

  let transporter = nodemailer.createTransport(
    smtpTransport({
      service: 'gmail',
      port: 465,
      // secure: true,
      auth: {
        user: process.env.MAILER_EMAIL,
        pass: process.env.MAILER_PASS
      }
    })
  )

  const mailOptions = {
    from: email,
    to: process.env.MAILER_EMAIL,
    subject: subject,

    html: `<p>here is simply HTML code</p>`
  }

  transporter.sendMail(mailOptions, (err, info) => {
    if (err) {
      res.send({
        sendStatusMsg: 'Sorry! something went wrong!',
        errResponse: err,
        sendStatus: 0
      })
    } else {
      res.send({
        sendStatusMsg: 'Thanks for contacting us',
        infoResponse: info.response,
        sendStatus: 1
      })
    }
  })
}

And thanks a lot.


Solution

  • I found this question with the same issue. It has other recommendations besides enabling less secure apps, you can review it and see if it helps

     

    Also, some recomend to use XOAuth2 instead to authenticate