const nodeMailer = require("nodemailer");
const { google } = require('googleapis');
const oAuth2Client = new google.auth.OAuth2(
process.env.CLIENT_ID,
process.env.CLEINT_SECRET,
process.env.REDIRECT_URI
);
oAuth2Client.setCredentials({ refresh_token: process.env.REFRESH_TOKEN });
const sendEmail = async (options) => {
const accessToken = await oAuth2Client.getAccessToken();
const transporter = nodeMailer.createTransport({
host: process.env.SMTP_HOST,
port: process.env.SMTP_PORT,
secure: true,
service: process.env.SMTP_SERVICE,
auth: {
type: 'OAuth2',
user: process.env.SMTP_MAIL,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLEINT_SECRET,
refreshToken: process.env.REFRESH_TOKEN,
accessToken: accessToken,
},
});
const mailOptions = {
from: process.env.SMPT_MAIL,
to: options.email,
subject: options.subject,
text: options.message,
};
await transporter.sendMail(mailOptions,function(err,info){
if(err){
console.log(err)
} else {
console.log('success')
}
});
};
module.exports = sendEmail;
when it says no refresh token , but each time when new req is made oAuth2Client creates new token and that is being passed into setting the credentials for refresh token, but ultimately the transporter is unable to process the mailing part.
In https://developers.google.com/oauthplayground : i've set API as "https://mail.google.com" and "Use your own OAuth credentials"
also the temp gmail account which i created for mailing purpose i activated Less secure app access to "On" mode.
can anyone help me to pass this error? reg