缺少"PLAIN"节点制作者的凭据

igo*_*olo 9 javascript scripting node.js nodemailer server

我正在尝试在联系表单中使用nodemailer来接收反馈并将其直接发送到电子邮件.这是下面的表格.

<form method="post" action="/contact">
      <label for="name">Name:</label>
      <input type="text" name="name" placeholder="Enter Your Name" required><br>
      <label for="email">Email:</label>
      <input type="email" name="email" placeholder="Enter Your Email" required><br>
      <label for="feedback">Feedback:</label>
      <textarea name="feedback" placeholder="Enter Feedback Here"></textarea><br>
      <input type="submit" name="sumbit" value="Submit">
</form>
Run Code Online (Sandbox Code Playgroud)

这就是服务器端的请求

app.post('/contact',(req,res)=>{
let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'user@gmail.com',
        password: 'password'
    }
});
var mailOptions = {
    from: req.body.name + '&lt;' + req.body.email + '&gt;',
    to: 'bantspl@gmail.com',
    subject: 'Plbants Feedback',
    text: req.body.feedback 
};
transporter.sendMail(mailOptions,(err,res)=>{
    if(err){
        console.log(err);
    }
    else {

    }
});
Run Code Online (Sandbox Code Playgroud)

我收到了错误Missing credentials for "PLAIN".非常感谢任何帮助,非常感谢.

小智 25

你有

auth: {
    user: 'user@gmail.com',
    password: 'password'
}
Run Code Online (Sandbox Code Playgroud)

但是你应该写这个

auth: {
    user: 'user@gmail.com',
    pass: 'password'
}
Run Code Online (Sandbox Code Playgroud)

只需重命名密码即可通过。

  • 你值得获奖!这救了我谢谢 (5认同)
  • 简单直接,活命救星。只需重命名密码即可通过。解决了这个问题。您可能写错了密码而不是直接通过 (2认同)

the*_*ite 13

我能够通过使用数字3,设置3LO身份验证,例如来自nodemailer文档(链接:https://nodemailer.com/smtp/oauth2/ )来解决此问题.我的代码看起来像这样:

let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        type: 'OAuth2',
        user: 'user@example.com',
        clientId: '000000000000-xxx0.apps.googleusercontent.com',
        clientSecret: 'XxxxxXXxX0xxxxxxxx0XXxX0',
        refreshToken: '1/XXxXxsss-xxxXXXXXxXxx0XXXxxXXx0x00xxx',
        accessToken: 'ya29.Xx_XX0xxxxx-xX0X0XxXXxXxXXXxX0x'
    }
});
Run Code Online (Sandbox Code Playgroud)

如果您查看我上面提到的链接中的示例,您可以看到存在"expires"属性,但在我的代码中我没有包含它,它仍然可以正常工作.

要获取clientId,clientSecret,refreshToken和accessToken,我刚看了这个视频https://www.youtube.com/watch?v=JJ44WA_eV8E.

我不知道这对你是否仍然有用.


Luk*_* V. 7

我们不需要为此降低 Google 帐户的安全性。这对我在本地主机和实时服务器上有效。版本:node 12.18.4、nodemailer ^6.4.11

第 1 步:按照本视频中设置 Google Api 访问的方式进行操作,并忽略他的代码(它对我不起作用):https://www.youtube.com/watch ?v=JJ44WA_eV8E

步骤 2:通过以下方式安装 nodemailer 和 dotenv 后,在主应用程序文件中尝试此代码npm i nodemailer dotenv

    require('dotenv').config();  //import and config dotenv to use .env file for secrets
    const nodemailer = require('nodemailer');

    function sendMessage() {
      try {
        // mail options
        const mailOptions = {
          from: "MySite@mysite.com",
          to: "my_gmail@gmail.com",
          subject: "Hey there!",
          text: "Whoa! It freakin works now."
        };
        // here we actually send it
        transporter.sendMail(mailOptions, function(err, info) {
          if (err) {
            console.log("Error sending message: " + err);
          } else {
            // no errors, it worked
            console.log("Message sent succesfully.");
          }
        });
      } catch (error) {
        console.log("Other error sending message: " + error);
      }
    }

    // thats the key part, without all these it didn't work for me
    let transporter = nodemailer.createTransport({
      host: 'smtp.gmail.com',
      port: 465,
      secure: true,
      service: 'gmail',
      auth: {
            type: "OAUTH2",
            user: process.env.GMAIL_USERNAME,  //set these in your .env file
            clientId: process.env.OAUTH_CLIENT_ID,
            clientSecret: process.env.OAUTH_CLIENT_SECRET,
            refreshToken: process.env.OAUTH_REFRESH_TOKEN,
            accessToken: process.env.OAUTH_ACCESS_TOKEN,
            expires: 3599
      }
    });

    // invoke sending function
    sendMessage();
