Nodemailer stops with Missing credentials for "PLAIN"
Error: Missing credentials for "PLAIN"Short answer
Nodemailer tried to log in but had no usable credentials. Either auth is there with empty user or pass, or the server does not need a login and auth should be left out or set to false. A pre-3.0 style OAuth block also causes it; use the current OAuth2 form.
Fix it fast
1Find out which cause you have
Check which auth values Nodemailer really receives. Print whether each is set, not the value.
node -e "console.log(Boolean(process.env.SMTP_USER), Boolean(process.env.SMTP_PASS))"2Match what you see, then jump to its fix
- 1
The SMTP relay allows your IP, and user is empty or absentThe server needs no login, but an auth object is still sentFix - 2
Gmail OAuth, and the settings come from an old tutorialOAuth set up the pre-3.0 wayFix - 3
Login is required, and printing whether each value is set gives falseuser or pass is undefined because the environment file was not loadedFix
- 1
What does the error look like?
Error: Missing credentials for "PLAIN"
Cause 1 of 3
The server needs no login, but an auth object is still sent
1Check
The docs say that if you omit auth entirely, Nodemailer sends without authentication. An auth object with an empty user does not do that.
grep -n "auth" mailer.js2Fix
Remove the auth block, or set auth to false as the maintainer advises.
const transporter = nodemailer.createTransport({
host: 'smtp.example.com',
port: 587,
secure: false,
auth: false,
});3Verify
Send a test message.
node send-test.jsConfirmed by nodemailer.com
Cause 2 of 3
OAuth set up the pre-3.0 way
1Check
The maintainer says legacy OAuth usage has not worked since Nodemailer 3.0.0.
npm ls nodemailer2Fix
Use auth.type OAuth2 with the user, clientId, clientSecret and refreshToken fields shown in the OAuth2 docs.
const transporter = nodemailer.createTransport({
host: 'smtp.gmail.com',
port: 465,
secure: true,
auth: {
type: 'OAuth2',
user: 'user@example.com',
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
refreshToken: process.env.REFRESH_TOKEN,
},
});3Verify
Send a test message.
node send-test.jsConfirmed by nodemailer.com
Cause 3 of 3
user or pass is undefined because the environment file was not loaded
1Check
The auth object needs user and pass for a normal login. Variables from a .env file exist only if something loaded the file. Node.js can do it with --env-file, and it throws if the file is missing.
node --env-file=.env -e "console.log(Boolean(process.env.SMTP_USER))"2Fix
Start the app with the env file and confirm the names match what mailer.js reads.
node --env-file=.env server.js3Verify
Run the check again. Both values should print true.
node --env-file=.env -e "console.log(Boolean(process.env.SMTP_USER), Boolean(process.env.SMTP_PASS))"Confirmed by nodejs.org
How do I stop it coming back?
- Fail at start-up when SMTP_USER or SMTP_PASS is missing instead of building a transport with undefined.
- Build the auth block only when a user is configured.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
Angular build says it cannot resolve '@angular/localize/init'
Can't resolve '@angular/localize/init' in '/usr/src/app/src'
The build imports @angular/localize, and that package is not in node_modules.
2 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fix