Nodemailer提供错误

时间:2018-01-22 14:26:54

标签: javascript node.js mongodb smtp nodemailer

我使用Nodemailer通过Gmail服务发送忘记密码邮件。我试图在StackOverflow之前达到相同的错误,但我找不到解决方案。请帮助我,我不知道为什么它会给出错误,

  

" TypeError:无法创建属性'邮件程序'在字符串' smtpTransport'"

以下是我的代码 -

var nodemailer = require('nodemailer');

app.post('/forgot', function(req, res, next) {
  async.waterfall([
    function(done) {
      crypto.randomBytes(20, function(err, buf) {
        var token = buf.toString('hex');
        done(err, token);
      });
    },
    function(token, done) {
      User.findOne({ email: req.body.email }, function(err, user) {
        if (!user) {
          req.flash('error', 'No account with that email address exists.');
          return res.redirect('/forgot');
        }

        user.resetPasswordToken = token;
        user.resetPasswordExpires = Date.now() + 3600000; // 1 hour

        user.save(function(err) {
          done(err, token, user);
        });
      });
    },
    function(token, user, done) {
        console.log(token, "Token");
        console.log(user, "user")
      var smtpTransport = nodemailer.createTransport('SMTP', {
        service: 'gmail',
        auth: {
          user: 'abc@gmail.com',
          pass: '123456'
        }
      });
      var mailOptions = {
        to: user.email,
        from: 'myproducts@mailinator.com',
        subject: 'My Products Password Reset',
        text: 'You are receiving this because you (or someone else) have requested the reset of the password for your account.\n\n' +
          'Please click on the following link, or paste this into your browser to complete the process:\n\n' +
          'http://' + req.headers.host + '/reset/' + token + '\n\n' +
          'If you did not request this, please ignore this email and your password will remain unchanged.\n'
      };
      smtpTransport.sendMail(mailOptions, function(err) {
        req.flash('info', 'An e-mail has been sent to ' + user.email + ' with further instructions.');
        done(err, 'done');
      });
    }
  ], function(err) {
    if (err) return next(err);
    res.redirect('/forgot');
  });
});

错误是这样的 -

  

/home/cis/Desktop/myproducts/node_modules/mongodb/lib/utils.js:132         扔错了;         ^

     

TypeError:无法创建属性'邮件程序'在字符串' smtpTransport'       在Mail(/home/cis/Desktop/myproducts/node_modules/nodemailer/lib/mailer/index.js:45:33)       at Object.module.exports.createTransport(/home/cis/Desktop/myproducts/node_modules/nodemailer/lib/nodemailer.js:52:14)       在/home/cis/Desktop/myproducts/app.js:185:38       at nextTask(/home/cis/Desktop/myproducts/node_modules/async/dist/async.js:5310:14)       在下一个(/home/cis/Desktop/myproducts/node_modules/async/dist/async.js:5317:9)       at /home/cis/Desktop/myproducts/node_modules/async/dist/async.js:958:16       在/home/cis/Desktop/myproducts/app.js:177:11       at /home/cis/Desktop/myproducts/node_modules/mongoose/lib/model.js:3913:16       在模型。$ ​​__ save.error(/home/cis/Desktop/myproducts/node_modules/mongoose/lib/model.js:342:7)       at /home/cis/Desktop/myproducts/node_modules/kareem/index.js:297:21       在下一个(/home/cis/Desktop/myproducts/node_modules/kareem/index.js:209:27)       在Kareem.execPost(/home/cis/Desktop/myproducts/node_modules/kareem/index.js:217:3)       at _cb(/home/cis/Desktop/myproducts/node_modules/kareem/index.js:289:15)       at $ __ handleSave(/home/cis/Desktop/myproducts/node_modules/mongoose/lib/model.js:280:5)       at /home/cis/Desktop/myproducts/node_modules/mongoose/lib/model.js:208:9       at args.push(/home/cis/Desktop/myproducts/node_modules/mongodb/lib/utils.js:404:72)   [nodemon]应用程序崩溃 - 在开始之前等待文件更改...

3 个答案:

答案 0 :(得分:0)

Nodemailer结构已更改,请尝试使用:

smtpTransport = nodemailer.createTransport({
service: 'Gmail', 
auth: {
xoauth2: xoauth2.createXOAuth2Generator({
user: 'youremail@gmail.com',
//and other stuff here
 });
 }
});

答案 1 :(得分:0)

a

试试这个。

答案 2 :(得分:0)

Link to a similar question

Gmail / Google应用电子邮件服务需要OAuth2进行身份验证。 PLAIN文本密码需要在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"                              
  }
});
相关问题