Riyafa Abdul Hameed
Riyafa Abdul Hameed

Reputation: 7973

GAS: Send emails at random time

I would like to send random emails about 2-3 times a week to a recipient using google apps script. I know I can use event triggers to send emails at a given time, but is it possible to randomize the triggers? I thought of using sleep(), but the script cannot be put to sleep for longer than five minutes.

Upvotes: 0

Views: 594

Answers (1)

sudo bangbang
sudo bangbang

Reputation: 28209

Use ScriptApp.newTrigger to create custom time triggers.

function createRandomTimeTrigger() {
 ScriptApp.newTrigger("function_you_wanna_call")
   .timeBased()
   .after(Math.random() * 60 * 1000)
   .create();
}

Change the argument for after function to better suit your requirements. Now you can use a normal time trigger to invoke this function and it'll take care of the rest

Make sure you delete the trigger after execution. Otherwise script will get shutdown due to too many triggers

A question on SO on how to delete triggers

Upvotes: 1

Related Questions