0
votes

Script below for reference.

I've created a script that forwards all tagged emails to a file account. I'd like to deploy this to all GSuite users on my domain - however I don't understand the process for doing so.

Deploy as 'Web App', allows me to restrict use to just my domain, but seems to require a 'doGet' function (even though my script doesn't use one - hence the blank line at the top), but then I can't work out how to deploy that across the GSuite users

Documentation seems to suggest I can deploy in 'Chrome Web Store' and then add that to all users - but I want this to be private to my domain, not publicly available.

I'm sure this is possible, but just can't work out from the docs how to approach it.

Thanks

function doGet(e){return ContentService.createTextOutput("")}

var triggers = ScriptApp.getProjectTriggers();
 for (var i = 0; i < triggers.length; i++) {
   ScriptApp.deleteTrigger(triggers[i]);
 }

ScriptApp.newTrigger("autoForward")
  .timeBased()
  .everyMinutes(5)
  .create();

function autoForward() {

  var recipient = '[email protected]';

  var labels = GmailApp.getUserLabels();

  for (var i = 0; i < labels.length; i++) {

    if(labels[i].getName().indexOf("Clients/")>-1){ 

      var threads = GmailApp.search('label:' + labels[i].getName() + ' NOT label:filed');

      var label = GmailApp.getUserLabelByName("filed");

      for (var j = 0; j < threads.length; j++) {
        var messages = threads[j].getMessages();
        messages[messages.length - 1].forward(recipient,{name: labels[i].getName()});
        threads[j].addLabel(label);
      }
    }
  }  
}
1

1 Answers

2
votes

You should put the trigger creation part inside the doGet function. Then publish this script as a web app such that only users in your domain can access this web app.

Now share the URL of the web app with users in your domain, they can open the URL in the web browser, authorize with their Google account and the script would forward their emails to the intended recipient.

function doGet() {

 var triggers = ScriptApp.getProjectTriggers();
 for (var i = 0; i < triggers.length; i++) {
   ScriptApp.deleteTrigger(triggers[i]);
 }

 ScriptApp.newTrigger("autoForward").timeBased().everyMinutes(5).create();

 return ContentService.createTextOutput("Script setup complete!");

}