-
Notifications
You must be signed in to change notification settings - Fork 2
/
mail.js
50 lines (41 loc) · 1.29 KB
/
mail.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
45
46
47
48
49
const nodemailer = require("nodemailer");
const { google } = require("googleapis");
const { OAuth2 } = google.auth
const {
MAILER_CLIENT_ID,
MAILER_CLIENT_SECRET,
MAILER_REFRESH_TOKEN,
MAILER_ADDR,
} = process.env
module.exports = async (email, code) => {
try {
const oAuth2Client = new OAuth2(
MAILER_CLIENT_ID,
MAILER_CLIENT_SECRET,
"https://developers.google.com/oauthplayground"
)
oAuth2Client.setCredentials({
refresh_token: MAILER_REFRESH_TOKEN
})
const accessToken = await oAuth2Client.getAccessToken()
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
type: 'OAuth2',
user: MAILER_ADDR,
clientId: MAILER_CLIENT_ID,
clientSecret: MAILER_CLIENT_SECRET,
refreshToken: MAILER_REFRESH_TOKEN,
accessToken
}
})
return transporter.sendMail({
from: MAILER_ADDR,
to: email,
subject: "1DBM email validation",
text: `${code} is your validation code. Enter this code on your first login to enable your account`
})
} catch (error) {
throw error
}
}