I’ve been playing with the Google Gmail API, in the hope to find a way to do the following.
The Android app I’m creating is intended for a closed group of users. All of them should be able to send e-mail from inside the app. The e-mail content is auto-generated, and will be send from one common Gmail account, linked to the app.
The users however,don’t have direct access to this account. So, the email should be sent via the “admin” account with the click of a button, from inside the app.
So far I have been able to set things up, and when the app is running in the browser (ionic serve -c), the e-mails are sent when giving permission via the Google authorization, but ONLY with the admin e-mail.
My question: is it possible to make this work such that the user does not have to authenticate, and the app can send the e-mails directly?
To illustrate what I am after, here is the main snippet. Basically I am looking for a way to send e-mail from the app, with an account that is used by the app.
I do not want the user app to authenticate and allow his/her e-mail address to be used to send the e-mail on their behalf.
I’ve commented the part out, that I hope to get rid off.
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);
})
};
As it is, the code results in a 401 “Login required”