node.js nodemailer gmail error

Darlyn picture Darlyn · Sep 8, 2015 · Viewed 16.8k times · Source

Im trying to send a email using node.js - nodemailer module , my whole code looks like

var http=require("http");
var nodemailer=require("nodemailer");

http.createServer(function(req,res){
     res.writeHead(200, {"Content-Type": "text/plain"});
     var transporter = nodemailer.createTransport('SMTP',{
    service: 'gmail',
    auth: {
        user: '[email protected]', // my mail
        pass: 'mypassword'
    }
});
var mailOptions = {
    from: 'Fred Foo ✔ <[email protected]>', // sender address 
    to: '[email protected]', // the same mail = want to send it to myself
    subject: 'Hello ✔', // Subject line 
    text: 'Hello world ✔', // plaintext body 
    html: '<b>Hello world ✔</b>' // html body 
};
    transporter.sendMail(mailOptions, function(error, info){
    if(error){
        return console.log(error);
    }
    console.log('Message sent: ' + info.response);

});
    res.end("hllo");
}).listen(4000)

but when i run it , it throws "login" error , altought my name/pass are correct the whole error looks like this

{ [AuthError: Invalid login - 534-5.7.14 <https://accounts.google.com/ContinueSi
gnIn?sarp=1&scc=1&plt=AKgnsbtWV
534-5.7.14 FHw6FqlXkg3nIQW7AtsO7o0r-4EtD7eTtnrgHeXxeULYqUtHjrnTB9R647mlR7GY9Z4lS
z
534-5.7.14 WuzYYnNwNBjIyrjNzHk-o9sIV_vIsbjgF5HsFPtJl5S0dSf3eyNCX_E8tQ_Z6s4Z1mUe5
i
534-5.7.14 p8Y5YagzdN5RSkjpB1yTu3TfWHSTLisczIZphhLfhlz-v86tAB8MX-4jRqA51hfJYy0ko
F
534-5.7.14 qytMeIbs1kUZEJzPIabmjRi87DIk> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 cx1sm17733852wib.0 - gs
mtp]
  name: 'AuthError',
  data: '534-5.7.14 <https://accounts.google.com/ContinueSignIn?sarp=1&scc=1&plt
=AKgnsbtWV\r\n534-5.7.14 FHw6FqlXkg3nIQW7AtsO7o0r-4EtD7eTtnrgHeXxeULYqUtHjrnTB9R
647mlR7GY9Z4lSz\r\534-5.7.14 WuzYYnNwNBjIyrjNzHk-o9sIV_vIsbjgF5HsFPtJl5S0dSf3ey
NCX_E8tQ_Z6s4Z1mUe5i\r\n534-5.7.14 p8Y5YagzdN5RSkjpB1yTu3TfWHSTLisczIZphhLfhlz-v
86tAB8MX-4jRqA51hfJYy0koF\r\n534-5.7.14 qytMeIbs1kUZEJzPIabmjRi87DIk> Please log
 in via your web browser and\r\n534-5.7.14 then try again.\r\n534-5.7.14  Learn
more at\r\n534 5.7.14  https://support.google.com/mail/answer/78754 cx1sm1773385
2wib.0 - gsmtp',
  stage: 'auth' }

is there something i have to set in my gmail acc to allow this to send email from that or how can i fix it

Answer

Frederico Jesus picture Frederico Jesus · Sep 8, 2015

Try installing nodemailer-smtp-transport and then use it inside your createTransport function.

var smtpTransport = require('nodemailer-smtp-transport');

var transport = nodemailer.createTransport(smtpTransport({
    service: 'gmail',
    auth: {
        user: '[email protected]', // my mail
        pass: 'mypassword'
    }
}));

And try also while in your google account enabling this: https://www.google.com/settings/security/lesssecureapps

I really think this should be your auth problem