Search code examples
google-apps-scriptgoogle-groups

Sign in as a Google Group to create an Installable Trigger for App Scripts


I have created a number of tools that use Installable Triggers to send emails. Because I'm the one who wrote the scripts, and created the triggers, those emails are all sent from my account.

I am wondering if there is a way to create those installable triggers, not as my own account, but as a Google Group account. Ideally, I would like for those triggered emails to be sent from the Group's account, as opposed to my own. I am an Owner of the Google Group that I would like to use, so if it's possible, I should have the appropriate security. I'm just not sure if there's a way to log in as the Group, so that I can create the triggers from the Group's account, and therefore have the emails sent from the Group's account. Is that possible?


Solution

  • You don't need to sign in your group in the script, you just need to add the group email to your gmail settings Send mail as:

    sample

    After adding it, a window will prompt you and add your group credentials there. After confirmation, you should be able to use that email as your parameter for sendEmail. See code below.

    Code:

    function sendEmailUsingGroupEmail() {
      var alias = GmailApp.getAliases();
      var groupEmail = "[email protected]";
      var toEmail = "[email protected]";
      var strSubject = "subject";
      var strContent = "this is content";
      
      // Check first if that email exists after adding
      if(alias.includes(groupEmail))
        GmailApp.sendEmail(toEmail,strSubject,strContent,{from: groupEmail});
    }