I'm trying to make a simple mail sender using Nodemailer 3, the GMail API whit OAuth2 autentication.
Here's my script:
var serverConfig = {
gmail: {
client_user : 'my@email.com',
client_id : '349...myClientId',
secret : 'mysecret..123jd123',
refresh_token : 'x/xxxxxxxxxxxxxx-reZuEMeSuJaSERmCVY',
access_token : 'xxxxxxxxxxxxxxxxx',
expires: '3599'
}
}
// 3LO authentication https://nodemailer.com/smtp/oauth2/#example-3
var transporter = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: serverConfig.gmail.client_user,
clientId: serverConfig.gmail.client_id,
clientSecret: serverConfig.gmail.secret,
refreshToken: serverConfig.gmail.refresh_token
},
});
module.exports = {
"send": function(_from,_to,_subject,_html,_text){
// setup email data with unicode symbols
var mailOptions = {
from: _from,
to: _to, // list of receivers
subject: _subject, // Subject line
html: _html, // html body
text: _text // plain text body
};
transporter.sendMail(mailOptions, function(error, info){
if (error) {
return console.log(error);
}
console.log('Message ' + info.messageId + ' sent: %s' + info.response);
})
}
}
When I force the access_token in the auth object, the email is sent without any problem. But, when I don't specify the access_token
, only de refresh_token
, I'm getting this error:
{ Error: Invalid status code 401
at ClientRequest.req.on.res (/myproject/node_modules/nodemailer/lib/fetch/index.js:193:23)
at emitOne (events.js:96:13)
at ClientRequest.emit (events.js:188:7)
at HTTPParser.parserOnIncomingClient [as onIncoming] (_http_client.js:474:21)
at HTTPParser.parserOnHeadersComplete (_http_common.js:99:23)
at TLSSocket.socketOnData (_http_client.js:363:20)
at emitOne (events.js:96:13)
at TLSSocket.emit (events.js:188:7)
at readableAddChunk (_stream_readable.js:176:18)
at TLSSocket.Readable.push (_stream_readable.js:134:10)
type: 'FETCH',
sourceUrl: 'https://accounts.google.com/o/oauth2/token',
code: 'EAUTH',
command: 'AUTH XOAUTH2' }
Maybe it's too late, but just answering here incase someone comes across the same issue. My problem rectified when the refreshToken was reset. This article gives great instructions for the whole process