0
votes

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

1
It's not always possible to get the users email. Sometimes security rules do not allow it. In that case you must have the user supply it. If the don't want to then don't send the email.Cooper
This won't be a problem here, all users are in the same domain. I can easily get their emails while they are on the web app, my problem is to understand who is executing a function when the scheduled trigger is launched. I can even get their email during the trigger launched function if I share the script with them, but that is not feasible / desirableuser9041424

1 Answers

0
votes

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