-
Notifications
You must be signed in to change notification settings - Fork 1
/
send.js
55 lines (50 loc) · 1.8 KB
/
send.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
50
51
52
53
54
55
var getFreshToken = require('./getFreshToken');
var request = require('request');
var getInfo = require('./getInfo');
module.exports = sendGMail;
var info = {};
function sendGMail(refreshToken, message, done) {
getFreshToken(refreshToken, function (err, accessToken) {
if (err) return done(err);
getUserInfo(refreshToken, accessToken, function (err, userInfo) {
if (err) return done(err);
var name = message.fromName || userInfo.name;
var email = message.fromEmail || userInfo.email;
var smtpMessage = [
"From: " + (message.from || (JSON.stringify(name) + ' <' + email + '>')),
"To: " + message.to.join(','),
"Content-type: " + (message.contentType || 'text/plain;charset=utf8'),
"MIME-Version: 1.0"
].join("\r\n") + "\r\n";
if (message.headers) for (var key in message.headers) if (message.headers.hasOwnProperty(key)) {
smtpMessage += key + ': ' + message.headers[key] + "\r\n";
}
smtpMessage += "\r\n" + (message.body || "");
request.post({
url: "https://www.googleapis.com/upload/gmail/v1/users/me/messages/send",
headers: {
Authorization: 'Bearer ' + accessToken,
'Content-Type': 'message/rfc822'
},
body: smtpMessage
}, function (err, res, body) {
if (err) return done(err);
try {
var ret = JSON.parse(body);
if (ret.error) return done(err);
return done(null, ret);
} catch (e) {
return done(e);
}
});
});
});
}
function getUserInfo(refreshToken, accessToken, done) {
if (info[refreshToken]) return done(null, info[refreshToken]);
getInfo(accessToken, function (err, _info) {
if (err) return done(err);
info[refreshToken] = _info;
return done(null, _info);
});
}