0

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"}
                          ));
                    })


difoxy2
  • 59
  • 9
  • I have no experience in making chrome extensions, but I did some research and found that they are programmed in JavaScript. You may want to look into the Gmail JavaScript API. https://developers.google.com/gmail/api/quickstart/js – person the human Apr 07 '22 at 23:18
  • 1
    Have you reviewed the method by calling a Google API request using the [`extension Background Pages`](https://riptutorial.com/google-chrome-extension/example/7945/background-page) by any chance? As described from a comment [here](https://stackoverflow.com/a/41179246), "_The best idea is to get user token in background.js page and to forward it to the content script and then create XMLHttpRequest requests for the gmail API_" (to send a Gmail API HTTP request for sending email message). You may refer to this sample implementation from [this similar post](https://stackoverflow.com/q/46766725). – SputnikDrunk2 Apr 08 '22 at 01:38
  • After content.js get the token, where do I put the token in the XMLHttpRequest requests? – difoxy2 Apr 08 '22 at 14:17
  • 1
    Something like: xmlHttpRequest.setRequestHeader('Authorization', 'Bearer ' + token); ?? – difoxy2 Apr 08 '22 at 14:18
  • @difoxy2 Yes, something like that. – SputnikDrunk2 Apr 08 '22 at 23:09
  • @Irvin Jay G. Thanks man, I finally got the email sending going on. Thanks so much :DDD – difoxy2 Apr 09 '22 at 03:21
  • You can send someone to a page quickly when a button is clicked and then close that page with `window.close();`. – ethry Apr 09 '22 at 03:32

0 Answers0