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: '[email protected]',
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);
};
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.