Search code examples
oauthscopesmtpgmailnodemailer

Which scope is required to send emails through gmail oauth in nodemailer?


I have setup the authentication of google accounts and I'm able to get the access and refresh token of users but when I set the scope to only https://www.googleapis.com/auth/gmail.send it doesn't work but when I set the authentication scope to https://mail.google.com/ then it works without problem. What is going on here? Why can't I just use the send scope for sending emails via nodemailer? Here is my code:-

var nodemailer = require('nodemailer');

let transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 587,
    secure: false,
    requireTLS: true,
    auth: {
        type: 'OAuth2',
        clientId: 'my_client_id',
        clientSecret: 'my_client_secret'
    }
});

transporter.sendMail({
    from: '[email protected]',
    to: '[email protected]',
    subject: 'Message',
    text: 'Hi there',
    auth: {
        user: '[email protected]',
        refreshToken: 'user_refresh_token',
        accessToken: 'user_access_token'
    }
}).catch(error => {
    console.log(error);
});

Solution

  • There is a note in the Nodemailer documentation saying the scope has to be 'https://mail.google.com/': https://nodemailer.com/smtp/oauth2/#troubleshooting

    And it is actually specified by Google here: https://developers.google.com/gmail/imap/xoauth2-protocol