Author: Matt Brigidi | Published: March 16, 2025
You can automate emails using Google’s native programming language Google Apps Script.
Apps Script uses Google’s version of the JavaScript programming language. It allows users to augment the native functionality of Google Drive’s productivity applications, such as Documents, Sheets, Slides, and Gmail.
This tutorial will focus on the fundamentals of sending an automated HTML email using Google Apps Script.
There are two types of Apps Script projects:
The only significant difference between the two types is that bound projects can access the app it was created from using methods that reference the “active” file; while unbound projects need to define the file it wants to connect to using the File ID or the File URL.
Otherwise, all aspects of the two types are the same.
New Google Apps Script projects will always open with a script file called “Code.gs”. It will have boilerplate code written for a function called “myFunction”. You can use that function name if you would like to. I generally like to start from scratch because I like to maximize the amount of time I spend on my keyboard and minimize the amount of clicks I need with a mouse.
Apps Script requires a function to execute a script file, so you’ll need to create a function if you want to get output in your console and/or action from your code.
You can name your function whatever you like, however, you’ll want to give it a distinct name. If your project has more than one function and they share a name, then Apps Script won’t know which function you want to run.
For this tutorial, I decided to call my function “autoEmail” and stored it in the “Code.gs” file.
function autoEmail() {
}
Navigate to the “Files” portion of the navigation bar and select the “+” icon.
You will be prompted with two options:
We’ll want to create an HTML file, so that we can design the structure of our email, so that it has basic formatting, line breaks, and linked text.
Name your HTML file whatever you’d like; I’m following along with my YouTube tutorial, so I’ll be calling my HTML file “email”.
We need to establish a connection between the script file and the HTML file. We’ll do this by declaring a variable called htmlTemplate, which will reference a method from the HtmlService class.
The HtmlService class has a method called createTemplateFromFile that allows us to reference an HTML file. The createTemplateFromFile method requires the input value to be structured as a sting, so we will pass the name of the HTML file — in my case, the file name is “email” — surrounded by quotation marks.
Your code will now look like this:
function autoEmail() {
// connect to the html template
var html = HtmlService.createTemplateFromFile('email');
}
Essentially, we need to create a connection from our HTML file back to our Script file, so that we can use the Script file to send the HTML as the body of the email.
We can accomplish this by using the htmlTemplate variable we created earlier.
We’ll create a new variable called “htmlForEmail” that is set equal to the htmlTemplate variable. We’ll use two additional methods — evaluate and getContent — in order to get the contents of the HTML file.
function autoEmail() {
// connect to the html template
var html = HtmlService.createTemplateFromFile('email');
// connect the template so you can send it via gmail app
var emailContent = html.evaluate().getContent();
}
The GmailApp is a class just like the SpreadsheetApp, which is what we used to extract data from the Google Sheet.
GmailApp has a method called “sendEmail”, which is the method I recommend when attempting to automate emails. Now, I use Gmail and typically send emails to other people who use Gmail (I work for a company that uses Google’s productivity suite).
If you have issues with using the GmailApp, there is also a mailApp class that has much of the same functionality.
The sendEmail method has four inputs:
Since we are sending an HTML email using an HTML file, we’re going to need to use the options portion of the method. Your code will end up looking like this:
// send email
GmailApp.sendEmail(
["EMAIL@your_email.com", "EMAIL_2@your_email.com"],
"Reminder to pick up groceries",
"Please use gmail",
{htmlBody: emailContent}
)
The first input is the variable that references our email variable. You can also hardcode the email(s) if you would like.
The second input is the subject line. In my example, I am using the name variable with some boilerplate text. This way, the recipient has a personalized subject.
The third input is usually the body of the message. However, since we are using an HTML file, we will use this section to add a message if the HTML does not display for some reason.
The fourth input is where we can set additional options. In this case, we will use the htmlBody options to reference the “htmlForBody” variable, which is using the getContent method from the HtmlService to get our HTML file’s content.The code for all that looks like this:
function autoEmail() {
// connect to the html template
var html = HtmlService.createTemplateFromFile('email');
// connect the template so you can send it via gmail app
var emailContent = html.evaluate().getContent();
// send email
GmailApp.sendEmail(
["EMAIL@your_email.com", "EMAIL_2@your_email.com"],
"Reminder to pick up groceries",
"Please use gmail",
{htmlBody: emailContent}
)
}
When you created your HTML file, Apps Script auto-populated boilerplate HTML code.
We’ll be focusing on writing a few paragraphs, so we’ll be working inside the body tags. You don’t need to know much HTML, but I think understanding the basics is a good idea. I will link my code after the next section, so that you can copy it into your project.
HTML is pretty straightforward, so if you’re exploring Apps Script, then I’d recommend looking into it via YouTube tutorials or intro resources like those provided by W3 Schools.
That’s it! You should be able to send an email using the most recently added data from your Google Sheets workbook tab!
Here is the full code:
function autoEmail() {
// connect to the html template
var html = HtmlService.createTemplateFromFile('email');
// connect the template so you can send it via gmail app
var emailContent = html.evaluate().getContent();
// send email
GmailApp.sendEmail(
["mattbrigidi.dev@gmail.com", "mattbrigidi@gmail.com"],
"Reminder to pick up groceries",
"Please use gmail",
{htmlBody: emailContent}
)
}
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<p>Hello,</p>
<p>This is a reminder to pick up the groceries this week.</p>
</body>
</html>
I hope you found this tutorial helpful. Please check out the Technology Tutorial section of this site for more lessons or check out my YouTube channel for all my videos.