-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransporter.js
44 lines (37 loc) · 1 KB
/
transporter.js
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
const nodemailer = require("nodemailer");
const { google } = require("googleapis");
const OAuth2 = google.auth.OAuth2;
require("dotenv").config();
const createTransporter = async () => {
const oauth2Client = new OAuth2(
process.env.CLIENT_ID,
process.env.CLIENT_SECRET,
process.env.REDIRECT_URI
);
oauth2Client.setCredentials({
refresh_token: process.env.REFRESH_TOKEN,
});
const accessToken = await new Promise((resolve, reject) => {
oauth2Client.getAccessToken((err, token) => {
if (err) {
reject("Failed to create access token :(");
}
resolve(token);
});
});
const transporter = 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,
},
from: process.env.EMAIL,
}
);
return transporter;
};
module.exports = createTransporter;