-4

I've written a script that reads the date from a column in a spreadsheet in Google Sheets. It's separately attached to a trigger that runs daily. When the trigger runs, the date column is analyzed and compares the date to the current date to detect when it's 2 days out from date listed in the aforementioned column. For any matches, it pulls in other data from the same row(s) and generates an email for each row.

The trigger runs from a paid Google account since this script can send a lot of emails on any given day. The automation works perfectly fine.

However, I also have a use case where this same email needs to be manually triggered by users who work with this spreadsheet.

I have the script written to read the rows selected by the user, followed by allowing them to trigger the function under the macros. However, this causes the email generated by the script to be triggered from the active user, rather than through my paid account/email. Some users are using personal addresses, which causes a spoofing issue preventing email delivery in a series of instances.

How can I force the sender address to be from the paid account that's running the daily trigger, even in these scenarios where a user is manually triggering the macro/function?

Rubén
  • 34,714
  • 9
  • 70
  • 166
habib_101
  • 19
  • 6

2 Answers2

2

MailApp hasn't a parameter to specify the sender, it always will use the email address of the effective user.

The active user is the user that that have opened the web app being used to run the function. The effective user might be different i.e. then a function has being executed by an installable trigger.

GmailApp allows to sent as the sender an email alias of the effective user. Another option is to use domain wide delegation of authority to impersonate a user from your domain.

Related

Resources

Rubén
  • 34,714
  • 9
  • 70
  • 166
  • 1
    A bit behind, but wanted to close the loop on this request. This answer was very helpful. I added an alias and modified my script to use GmailApp to send on behalf of the alias and it worked perfectly. Thank you very much! – habib_101 Jun 08 '21 at 19:25
1

If by "manual," you mean actually going into the editor and hitting run, there's not much that can be done for security reasons. However, if you deploy the app as a web app, it gives you an option in your deployment settings for users to execute as you (the paid account) or them.

Fig. 1 Fig. 2

Colebot
  • 71
  • 4