1

I am trying to setup nodemail, but run into a strange issue where I cannot wrap my head around. Whenever I fill in the credentials as a string, everything works fine (but of course I don't want this). If I start using env variables, then I get the following error message:

UnhandledPromiseRejectionWarning: Error: Missing credentials for "PLAIN"

It's using a gmail account to send an email. The account has the bypass for unsafe accounts enabled. When I add a console.log() statement, the password is clearly visible. Does anyone have any idea why this is happening and how this can be resolved. Below you'll find a code snippet which sends the email.

import nodemailer from 'nodemailer';

const transporter = nodemailer.createTransport({
  service: 'gmail',
  auth: {
    user: 'some-account@gmail.com',
    pass: process.env.PWD,
  },
});

export const sendUserCreateEmail = (to, firstName) => {
  const mailOptions = {
    from: 'someone',
    to: to,
    subject: 'It works',
    html: `
      <p>It works!</p>
    `,
  };

  return transporter.sendMail(mailOptions);
};
NewVigilante
  • 1,291
  • 1
  • 7
  • 23
  • I believe your authentication to gmail is failing as they require Oauth2 https://stackoverflow.com/questions/48854066/missing-credentials-for-plain-nodemailer – Kasey Chang May 06 '20 at 19:39

2 Answers2

2

You need to install dotenv

npm install dotenv

then import it at your file

const dotenv = require('dotenv');
dotenv.config();

then it should works

0

I figured it out, although it is more of a work around. Apparently, the variables which where set by a dotenv file are not working there. I modified my npm command to include the variables and then it works as expected.

PWD=password node index.js

I can set these without committing them locally, so I can live with this solution for the moment. Still not sure why the dotenv variables are not accepted though.

NewVigilante
  • 1,291
  • 1
  • 7
  • 23