1
votes

I'm using Nodemailer for sending a forget password mail with Gmail service.I tried to reach to the same error earlier in the StackOverflow, but I couldn't find the solution. Please help me, I have no idea why it is giving error like,

"TypeError: Cannot create property 'mailer' on string 'smtpTransport'"

Here is my code below-

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: '[email protected]',
          pass: '123456'
        }
      });
      var mailOptions = {
        to: user.email,
        from: '[email protected]',
        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');
  });
});

And the error is something like this-

/home/cis/Desktop/myproducts/node_modules/mongodb/lib/utils.js:132 throw err; ^

TypeError: Cannot create property 'mailer' on string 'smtpTransport' at 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) at /home/cis/Desktop/myproducts/app.js:185:38 at nextTask (/home/cis/Desktop/myproducts/node_modules/async/dist/async.js:5310:14) at next (/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 at /home/cis/Desktop/myproducts/app.js:177:11 at /home/cis/Desktop/myproducts/node_modules/mongoose/lib/model.js:3913:16 at model.$__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 at next (/home/cis/Desktop/myproducts/node_modules/kareem/index.js:209:27) at 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] app crashed - waiting for file changes before starting...

3
@abdulbarik, this duplicate question helped me to find the solution to this problem. var smtpTransport = nodemailer.createTransport("smtps://youruser%40gmail.com:"+encodeURIComponent('yourpass#123') + "@smtp.gmail.com:465");Aditya Rajak

3 Answers

0
votes

The Nodemailer structure has been changed, try use this :

smtpTransport = nodemailer.createTransport({
service: 'Gmail', 
auth: {
xoauth2: xoauth2.createXOAuth2Generator({
user: '[email protected]',
//and other stuff here
 });
 }
});
0
votes
var nodemailer = require("nodemailer");

var smtpTransport = nodemailer.createTransport({
   service: "Yahoo",  // sets automatically host, port and connection security settings
   auth: {
       user: "[email protected]",
       pass: "xxxxxxxxxxxx"
   }
});

function mail(messageBody) {
    let messageBodyJson = JSON.stringify(messageBody)
    smtpTransport.sendMail({  //email options
        from: "[email protected]", // sender address.  Must be the same as authenticated user if using Gmail.
        to: "[email protected]", // receiver
        subject: "Emailing with nodemailer", // subject
        text: messageBodyJson // body
     }, function(error, response){  //callback
        if(error){
           console.log("error",error);
        }else{
            console.log(response);
        }

     //    smtpTransport.close(); // shut down the connection pool, no more messages.  Comment this line out to continue sending emails.
     });
}

 mail("your mail message");

Try this.

0
votes

Link to a similar question

Gmail / Google app email service requires OAuth2 for authentication. PLAIN text password will require disabling security features manually on the google account.

To use OAuth2 in Nodemailer, refer: https://nodemailer.com/smtp/oauth2/

Sample code:

var email_smtp = nodemailer.createTransport({      
  host: "smtp.gmail.com",
  auth: {
    type: "OAuth2",
    user: "[email protected]",
    clientId: "CLIENT_ID_HERE",
    clientSecret: "CLIENT_SECRET_HERE",
    refreshToken: "REFRESH_TOKEN_HERE"                              
  }
});