Search code examples
dockeremailgmailnodemailer

Error occurred: Invalid login: 535-5.7.8 Username and Password not accepted. Google App Password on docker failing and on dev working


I have created an app password on Google and stored also in the .env files:

EMAIL_SERVICE=gmail
[email protected]
EMAIL_PASSWORD="xxxx yyyy zzzz cccc"

This is the code:

const sendEmailResetPassword = async (
  email,
) => {
  return new Promise((resolve, reject) => {
    // Create a transporter object using Gmail SMTP transport
    const transporter = nodemailer.createTransport({
      service: process.env.EMAIL_SERVICE,
      auth: {
        user: process.env.EMAIL_USERNAME,
        pass: process.env.EMAIL_PASSWORD,
      },
    });

    // Define the email options
    const mailOptions = {
      from: process.env.EMAIL_USERNAME,
      to: email,
      subject: subject,
    };

    // Send the email
    transporter.sendMail(mailOptions, function (error, info) {
      if (error) {
        console.log("Error occurred:", error.message);
        reject(error);
      } else {
        resolve(info);
      }
    });
  });
};

When I run the service locally with npm run dev, I do not get any error and the email is sent successfully.

When I build the image and run the docker container, it fails, I know the .env is recognized because I checked using

docker exec -it container env

And it prints:

EMAIL_SERVICE=gmail
[email protected]
EMAIL_PASSWORD="xxxx yyyy zzzz cccc"

When I send when I run the docker container, I get this:

  response: '535-5.7.8 Username and Password not accepted. For more information, go to\n' +
    '535 5.7.8  https://support.google.com/mail/?p=BadCredentials fl25-20020a05600c0b9900b0040b3e26872dsm28169818wmb.8 - gsmtp',
  responseCode: 535,

Also, the other .env variables are working such as MySQL URL and others so the .env variables are recognized and are functional.


Solution

  • The password generated by Google is something like this: "AAAA BBBB CCCC DDDD", I removed the spaces removed the parenthesis so it looked like this: AAAABBBBCCCCDDDD and it worked.