【发布时间】:2017-05-26 11:26:04
【问题描述】:
问候 Gmail API 粉丝。
我编写了一些非常棒的代码,它们使用带有 JavaScript 的 Gmail API 巧妙地验证和发送电子邮件。
但是,即使我的大脑和我一样大,我也有问题。虽然使用 gapi.client.gmail.users.messages.send 并将 userId 设置为 "me" 可以完美发送电子邮件,但我无法将其从与我的帐户关联的备用群组电子邮件地址发送。
例如,如果我尝试将 userId 更改为 "myalternate@somewhere.com",执行会引发 401“需要登录”错误,即使该电子邮件地址与我的 Gmail 帐户相关联。
有什么想法吗?
下面是一小段代码:
// Stuff gleaned from a form
var headers = {
'subject': e.data.subject,
'to': e.data.to,
'cc': e.data.cc,
'bcc': e.data.bcc,
'content-type': 'text/html; charset=utf-8'
};
// grab the email content
message = editor.getContent({format : 'raw'});
// construct the email
var email = '';
for(var header in headers)
email += header += ': ' + headers[header] + '\r\n';
email += '\r\n' + message;
// This is the bit - changing userId to anything other than 'me' (or the primary email address) kicks error
var sendRequest = gapi.client.gmail.users.messages.send({
'userId': 'me',
'resource': {
'raw': window.btoa(unescape(encodeURIComponent(email))).replace(/\+/g, '-').replace(/\//g, '_')
}
});
return sendRequest.execute(function(){});
}
【问题讨论】:
-
如果你对它进行 uri 编码,它会起作用吗?
encodeURIComponent('myalternate@somewhere.com') === "myalternate%40somewhere.com" -
不错的想法,但不幸的是它没有任何区别。我可以将 userId 从 'me' 更改为 'joe.bloggs@somewhere.com' 并且它仍然有效 - 它可以从主电子邮件地址发送。
标签: javascript gmail gmail-api