Search code examples
google-apps-scriptweb-applicationstriggersgoogle-workspace

Can a web app user schedule an apps script trigger to send an email at a given time?


I am trying to find out if the following scenario is possible with apps script:

  1. I deploy a web app that is executing according to the user accessing it.
  2. user opens the deployed web app and authorizes email sending
  3. user clicks on a button that triggers a google.script.run call to a function in Code.gs that createse a new, time based trigger to run at a predefined time,which will execute an email sending function also stored in Code.gs.
  4. At the predefined time, the email sending function sends an email in the name of the user that created this time based trigger.

I can create the installable trigger based on the user action, but I struggle to see how I can retrieve the web user's email address who created the timed trigger (and who is executing it) at the moment when the email sending function is triggered.

Session.getActiveUser().getEmail() does not work in this case, unless the user has access to the apps script project hosting the email sending function?

Is there a workaround?

Edit:users would be all in the same domain


Solution

  • You are right that Session.getActiveUser().getEmail() cannot always retrieve the user identity correctly from a WebApp

    Workaround for accessing the user identity:

    1. Use Session.getEffectiveUser().getEmail()

    2. Use the People API (after enabling):

    People.People.get("people/me", {personFields: "emailAddresses"}).emailAddresses[0].value