-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathemail_service.js
More file actions
50 lines (44 loc) · 1.2 KB
/
Copy pathemail_service.js
File metadata and controls
50 lines (44 loc) · 1.2 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
41
42
43
44
45
46
47
48
49
50
require('dotenv').config();
const nodemailer = require('nodemailer');
const { google } = require('googleapis');
const Oauth2 = google.auth.OAuth2;
// Create SMTP transporter
const createTransporter = async () => {
const oauth2Client = new Oauth2(
process.env.CLIENT_ID,
process.env.CLIENT_SECRET,
"https://developers.google.com/oauthplayground"
);
oauth2Client.setCredentials({
refresh_token: process.env.REFRESH_TOKEN
});
const accessToken = await new Promise((resolve, reject) => {
oauth2Client.getAccessToken((err, token) => {
if (err) {
reject("Failed to fetch the access token!!");
}
resolve(token);
});
});
return nodemailer.createTransport({
service: "gmail",
auth: {
type: "OAuth2",
user: process.env.EMAIL,
accessToken,
clientId: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
refreshToken: process.env.REFRESH_TOKEN
}
});
}
// Send email with the given options.
const sendEmail = async (emailOptions) => {
try {
const transporter = await createTransporter();
await transporter.sendMail(emailOptions);
} catch (err) {
console.log(err);
}
}
module.exports = sendEmail;