-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmailer.js
More file actions
122 lines (112 loc) · 3.73 KB
/
mailer.js
File metadata and controls
122 lines (112 loc) · 3.73 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const fs=require('fs');
const readline= require('readline');
const { google } = require('googleapis');
const SCOPES = [
'https://mail.google.com/',
'https://www.googleapis.com/auth/gmail.send',
];
const TOKEN_PATH = './src/config/mail_token.json';
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min) + min);
}
function base64Encode(message) {
return Buffer.from(message)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_');
}
function sendMail(auth, emailAddress) {
return new Promise((resolve, reject) => {
const gmail = google.gmail({ version: 'v1', auth });
const authentication_code = getRandomInt(100000, 999999).toString();
gmail.users.messages.send(
{
userId: 'me',
requestBody: {
raw: base64Encode(
'From: oldedusolution@gmail.com\n' +
`To: ${emailAddress}\n` +
'Subject: "UniUnity Authentication Code"\n' +
'MIME-Version: 1.0\n' +
'Content-Type: text/plain; charset="UTF-8"\n' +
'Content-Transfer-Encoding: message/rfc2822\n' +
'\n' +
`Authentication Code: ${authentication_code} \n`
),
},
},
(err, res) => {
if (err) {
console.log('The API returned an error:', err);
reject(err);
} else {
const messageId = res.data.id;
console.log(`Message sent with ID: ${messageId}`);
resolve(authentication_code);
}
}
);
});
}
async function authorize(credentials, callback, emailAddress) {
return new Promise((resolve, reject) => {
const { client_secret, client_id, redirect_uris } = credentials.web;
const oAuth2Client = new google.auth.OAuth2(
client_id,
client_secret,
redirect_uris[0]
);
fs.readFile(TOKEN_PATH, (err, token) => {
if (err) return getNewToken(oAuth2Client, callback, emailAddress);
oAuth2Client.setCredentials(JSON.parse(token));
callback(oAuth2Client, emailAddress)
.then((authentication_code) => resolve(authentication_code))
.catch((err) => reject(err));
});
});
}
function getNewToken(oAuth2Client, callback, emailAddress) {
const authUrl = oAuth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES,
});
console.log('Authorize this app by visiting this url:', authUrl);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question('Enter the code from that page here: ', (code) => {
rl.close();
oAuth2Client.getToken(code, (err, token) => {
if (err) return console.error('Error retrieving access token', err);
oAuth2Client.setCredentials(token);
fs.writeFile(TOKEN_PATH, JSON.stringify(token), (err) => {
if (err) return console.error(err);
console.log('Token stored to', TOKEN_PATH);
});
callback(oAuth2Client, emailAddress);
});
});
}
async function sendEmailWithAuthorization(emailAddress) {
return new Promise((resolve, reject) => {
fs.readFile('./src/config/mail_credentials.json', (err, content) => {
if (err) {
console.log('Error loading client secret file:', err);
reject(err);
}
authorize(JSON.parse(content), sendMail, emailAddress)
.then((authentication_code) => resolve(authentication_code))
.catch((err) => reject(err));
});
});
}
module.exports=sendEmailWithAuthorization;
if (require.main === module) {
// 테스트용 이메일 주소
sendEmailWithAuthorization('20221077@sungshin.ac.kr')
.then(code => console.log('인증 코드 전송 성공:', code))
.catch(err => console.error('에러 발생:', err));
}