I am trying to send mail using OAuth2.0 from my gmail tp the user mail for confirming registration. The below is my code. There is no error. But no output also.
I dont have complete understanding of the code. Somehow I managed to construct them by referring so many websites.
I am stuck now. Kindly let me know the solution.
const dotenv = require("dotenv");
const nodemailer = require("nodemailer");
const oauth_link = "https://developers.google.com/oauthplayground";
const { OAuth2Client } = require("google-auth-library");
const opn = require("opn");
const destroyer = require("server-destroy");
const http = require("http");
const https = require("https");
const url = require("url");
const { google } = require("googleapis");
dotenv.config();
const { EMAIL, MAILING_ID, MAILING_REFRESH, MAILING_SECRET, MAILING_ACCESS } =
process.env;
exports.sendVerificationEmail = async (email, name, url) => {
console.log("inside send verification email");
const oauth2Client = new google.auth.OAuth2(
MAILING_ID,
MAILING_SECRET,
oauth_link
);
// Access scopes for read-only Drive activity.
const scopes = ["https://www.googleapis.com/auth/drive.metadata.readonly"];
// Generate a url that asks permissions for the Drive activity scope
const authorizationUrl = oauth2Client.generateAuthUrl({
access_type: "offline",
scope: scopes,
include_granted_scopes: true,
});
let userCredential = null;
const server = http.createServer(async function (req, res) {
// Example on redirecting user to Google's OAuth 2.0 server.
if (req.url == "/") {
res.writeHead(301, { Location: authorizationUrl });
}
// Receive the callback from Google's OAuth 2.0 server.
if (req.url.startsWith("/oauth2callback")) {
// Handle the OAuth 2.0 server response
let q = url.parse(req.url, true).query;
if (q.error) {
// An error response e.g. error=access_denied
console.log("Error:" + q.error);
} else {
// Get access and refresh tokens (if access_type is offline)
let { tokens } = await oauth2Client.getToken(q.code);
oauth2Client.setCredentials(tokens);
/** Save credential to the global variable in case access token was refreshed.
* ACTION ITEM: In a production app, you likely want to save the refresh token
* in a secure persistent database instead. */
userCredential = tokens;
const tokenInfo = await oAuth2Client.getTokenInfo(
oAuth2Client.credentials.access_token
);
console.log("obtained tokeninfo");
console.log(tokenInfo);
//Sending mail
const smtp = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: EMAIL,
clientId: MAILING_ID,
clientSecret: MAILING_SECRET,
refreshToken: tokenInfo.refreshToken,
accessToken: tokenInfo.accessToken,
},
});
console.log("smtp " + smtp);
const mailOptions = {
from: EMAIL,
to: email,
subject: "email verification",
html: '<div><div style="max-width:500px;margin-bottom:1rem;align-items:center;display:flex"><img style="width:40px" src="https://seeklogo.com/images/F/facebook-icon-circle-logo-09F32F61FF-seeklogo.com.png" alt=""><span style="font-family:sans-serif;color:#3266a8;font-weight:700;padding-left:10px">Action Required : Activate your account</span></div><div style="border-top:1px solid;border-bottom:1px solid;border-color:#deb887;padding:10px;padding-bottom:20px"><div style="height:35px"><span style="font-weight:700;font-family:sans-serif">Hello ${name}</span></div><div style="height:28px;font-family:sans-serif;padding-bottom:20px"><span>You recently created a profile on our app. Please confirm your account</span></div><div style=""><a href={url} style="background-color:#3266a8;color:#fff;border-radius:10px;font-weight:700;font-family:sans-serif;text-decoration:none;font-size:15px;text-align:center;padding:9px;margin-bottom:1.5em">Confirm Your Account</a></div></div></div>',
};
console.log("mailOptions" + mailOptions);
smtp.sendMail(mailOptions, (err, res) => {
if (err) return err;
return res;
});
}
}
// Example on revoking a token
if (req.url == "/revoke") {
//...
}
res.end();
});
};