I want to write a chrome extension that can send emails with attachments.
I have looked around for days and I can't get gmail api working in my chrome extension.
Fundamentally is it possible to send email from a chrome extension? I came across a lot of posts saying I can't do that because I cannot send email from client-side javascript alone. So I need to switch to something like node.js?
However, there are extensions from the Chrome extension webstore that can work with my gmail inbox. (for example this one: https://chrome.google.com/webstore/detail/checker-plus-for-gmail/oeopbcgkkoapgobdbedcemjljbihmemj)
Finally get the email sending going on. Thank you so much!
background.js
window.onload = chrome.identity.getAuthToken({ 'interactive': true }, function(token,grantedScopes) {
console.log(token);
console.log(grantedScopes);
chrome.storage.local.set({oauth2token: token}, function() {});
var currentSessionAccessToken = token;
var x = new XMLHttpRequest();
x.open('GET','https://www.googleapis.com/oauth2/v2/userinfo?alt=json&access_token=' + token);
x.onload = function() {
console.log(x.response);
};
x.send();
var y = new XMLHttpRequest();
y.open('GET','https://gmail.googleapis.com/gmail/v1/users/me/labels?alt=json&access_token=' + token);
y.onload = function() {
console.log(y.response);
};
y.send();
var z = new XMLHttpRequest();
z.open(
'POST',
'https://gmail.googleapis.com/gmail/v1/users/me/messages/send?alt=json&access_token=' + token,
{"raw": "the email raw"
});
z.setRequestHeader('Authorization', 'Bearer/' + token);
z.setRequestHeader('Accept', 'application/json');
z.setRequestHeader('Content-Type', 'application/json');
z.onload = function() {
console.log(z.response);
};
z.send(
JSON.stringify({"raw": "the email raw"}
));
})