-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmail.js
More file actions
40 lines (35 loc) · 1.4 KB
/
Copy pathmail.js
File metadata and controls
40 lines (35 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const nodemailer = require('nodemailer');
const { google } = require('googleapis');
require('dotenv').config();
const CLIENT_ID = process.env.CLIENT_ID
const CLIENT_SECRET = process.env.CLIENT_SECRET
const REDIRECT_URI = 'https://developers.google.com/oauthplayground'
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 })
async function sendUserMail(email, subject, mailbody) {
const accessToken = await oAuth2Client.getAccessToken();
const transport = nodemailer.createTransport({
service: 'gmail',
auth: {
type: 'OAuth2',
user: 'fullstkwebdev@gmail.com',
clientId: CLIENT_ID,
clientSecret: CLIENT_SECRET,
refreshToken: REFRESH_TOKEN,
accessToken: accessToken
}
});
try {
const mailOptions = {
from: 'ProgrammingPracticeTests <fullstkwebdev@gmail.com>',
to: email ,
subject: subject,
html: mailbody
};
// send email
const sendMail = await transport.sendMail(mailOptions);
if (sendMail) return 'Email with change password link has been sent...';
} catch(err) {'Unsuccesful!', err}
}
module.exports = {sendUserMail}