Run Code Online (Sandbox Code Playgroud)

上述代码的文件.env应类似于以下内容:

GMAIL_USERNAME=your_mail@gmail.com
GMAIL_PASSWORD=lakjrfnk;wrh2poir2039r
OAUTH_CLIENT_ID=vfo9u2o435uk2jjfvlfdkpg284u3.apps.googleusercontent.com
OAUTH_CLIENT_SECRET=og029503irgier0oifwori
OAUTH_REFRESH_TOKEN=2093402i3jflj;geijgp039485puihsg[-9a[3;wjenjk,ucv[3485p0o485uyr;ifasjsdo283wefwf345w]fw2984329oshfsh
OAUTH_ACCESS_TOKEN=owiejfw84u92873598yiuhvsldiis9er0235983isudhfdosudv3k798qlk3j4094too283982fs
Run Code Online (Sandbox Code Playgroud)

  • 您知道当您搜索这个简单的解决方案两天但什么也没找到时的感觉。当您将其发布到此处时,会发现您上面的 3 个人已经发布了它,只是您之前没有看到而已!:-P (5认同)

Ale*_*lex 7

对我来说,问题是我没有正确访问 .env 文件变量(我假设您也将电子邮件和密码存储在 .env 文件中)。我必须将其添加到文件的顶部:

const dotenv = require('dotenv');
dotenv.config();
Run Code Online (Sandbox Code Playgroud)

一旦我这样做了,我就可以填写凭证的“auth”部分,如下所示:

auth: {
  user: process.env.EMAIL_USERNAME,
  pass: process.env.EMAIL_PASSWORD
}
Run Code Online (Sandbox Code Playgroud)

当然,您需要将 EMAIL_USERNAME 和 EMAIL_PASSWORD 替换为您在 .env 文件中调用的这些变量。


小智 6

Gmail / Google 应用电子邮件服务需要 OAuth2 进行身份验证。纯文本密码需要在 google 帐户上手动禁用安全功能。

要在 Nodemailer 中使用 OAuth2,请参考:https ://nodemailer.com/smtp/oauth2/

示例代码:

var email_smtp = nodemailer.createTransport({      
  host: "smtp.gmail.com",
  auth: {
    type: "OAuth2",
    user: "youremail@gmail.com",
    clientId: "CLIENT_ID_HERE",
    clientSecret: "CLIENT_SECRET_HERE",
    refreshToken: "REFRESH_TOKEN_HERE"                              
  }
});
Run Code Online (Sandbox Code Playgroud)

如果您仍然只想使用纯文本密码,请在您的 google 帐户上禁用安全登录并使用如下:

var email_smtp = nodemailer.createTransport({      
  host: "smtp.gmail.com",
  auth: {
    type: "login", // default
    user: "youremail@gmail.com",
    pass: "PASSWORD_HERE"
  }
});
Run Code Online (Sandbox Code Playgroud)


alb*_*o56 6

当我尝试将邮件发送到MailHog时,就发生了这种情况,MailHog 是一个在端口 1025 上运行的开发本地邮件服务器,默认情况下不使用用户名/密码。

我的代码提供了空的用户名/密码,但实际上您根本不应该提供任何凭据。

这不起作用(导致“缺少“PLAIN”的凭据”):

const transport = require('nodemailer').createTransport({
  host: mailhog,
  port: 1025,
  secure: false,
  auth: {
    user: '',
    pass: '',
  },
});
Run Code Online (Sandbox Code Playgroud)

但这有效:

const transport = require('nodemailer').createTransport({
  host: mailhog,
  port: 1025,
  secure: false,
});
Run Code Online (Sandbox Code Playgroud)