使用Gmail gapi从“普通”单一帐户发送电子邮件

时间:2018-10-12 12:00:59

标签: javascript android ionic-framework gapi

我一直在使用Google Gmail API,希望找到一种执行以下操作的方法。 我正在创建的Android应用仅供封闭的用户群使用。他们所有人都应该能够使用一个Gmail帐户从应用程序内部发送电子邮件。

但是,用户没有直接访问此帐户的权限。因此,电子邮件应通过应用程序帐户发送,而无需用户授权。

我的问题:是否可以使这项工作使用户不必进行身份验证,并且该应用程序可以直接发送电子邮件?

为了说明我所追求的,这是主要的代码片段。 我不希望用户应用进行身份验证并允许他/她的电子邮件地址用来代表他们发送电子邮件。

我已经注释掉了一部分,希望能摆脱它。

const apiKey =  'this_is_my_apiKey';
const clientId = 'this_is_my_clientId';

const scopes = 'https://www.googleapis.com/auth/gmail.compose ' + 'https://www.googleapis.com/auth/gmail.send';


/*gapi.auth2.authorize({
  client_id: clientId,
  scope: scopes,
  immediate: false
}); */

gapi.client.init({
  'apiKey': apiKey,
  'clientId': clientId,
  'scope': scopes,
  'immediate': true
}).then( () => {
  return gapi.client.load('gmail', 'v1');
}).catch(error => {
  console.log(error);
  alert(error.details);
});  


 sendEmail() {

this.createAndSendMessage({
  'To': 'customer_email_address@somewhere.cyberspace',
  'Subject': 'Test e-mail',
},
'My mseesage content comes here.',
callback);

return false;
}

createAndSendMessage(headers_obj, message, callback) {

var email = '';

for (var header in headers_obj)
    email += header += ": " + headers_obj[header] + "\r\n";

email += "\r\n" + message;

gapi.client.gmail.users.messages.send({
  'userId': 'the_app_account@mail.cyberspace',
  'resource': {
    'raw': window.btoa(email).replace(/\+/g, '-').replace(/\//g, '_')
  }
}).then(response => {
  console.log(response);
}).catch(error => {
  console.log(error);
  alert(error);
})
};

0 个答案:

没有答案
相关问题