-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.js
More file actions
171 lines (158 loc) · 5.74 KB
/
app.js
File metadata and controls
171 lines (158 loc) · 5.74 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const fs = require('fs');
const path = require('path');
const readline = require('readline');
const google = require('googleapis');
const googleAuth = require('google-auth-library');
const SCOPES = ['https://www.googleapis.com/auth/gmail.send'];
const EventEmitter = require('events');
var __clientSecret;
var __tokenFilePath;
var emailEmitter = new EventEmitter();
var gmailClass = (function () {
var _init = function (credentials, tokenFilePath, callback) {
__clientSecret = credentials;
__tokenFilePath = tokenFilePath;
authorize(__clientSecret, null, function (auth) {
if (auth) {
callback(null, 'Successfully Initialized');
emailEmitter.emit('init', {success: 'Successfully Initialized'});
} else {
callback('Error in initializing', null);
emailEmitter.emit('init', {error: 'Error in initializing'});
}
});
};
var _send = function (emailObject, callback) {
authorize(__clientSecret, null, function (auth) {
sendMail(auth, emailObject, 'plain', callback);
});
};
var _sendHTML = function (emailObject, callback) {
authorize(__clientSecret, null, function (auth) {
sendMail(auth, emailObject, 'html', callback);
});
};
var _sendWithToken = function (emailObject, credentials, token, callback) {
authorize(credentials, token, function (auth) {
sendMail(auth, emailObject, 'plain', callback);
});
};
var _sendHTMLWithToken = function (emailObject, credentials, token,callback) {
authorize(credentials, token, function (auth) {
sendMail(auth, emailObject, 'html', callback);
});
};
var _clearToken = function (callback) {
fs.unlink(__tokenFilePath, callback);
};
var _generateToken = function (credentials, code, callback) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
oauth2Client.getToken(code,callback);
};
var _generateUrl = function (credentials) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
return authUrl;
};
return {
init: _init,
send: _send,
sendHTML: _sendHTML,
sendWithToken: _sendWithToken,
sendHTMLWithToken: _sendHTMLWithToken,
clearToken: _clearToken,
generateUrl: _generateUrl,
generateToken: _generateToken,
event: emailEmitter
}
})();
function authorize(credentials, token, callback) {
var clientSecret = credentials.installed.client_secret;
var clientId = credentials.installed.client_id;
var redirectUrl = credentials.installed.redirect_uris[0];
var auth = new googleAuth();
var oauth2Client = new auth.OAuth2(clientId, clientSecret, redirectUrl);
if(token){
oauth2Client.credentials = token;
callback(oauth2Client);
}else {
fs.readFile(__tokenFilePath, function (err, token) {
if (err) {
getNewToken(oauth2Client, callback);
} else {
oauth2Client.credentials = JSON.parse(token);
callback(oauth2Client);
}
});
}
}
function makeBody(to, subject, message, type) {
var str = [];
if (type == 'html') {
str.push("Content-Type: text/html; charset=\"UTF-8\"\n")
} else {
str.push("Content-Type: text/plain; charset=\"UTF-8\"\n")
}
str.push("MIME-Version: 1.0\n");
str.push("Content-Transfer-Encoding: 7bit\n");
str.push("to: ", to, "\n");
str.push("subject: ", subject, "\n\n");
str.push(message);
str = str.join('');
var encodedMail = new Buffer(str).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
return encodedMail;
}
function sendMail(auth, emailObject, type, callback) {
var gmail = google.gmail('v1');
var raw = makeBody(emailObject.to, emailObject.subject, emailObject.message, type);
gmail.users.messages.send({
auth: auth,
userId: 'me',
resource: {
raw: raw
}
}, (error, success) => {
callback(error, success);
emailEmitter.emit('sent', {error, success});
});
}
function getNewToken(oauth2Client, callback) {
var authUrl = oauth2Client.generateAuthUrl({
access_type: 'offline',
scope: SCOPES
});
console.log('Authorize this app by visiting this url: ', authUrl);
var r1 = readline.createInterface({
input: process.stdin,
output: process.stdout
});
r1.question('Enter the code from that page here: ', function (code) {
r1.close();
oauth2Client.getToken(code, function (err, token) {
if (err) {
console.log('Error while trying to retrieve access token', err);
return;
}
oauth2Client.credentials = token;
storeToken(token, __tokenFilePath);
callback(oauth2Client);
emailEmitter.emit('token', token);
});
});
}
function storeToken(token, tokenFilePath) {
fs.writeFileSync(tokenFilePath, JSON.stringify(token));
console.log('Token stored to ', tokenFilePath);
}
module.exports = gmailClass;