1

I want to add to my web app that after order I'm sending a mail.

I choose Nodemailer because it's the most famous npm to use.

I coded my request and in the local environment, it's working.

I uploaded the code to Heroku and I get an Error.

Error: Invalid login: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbs

I checked people told me to disable the captcha wish I did here: UnlockCaptcha

And now I still get the same error, and I get a mail that google blocked the connection what can I do?

const nodemailer = require('nodemailer');
const { sendLog } = require('../middleware/sendLog');
const { coupons, actions } = require('../constant/actionCoupon');

var simple = function () {
  var textMultiple = {
    text1: 'text1',
    text2: 'text2',
  };
  return textMultiple;
};
// send mail system for the (REQUEST ACCEPTED SYSTEM)
const sendMail = (mail, action) => {
  let mailTransporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
      user: process.env.MAIL,
      pass: process.env.PASSWORD,
    },
  });

  let mailDetails = {
    from: process.env.MAIL,
    to: mail,
    subject: `Thank you for your purchase. with love FameGoal`,
    text: "for any probleme please reply on this message",
  };

  mailTransporter.sendMail(mailDetails, function (err, data) {
    if (err) {
      console.log(err);
      console.log(`error sent mail to ${mail}`, 'error');
    } else {
      console.log('succeed');
      console.log(`succesfully sent mail to ${mail}`, 'info');
    }
  });
};

exports.sendMail = sendMail;
Ethanolle
  • 1,106
  • 1
  • 8
  • 26

1 Answers1

1

Using Gmail as an SMTP relay isn't the most ideal because Google servers may reject basic username/password authentication at times.

There are some workarounds. The most ideal is to use OAuth2 to send emails.

OAuth2

OAuth2 uses access tokens to perform authentication instead of a password. I won't go over the steps to set up OAuth2 because it can take some time but if you're interested, this answer: https://stackoverflow.com/a/51933602/10237430 goes over all of the steps.

App passwords

If the Google account you're trying to send emails from has two step verification enabled, using a password to send emails will not work. You instead need to generate a app-specific password on Google's site and pass that in the password field.

More info on that here: https://support.google.com/accounts/answer/185833?hl=en

Enabling less secure apps

If you still want to use your current setup, you have to make sure you enable less secure apps on the Google account you're sending emails from. This will let you authenticate with Google using just an email and a password.

More info on that here: https://support.google.com/accounts/answer/6010255?hl=en

Basic password authentication will not work until you enable less secure apps.

Abir Taheer
  • 2,502
  • 3
  • 12
  • 34