On hitting send on webpage of a react contact form, mail doesn’t send and the error No refresh token is set
is returned in the terminal
require ('dotenv').config();
const express = require("express")
const app = express()
const cors = require("cors")
const nodemailer = require('nodemailer');
const { google } = require('googleapis');
const CLIENT_ID = process.env.CLIENT_ID
const CLIENT_SECRET = process.env.CLIENT_SECRET
const REDIRECT_URI = process.env.REDIRECT_URI
const REFRESH_TOKEN = process.env.REFRESH_TOKEN
const oAuth2Client = new google.auth.OAuth2(
CLIENT_ID,
CLIENT_SECRET,
REDIRECT_URI
);
oAuth2Client.setCredentials({ refresh_token: REFRESH_TOKEN });
// the below code didn't fix problem, hence commented
// oAuth2Client.refreshAccessToken(function(err, tokens){
// console.log(tokens)
// oAuth2Client.credentials = {access_token : tokens.access_token}
// callback(oAuth2Client);
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.use(cors())
app.get('/', cors(), async (req, res) =>{
res.send("this is working")
})
app.post("/send_mail", cors(), async (req, res) => {
res.send('ok')
const output = `
<p>You have a new contact request</p>
<h3>Contact Details</h3>
<ul>
<li>Name: ${req.body.name}</li>
<li>Email: ${req.body.email}</li>
</ul>
<h3>Message</h3>
<p>${req.body.message}</p>
`;
async function sendMail() {
try {
const accessToken = await oAuth2Client.getAccessToken();
const transport = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: process.env.USERNAME,
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
refreshToken: REFRESH_TOKEN,
accessToken: accessToken
},
});
const mailOptions = {
from: '"Web name" <[email protected]>',
to: '[email protected]',
subject: 'Hello from gmail using API',
text: 'Hello from gmail email using API',
html: output
};
const result = await transport.sendMail(mailOptions);
return result;
} catch (error) {
return error;
}
}
sendMail()
.then((result) => console.log('Email sent...', result))
.catch((error) => console.log(error.message));
})
app.listen(7000, () => console.log('Server started...'));
Email sent... Error: No refresh token is set.
[0] at OAuth2Client.getAccessTokenAsync (D:UsersUser NameDocumentswebdev 1. PROJECTSxxxapinode_modulesgoogle-auth-librarybuildsrcauthoauth2client.js:214:23)
[0] at OAuth2Client.getAccessToken (D:UsersUser NameDocumentswebdev 1. PROJECTSxxxapinode_modulesgoogle-auth-librarybuildsrcauthoauth2client.js:207:25)
[0] at sendMail (D:UsersUser NameDocumentswebdev 1. PROJECTSxxxapiserver.js:52:44)
[0] at D:UsersUser NameDocumentswebdev 1. PROJECTSxxxapiserver.js:82:1
[0] at Layer.handle [as handle_request] (D:UsersUser NameDocumentswebdev 1. PROJECTSxxxapinode_modulesexpresslibrouterlayer.js:95:5)
[0] at next (D:UsersUser NameDocumentswebdev 1. PROJECTSxxxapinode_modulesexpresslibrouterroute.js:137:13)
[0] at cors (D:UsersUser NameDocumentswebdev 1. PROJECTSxxxapinode_modulescorslibindex.js:188:7)
[0] at D:UsersUser NameDocumentswebdev 1. PROJECTSxxxapinode_modulescorslibindex.js:224:17
[0] at originCallback (D:UsersUser NameDocumentswebdev 1. PROJECTSxxxapinode_modulescorslibindex.js:214:15)
[0] at D:UsersUser NameDocumentswebdev 1. PROJECTSxxxapinode_modulescorslibindex.js:219:13