forked from fedecia/gmail-sender-oauth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
39 lines (31 loc) · 1.11 KB
/
index.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
var google = require('googleapis');
exports.send = function (token, params, callback) {
var gmail = google.gmail('v1');
var headers = [];
headers.push('From: <' + params.from + '>');
headers.push('To: ' + params.to);
headers.push('Content-type: text/html;charset=iso-8859-1');
headers.push('MIME-Version: 1.0');
headers.push('Subject: ' + params.subject);
headers.push('');
headers.push(params.body);
var email = headers.join('\r\n').trim();
var base64EncodedEmail = new Buffer(email).toString('base64');
base64EncodedEmail = base64EncodedEmail.replace(/\+/g, '-').replace(/\//g, '_');
var reqParams = {
auth: oauth,
userId: 'me',
resource: {
raw: base64EncodedEmail
}
};
gmail.users.messages.send(reqParams, null, function (err, resp) {
if (!err) {
return callback(null, resp);
}
else {
return callback('Unable to send email: ' + err, null);
}
});
});
};