DEV Community

Discussion on: How to implement email functionality with Node.js, React.js, Nodemailer, and OAuth2

Collapse
 
jlong4223 profile image
Jared Long

Hey happy to try and help. What does your code look like?

Collapse
 
techspark_55 profile image
Md Nasir Uddin

**import express from "express";
import expressAsyncHandler from "express-async-handler";
import { google } from "googleapis";
import nodemailer from "nodemailer";
import dotenv from "dotenv";

dotenv.config();
const testRouter = express.Router();
// eslint-disable-next-line no-undef
const client_id = process.env.GMAIL_CLIENT_ID;
// eslint-disable-next-line no-undef
const client_secret = process.env.GMAIL_CLIENT_SECRET;
// eslint-disable-next-line no-undef
const redirect_uri = process.env.GMAIL_REDIRECT_URI;
// eslint-disable-next-line no-undef
const refresh_Token = process.env.GMAIL_REFRESH_TOKEN;
// eslint-disable-next-line no-undef
const myEmail = process.env.EMAIL;
// eslint-disable-next-line no-undef
const outLookEmail = process.env.OUTLOOKEMAIL;
// eslint-disable-next-line no-undef
const outLookPass = process.env.OUTLOOKPASS;

console.log(client_secret);

const oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uri
);
oAuth2Client.setCredentials({
refresh_token: refresh_Token,
});

testRouter.post(
"/email",
expressAsyncHandler(async (req, res) => {
async function send_mail() {
const access_token = await oAuth2Client.getAccessToken();
const transport = nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: myEmail,
clientId: client_id,
clinet_secret: client_secret,
refreshToken: refresh_Token,
accessToken: access_token,
},
tls: {
rejectUnauthorized: false,
},
});
// there is problem with this approach when verifying the transport is says clinent secret is missing;
transport.verify((err, success) => {
err
? console.log(err)
: console.log(=== Server is ready to take messages: ${success} ===);
});
const mailOptions = {
from: RareCarry ${myEmail},
to: "example123@gmail.com",
subject: "Your Order has been placed! ",
// attachment: attch,
text: Hello ${name} your order ID: . Thank you for shopping with us!. we will send your purchase details & invoice soon.,
html: <h2>Hello, ${name}</h2>
<h3>Your order ID:</h3>
<p>Thank you for shopping with us!. we will send your purchase details & invoice soon</p>
<em>Please visit our blog and news to get a discount deal. <a href='https://rarecarry.com'>RareCarry</a></em>
,
};
transport.sendMail(mailOptions, (error, result) => {
if (error) {
console.log("Error:", error);
} else {
console.log("Success: ", result);
}
transport.close();
});
}
send_mail();
res.send({ message: "Hi!" });
})
);

testRouter.post(
"/outlook",
expressAsyncHandler(async (req, res) => {
const transport = nodemailer.createTransport({
service: "hotmail",
auth: {
user: outLookEmail,
pass: outLookPass,
},
tls: {
rejectUnauthorized: false,
},
});
const mailOptions = {
from: RareCarry ${outLookEmail},
to: "careerit29@gmail.com",
// cc:'careerit29@gmail.com',
subject: "Your Order has been placed! ",
// attachment: attch,
text: Hello Nasir your order ID: . Thank you for shopping with us!. we will send your purchase details & invoice soon.,
html: <h2>Hello, Nasir</h2>
<h3>Your order ID:</h3>
<p>Thank you for shopping with us!. we will send your purchase details & invoice soon</p>
<em>Please visit our blog and news to get a discount deal. <a href='https://rarecarry.com'>RareCarry</a></em>
,
};

transport.sendMail(mailOptions, (err, result) => {
  if (err) {
    console.log(err);
  } else {
    console.log("Message sent:", result);
  }
});
transport.close();
res.send({ message: "HI!!!" });
Enter fullscreen mode Exit fullscreen mode

})
);

export default testRouter;**

Thread Thread
 
jlong4223 profile image
Jared Long • Edited

Awesome thanks. One thing that I see is that your client secret key is misspelled within your createTransport object but I think it should be camelCase as well. Hopefully that does the trick :)

clinet_secret: client_secret -> clientSecret: client_secret

and/or if you wanted to get fancy with new es6 syntax (not necessary at all):

define your client secret camelCase as well
const clientSecret = process.env.GMAIL_CLIENT_SECRET;

and within you transporter object, you could just define like so (could do with most of your variables):

const transport = nodemailer.createTransport({
service: "gmail",
auth: {
      type: "OAuth2",
      user: myEmail,
      clientId: client_id,
      clientSecret,                              <-------------------
      refreshToken: refresh_Token,
      accessToken: access_token,
},
Enter fullscreen mode Exit fullscreen mode
Thread Thread
 
techspark_55 profile image
Md Nasir Uddin

Thank you so much Jared, it's work. Great tutorial.