1

I am using nodemailer module to send email Whenever user signs up with the app but when I send request from postman it is sending the request and it keeps running. It is saving data to the database but it is not sending emails to the user's email address.

The sendMail function:

const sendEmail = async (email, subject, text) => {
  var transporter = nodemailer.createTransport({
    service: "gmail",
    auth: {
      user: keys.senderEmail,
      pass: keys.senderPassword,
    },
  });

  var mailOptions = {
    from: keys.senderEmail,
    to: email,
    subject,
    text,
  };

  transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
      console.log(error);
    } else {
      console.log("Email sent: " + info.response);
      return info.response;
    }
  });
};

The Signup function:

exports.signup = async (req, res, next) => {
  try {
    const ootp = otpGenerator(4);
    // check existing email
    const { phoneNumber, password, accountType, email } = req.body;
    let check_user = await Auth.findOne({ phoneNumber });
    if (check_user)
      return res
        .status(409)
        .json({ error: "Phone number is already registered" });

    const hashedPassword = passwordHash.generate(password);
    const otp = otpGenerator(4);
    await fast2sms(
      {
        message: `Your OTP is ${otp}`,
        contactNumber: phoneNumber,
      },
      next
    );
    const qrData = { phoneNumber };
    let strData = JSON.stringify(qrData);
    const generateQR = await qrcode.toDataURL(strData);
    let new_user = new Auth({
      phoneNumber,
      email,
      password: hashedPassword,
      accountType,
      otp,
      qrCode: generateQR,
    });

    const payload = {
      id: new_user._id,
      phoneNumber: `${new_user.phoneNumber}`,
    };
    let token = jwt.sign(payload, keys.secretOrKey, { expiresIn: 31556926 });
    const save = await new_user.save();

    await sendMail(new_user.email, subject, text);

    res.send({
      success: true,
      msg: "Details saved",
      data: { user: save, token },
    });
  } catch (error) {
    return error.message;
  }
};

What I am doing wrong here? I am not sending subject or text properly? OK, It is giving me the error in the console:

Error: Invalid login: 535-5.7.8 Username and Password not accepted. Learn more at
535 5.7.8  https://support.google.com/mail/?p=BadCredentials a2-20020a17090a70c200b001ef8ea89a33sm2988042pjm.2 - gsmtp
    at SMTPConnection._formatError (C:\Users\Kheni\OneDrive\Desktop\nodejs projects\Node.js projects\Verover\Verover_Backend\node_modules\nodemailer\lib\smtp-connection\index.js:784:19)
    at SMTPConnection._actionAUTHComplete (C:\Users\Kheni\OneDrive\Desktop\nodejs projects\Node.js projects\Verover\Verover_Backend\node_modules\nodemailer\lib\smtp-connection\index.js:1536:34)
    at SMTPConnection.<anonymous> (C:\Users\Kheni\OneDrive\Desktop\nodejs projects\Node.js projects\Verover\Verover_Backend\node_modules\nodemailer\lib\smtp-connection\index.js:540:26)
    at SMTPConnection._processResponse (C:\Users\Kheni\OneDrive\Desktop\nodejs projects\Node.js projects\Verover\Verover_Backend\node_modules\nodemailer\lib\smtp-connection\index.js:947:20)
    at SMTPConnection._onData (C:\Users\Kheni\OneDrive\Desktop\nodejs projects\Node.js projects\Verover\Verover_Backend\node_modules\nodemailer\lib\smtp-connection\index.js:749:14)
    at TLSSocket.SMTPConnection._onSocketData (C:\Users\Kheni\OneDrive\Desktop\nodejs projects\Node.js projects\Verover\Verover_Backend\node_modules\nodemailer\lib\smtp-connection\index.js:189:44)
    at TLSSocket.emit (node:events:527:28)
    at addChunk (node:internal/streams/readable:315:12)
    at readableAddChunk (node:internal/streams/readable:289:9)
}
Talc
  • 109
  • 6
  • Try using email variable . instread of reading from new_user.email . code snippet : await sendMail(email, subject, text); – Senthil Jul 28 '22 at 06:08
  • Yes, I have tried it but not working @Senthil – Talc Jul 28 '22 at 06:13
  • Are you using an account with multifactor authentication? If so, you need to create a password for your application on gmail, won't work with the normal account password – Jcl Jul 28 '22 at 06:22
  • @Jcl Sorry, I am not very much familiar with all this. Can you please elaborate in detail what steps I need to take? – Talc Jul 28 '22 at 06:25
  • Follow this to create an app and there you will get a user name and password / secret id to connect to that https://support.google.com/accounts/answer/185833. Using gmail user name and pwd is not more supported. – Senthil Jul 28 '22 at 06:27
  • All the documentation is in the URL you are getting in the message in your console error ( https://support.google.com/mail/?p=BadCredentials ) – Jcl Jul 28 '22 at 06:31

0 Answers0