通过 google http rest api 发送电子邮件
send email via google http rest api
我正在尝试从 Linux 服务器上的 NodeJS 运行 向 Google Gmail RESR HTTP API 发送电子邮件。不使用库,只是发送 https
。我已经弄清楚了 OAuth 部分,有一个访问令牌并从 google 获得响应。但是我无法通过各种错误消息。我已经发布了下面的代码。这不是很明显,但是 EmailSend()
在我从 google 获得访问令牌后被调用,所以是的它被调用了。
var emailStr = new Buffer(
"Content-Type: text/plain; charset=\"UTF-8\"\n" +
"MIME-Version: 1.0\n" +
"Content-Transfer-Encoding: 7bit\n" +
"to: SOMEONE@gmail.com\n" +
"from: SOMEONEELSE@MYDOMAIN.com\n" +
"subject: Subject Text\n\n" +
"The actual message text goes here"
).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
//var emailBase64UrlSafe = Rtrim( emailStr, '=' );
//var emailBase64UrlSafe = JsStrToUrlSafe ( emailStr );
var emailBase64UrlSafe = emailStr;
var http = require('https');
function EmailSend() {
var post_data = emailBase64UrlSafe;
var post_options = {
hostname: 'www.googleapis.com',
port: '443',
path: '/gmail/v1/users/me/messages/send',
method: 'POST',
headers: {
"Authorization": 'Bearer '+googleAccessKey['access_token'],
"Content-Type" : "application/json; charset=UTF-8"
},
};
console.log( post_options );
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
post_req.write(JSON.stringify({ "raw": emailBase64UrlSafe }));
post_req.end();
}; /* end EmailSend() */
Response: {
"error": {
"errors": [
{
"domain": "global",
"reason": "failedPrecondition",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
使用的资源:
亲身尝试过,成功了!
var http = require('https');
var mail = new Buffer(
"From: example@gmail.com\n" +
"To: example@gmail.com\n" +
"Subject: Subject Text\n\n" +
"Message text"
).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
var post_options = {
hostname: 'www.googleapis.com',
port: '443',
path: '/gmail/v1/users/me/messages/send',
method: 'POST',
headers: {
"Authorization": 'Bearer <ACCESS_TOKEN>',
"Content-Type" : "application/json"
}
};
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
post_req.write(JSON.stringify({ "raw": mail }));
post_req.end();
我正在尝试从 Linux 服务器上的 NodeJS 运行 向 Google Gmail RESR HTTP API 发送电子邮件。不使用库,只是发送 https
。我已经弄清楚了 OAuth 部分,有一个访问令牌并从 google 获得响应。但是我无法通过各种错误消息。我已经发布了下面的代码。这不是很明显,但是 EmailSend()
在我从 google 获得访问令牌后被调用,所以是的它被调用了。
var emailStr = new Buffer(
"Content-Type: text/plain; charset=\"UTF-8\"\n" +
"MIME-Version: 1.0\n" +
"Content-Transfer-Encoding: 7bit\n" +
"to: SOMEONE@gmail.com\n" +
"from: SOMEONEELSE@MYDOMAIN.com\n" +
"subject: Subject Text\n\n" +
"The actual message text goes here"
).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
//var emailBase64UrlSafe = Rtrim( emailStr, '=' );
//var emailBase64UrlSafe = JsStrToUrlSafe ( emailStr );
var emailBase64UrlSafe = emailStr;
var http = require('https');
function EmailSend() {
var post_data = emailBase64UrlSafe;
var post_options = {
hostname: 'www.googleapis.com',
port: '443',
path: '/gmail/v1/users/me/messages/send',
method: 'POST',
headers: {
"Authorization": 'Bearer '+googleAccessKey['access_token'],
"Content-Type" : "application/json; charset=UTF-8"
},
};
console.log( post_options );
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
post_req.write(JSON.stringify({ "raw": emailBase64UrlSafe }));
post_req.end();
}; /* end EmailSend() */
Response: {
"error": {
"errors": [
{
"domain": "global",
"reason": "failedPrecondition",
"message": "Bad Request"
}
],
"code": 400,
"message": "Bad Request"
}
使用的资源:
亲身尝试过,成功了!
var http = require('https');
var mail = new Buffer(
"From: example@gmail.com\n" +
"To: example@gmail.com\n" +
"Subject: Subject Text\n\n" +
"Message text"
).toString("base64").replace(/\+/g, '-').replace(/\//g, '_');
var post_options = {
hostname: 'www.googleapis.com',
port: '443',
path: '/gmail/v1/users/me/messages/send',
method: 'POST',
headers: {
"Authorization": 'Bearer <ACCESS_TOKEN>',
"Content-Type" : "application/json"
}
};
var post_req = http.request(post_options, function(res) {
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('Response: ' + chunk);
});
});
post_req.write(JSON.stringify({ "raw": mail }));
post_req.end();