You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用环境变量配置Nodemailer时遇PLAIN认证缺失凭证错误求助

Fixing Nodemailer's "Missing credentials for PLAIN" Error with Environment Variables

Hey there, let's work through this Nodemailer issue you're facing. The error clearly points to Nodemailer not getting valid credentials when you use environment variables, even though plaintext works. Here are the most likely fixes to try:

  • Verify Your Environment Variables Are Actually Loading
    First, let's rule out the simplest cause: your environment variables might not be accessible in your code.

    • Add a quick debug log right before you initialize Nodemailer to check if the values exist:
      console.log('Email:', process.env.EMAIL);
      console.log('Password:', process.env.PASSWORD);
      
      If either logs undefined, your variables aren't loading properly.
    • If you're using dotenv to load a .env file, make sure you call require('dotenv').config() at the very top of your app.js file—before you import any modules that use the variables. Also double-check that your .env file is in the root directory of your project, and that there are no typos in the variable names (e.g., EMAIL vs. Email or email—case matters!).
    • Don’t forget to restart your server after making changes to environment variables—they don’t reload automatically.
  • Check Your secret.js Import Setup
    If you’re using a secret.js file to wrap the environment variables, make sure the export and import are set up correctly:

    • In secret.js:
      module.exports = {
        email: process.env.EMAIL,
        password: process.env.PASSWORD
      };
      
    • In app.js, confirm you’re importing the right variables with the correct file path:
      const { email, password } = require('./secret'); // Adjust path if secret.js is in a subfolder
      
    • A common mistake here is misspelling the variable names (e.g., pass instead of password) or using the wrong relative path to secret.js.
  • Double-Check Gmail’s Authentication Settings
    Even though you enabled "Less secure app access", that feature is being phased out by Google and might not work for all accounts. Here are better alternatives:

    • Use an App Password (if 2FA is enabled): If you have two-factor authentication turned on for your Gmail account, you can’t use your regular password—you need to create an App Password. Go to your Google Account > Security > App Passwords (this option only appears if 2FA is on), generate a password for "Mail" and your device, then use that password in your environment variables instead of your regular password.
    • Avoid "Less secure app access": Google recommends disabling this option entirely. App Passwords or OAuth2 are more secure and reliable long-term.
  • Validate Your Nodemailer Configuration
    Make sure your transporter configuration is referencing the variables correctly. The auth object needs user and pass fields—don’t mix up the names:

    const transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: process.env.EMAIL, // Or the imported `email` variable from secret.js
        pass: process.env.PASSWORD // Or `password` from secret.js
      }
    });
    

    If you accidentally used username instead of user, or password instead of pass, that would trigger the "Missing credentials" error.

Start with the first step (debugging the environment variables) since that’s the most common culprit. Once you confirm the variables are loading correctly, the rest should fall into place.

内容的提问来源于stack exchange,提问作者Kieran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:33:46