tonymx227
tonymx227

Reputation: 5451

Nodemailer and GMail with access tokens

I try to use Nodemailer to send an email with my GMail account but it doesn't work, it works in local but on my remote server I recieve an email from Google "Someone is using your account...."

How can I do ?

    exports.contact = function(req, res){
        var name = req.body.name;
        var from = req.body.from;
        var message = req.body.message;
        var to = '******@gmail.com';
        var transport = nodemailer.createTransport("SMTP", {
            service: 'Gmail',
            auth: {
                XOAuth2: {
                    user: "******@gmail.com",
                    clientId: "*****",
                    clientSecret: "******",
                    refreshToken: "******",
                }
            }
        });
        var options = {
            from: from,
            to: to, 
            subject: name,
            text: message
        }
        transport.sendMail(options, function(error, response) {
          if (error) {
            console.log(error);
          } else {
            console.log(response);
          }
          transport.close();
        });
    }

Upvotes: 0

Views: 1134

Answers (1)

Alex Netkachov
Alex Netkachov

Reputation: 13522

Check out the solution from Unable to send email via google smtp on centos VPS:

In my case, my script is on a VPS so I don't have a way to load any url with a browser. What I did: Changed my gmail pw. Gmail > Settings > Accounts. Then in Google Accounts they listed suspicious logins that were blocked by google (these were my script's attempted logins). Then I clicked the option "Yes, that was me". After that, my script worked (using the new pw).

Upvotes: 1

Related Questions