How to send automated emails via Google Script?

Long time back, I was truly intrigued by the cool things that could be done using Google Apps Script. Automation was the main fascination with Google Script for me. Many repetitive tasks could be hacked using the automation of Google Script. Are you forgetting the deadlines due to lack of remainders? Are you bored of writing the same emails over and over again? Do you find it hard to stay awake till midnight to send your friend an email for his/her birthday? Don’t worry, Google Script is here in your rescue. You can now send automated emails via Google Script in no time.

Out of many Script Services, I will be talking about sending automated emails using Google Script called Mail Service. This service allows scripts to send email on a user’s behalf. Isn’t it cool? Well, I will let you decide it for yourself.

As I explain the process, I will be using the examples that were useful for me. Let’s get started.

First of all, go to https://www.google.com/script/start/ and click on Start Scripting.

A blank project will be created where one can write scripts based on JavaScript in order to send emails.

Send automated emails via Google Script to a single user:

Next, create a function and add the following code. Insert a valid email address, an appropriate Subject, and Email body and you are ready to go.

function sendEmails()
{
     MailApp.sendEmail
     (
          //TO:
               "abc@test.com",
         //Subject of the email:
              "Medicine Reminder",
        //Email Body:
              "It's time for you to take your medicines. Take care and get well soon!"
     );
}

After running the above script, it sends an email reminder to ‘ABC’ reminding him/her to take their medicines.

Setting a Trigger:
Time-driven triggers can be set depending on the requirement: Minutes timer, Hour timer, Day timer, Week timer, Month Timer.

Secondly, let’s go ahead and set up a trigger. To set up the triggers, click on the icon marked in the picture below.

  • Click on add new trigger.
  • Select the function name ‘sendEmails’
  • Select “Time-driven”, “Hour timer” and “Every 12 hours” in the Events section. And Save the trigger.
send automated emails via Google Script
Setting up a Time-driven trigger for Medicine Reminder Script

Here, the triggers are set such that ‘ABC’ gets a medicine reminder email every 12 hours.

Authorizing the Script:

Google Apps Script requires user Authorization to access private data and send emails on your behalf. So, we will need to allow the permission and save the changes in order to run the script.

Finally, in order to run the script, click on the icon next to the trigger button. An ‘authorization required’ window will appear. Click Review Permissions and follow the steps below.

Next up, sign in the email account that you want to authorize to send emails on your behalf. Later, the script asks for your permission to access your account. Click Allow.

send automated emails via Google Script
Authorizing the Medicine Reminder script to send emails on your behalf

After authorization, ABC will now get a medicine reminder email every 12 hours even if you are online or offline.

Send automated emails via Google Script to multiple users:

The same email can be sent to multiple users by placing all their email address separated by a comma delimiter.
Once upon a time when I was an intern, we had to fill our Progress/Project Status Report (PSR) every Thursday in order to lease our salaries else we would be in great big trouble. This was the script I used to send a reminder to myself and my friends every Thursday from 4 pm to 5 pm.

function PSRReminder() {
    MailApp.sendEmail
    (
        //TO:
            "abc@test.com, def@test.com, xyz@test.com",
        //Subject of the email:
            "PSR",
        //Email Body:
            "Dear Interns, This is a reminder for you to fill your PSR"
    );
}

There are various methods under the Class MailApp. Get further details at https://developers.google.com/apps-script/reference/mail/mail-app.

Sometimes, I write email scripts to wish my best friend Good Morning, Goodnight and tell him how much he means to me. I did teach him to use the script and now he automatically sends me an email every day encouraging me to work hard and do better.

There are numerous and creative ways possible to complete little or big tasks in our life using Google Apps Script. They can also be embedded into other applications which shall be further covered in upcoming posts. 🙂

Asmita
I am a WordPress Plugin Developer and a Freelance Google Apps Script Developer. I enjoy solving problems and dedicate my time to perfecting my knowledge about WordPress Plugins and Apps Script. My ideal day would consist of reading books or traveling with my close friends.

5 Comments

  1. Wow what a nice article.. learned a lot

  2. Nice article. Help me to learned a lot

  3. keep spirit to sharing

  4. Very nice article. Keep sharing <3

  5. Hi, I stumbled on your blog and I like this post a whole lot. You make some compelling points. Where can I find out more?

Leave a Reply