Delayed Email Sender

This program can send emails at a specified later time, based on what you’ve entered into a Google spreadsheet. Use it to automatically schedule some emails to send early tomorrow morning, for example.

This project will teach you several beginning programming concepts step-by-step, as well as how to get started with Google Apps Script. It’s broken up into 2 parts, each with different sub-sections. Follow each one in order to learn something new, and watch your program grow in complexity until it’s done!

Let’s Get Setup:

Google Apps Script is a programming language in which the programs (also known as “scripts”) can act on data in your Google Account. This can include things like your Gmail, Google Calendar, or even a Google Spreadsheet. The program we’re writing will operate on data in a spreadsheet, and we’ll even run the program from within the spreadsheet too!

To get started, you’ll first need to make your own copy of this Google Spreadsheet. This spreadsheet is where you’ll type the emails you want to send later.

In the new spreadsheet, open the “Script Editor” from the Tools menu.

Screen Shot 2016-02-15 at 2.03.29 PM

The Script Editor will pop up in a new page, and it’s where we’ll write our program. On the left you’ll see a list of files. Click the one called “”. A blank script will open to the right. This is where you’ll enter your code.

Screen Shot 2016-02-15 at 2.07.20 PM

Exercise 1A:

In this exercise we’ll learn:

  • How to use the script editor to edit and run code.
  • Code vs comments
  • What is a function and what are its arguments.
  • The power of Apps Script!

This exercise is a bit dense since there are a lot of concepts to introduce. But stick with it, as everything we learn here will be extremely useful in the later exercises!

The image below shows our code for exercise1A. It’s just 8 lines long, with the line numbers in the grey column to the left. You’ll need to replicate all of this code in your blank file by typing it word for word, character for character. But first, read the explanations below.

Screen Shot 2016-02-16 at 8.35.29 AM


What’s Happening in this Code?
This code will send an email to you, or anyone else you like!

The brown lines are called comments. These lines are only read by people, and are completely ignored by the computer. Anything following two forward-slashes (//) always becomes a comment. Commenting your code is essential to good programming to ensure that others can understand what your code does (including your future self).

On line #1 we setup (or “declare”) a part of the program called a function. A function is just a packaged-up piece of code that does something for you. Sometimes you’ll write your own functions, and sometimes you’ll use functions that other people wrote. For example, a math function might take 2 numbers and give you back their average. Or a grammar function might take a word and let you know if it’s spelled correctly or not. Another function might turn on the light in your classroom.

The math function I talked about would be setup (or “declared”) like this:

// Let’s declare a function to calculate the average of 2 numbers a and b
function calculateAverage(a, b)

This says that we’re defining (“declaring”) a function named calculateAverage expects to be “passed” two numbers, which will be called a and b. The items we pass to a function are called input parameters or just parameters. We always put the list of parameters in parenthesis after the function name. Later on we might use (or “call”) this function like this:

calculateAverage(4, 8);  // should give us back 6

Our function is just called “exercise1A”, and is declared on line 1.  Since our function doesn’t expect to be passed any input parameters, it just has an empty set of parenthesis after its name.

You’ll also notice a curly brace  ‘{‘  before the first line of code, and another  ‘}’  after the last line of code. These curly braces indicate where the code for this function starts and ends. Think of them like two bookends on a shelf, where the books in between are the code.

Other than the comments and braces, our function really only has one line of actual code. This is line 7. Here we are using a function that someone else wrote called “MailApp.sendEmail”, which sends an email. Note that we are not declaring our own function here. Rather, we are using (or “calling”) a function that someone else already declared and wrote somewhere else. This is a subtle but important distinction. If we were declaring our own function, the keyword function would appear before the name, as it does in line 1. You will be using (or “calling”) other people’s functions more often than you will be writing your own.

This function expects 3 pieces of information to be passed: Recipient Email Address, Subject, Email Message. So when we call the function, we must pass these inputs in the parenthesis following the function name.

To send ….

Ready to move on? Click here for Exercise 1B.

Comments are closed.