Greetings Gmail API fans.
I have written some absolutely awesome code, which neatly authenticates and sends email using the Gmail API with JavaScript.
However, even with a brain as big as mine, I have an issue. While emails get sent perfectly using gapi.client.gmail.users.messages.send
with a userId
set to "me"
, I cannot get it to send, instead, from an alternate group email address I have associated with my account.
For example, if I try to change the userId
to "[email protected]"
, executing kicks a 401 "Login Required" error, even when that email address is associated with my Gmail account.
Any ideas what's going on?
A smidgeon of code below:
// 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(){});
}
Don't change the "userId" in the URL path. Just set the "From" header in your email to that address (and it has to be a verified address for your Gmail account for that to work).