How to send HTML email in Google Apps Script

You can send HTML email from Google Apps Script. For example, suppose you want to send emails from your Google Sheet when certain events occur. You can send email using MailApp.sendEmail() function like this:

where the message is a string with HTML content.

An Example

Suppose you have a Google sheet with a list of candidates.

candidates sheet

Make a copy of this Google Sheet. You can see the whole code in your copy.

You want to streamline the approval process. The example below will show you how to approve a candidate when a menu button is pressed. Then the script will automatically send an email to the candidate welcoming them.

Create a new menu item in the Google sheet

Create a new blank Google Sheet. First, let us add a menu item to the Google Sheet.

The process is that you will select on the candidate and then, select the menu item Process → Approve.

After entering this code to the script editor (In google sheet → Script editor) do File → Save and then close and re-open the Google sheet so that the new menu item (Process) appears.

Select the menu item Process → Approve. Google Sheet will ask the permissions and approvals to run the script. Complete the process.

Get selected candidate

Now we need to get the details of the currently selected candidate. Here is the code for that:

Create an HTML Email Page

Create a new file in the script editor. Do File → New. Then select HTML file. Do File → Save and then give it a name candidate-email.html

Here are the contents of candidate-email.html

Note that this is an HTML template. It uses variables like candidate.first_name and candidate.name.

Send Email

The code:

First, we load the HTML template for the email. Then assign the candidate object to a variable in the template. When you call evaluate(), the variables in the template are used to compose the HTML email. (See candidate.first_name in the HTML template)

Then send the email using MailApp.sendEmail() function.

We then update the spreadsheet’s status column to “approved”.

If you run Process → approve now, it will send email to the candidate and update the status to “approved”.

HTML email from Google Sheet Apps Script

See also

Leave a Reply 0 comments

Leave a Reply: