Google Apps Script quickstart

This document shows you how to use Apps Script to send yourself an email with schemas to test email markup.

Create the project

Visit script.google.com. If this is your first time visiting script.google.com, you're redirected to an information page. Click Start Scripting to proceed to the script editor. In the script editor, create a script for a Blank Project.

Replace the code in Code.gs with the following:

gmail/markup/Code.gs
/**
 * Send an email with schemas in order to test email markup.
 */
function testSchemas() {
  try {
    const htmlBody =
      HtmlService.createHtmlOutputFromFile("mail_template").getContent();

    MailApp.sendEmail({
      to: Session.getActiveUser().getEmail(),
      subject: `Test Email markup - ${new Date()}`,
      htmlBody: htmlBody,
    });
  } catch (err) {
    console.log(err.message);
  }
}

Select File > New > HTML file to create an HTML file. Name the file mail_template to match the parameter in the preceding script. Replace the content of the HTML file with the following:

gmail/markup/mail_template.html
<!--
 Copyright 2022 Google LLC

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-->

<html>
  <head>
    <script type="application/ld+json">
    {
      "@context": "https://schema.org",
      "@type": "EmailMessage",
      "description": "Check this out",
      "potentialAction": {
        "@type": "ViewAction",
        "target": "https://www.youtube.com/watch?v=eH8KwfdkSqU"
      }
    }
    </script>
  </head>
  <body>
    <p>
      This a test for a Go-To action in Gmail.
    </p>
  </body>
</html>

Test the script

To test the script:

  1. Save the project.
  2. Select the tab for Code.gs.
  3. Make sure the function testSchemas is selected in the Select function menu.
  4. Click Run in the Apps Script development environment.

The first time you run the script, you're asked to grant authorization. After authorizing, run the script again. After the script runs, check your inbox for an email sent from yourself with a Go-to action button, as shown in the following screenshot:

Apps Script tutorial

How does the script work?

The testSchemas function reads the HTML content from the mail_template.html file and sends that content as an email to the authenticated user. As explained in Register with Google, Gmail displays all schemas that you send to yourself. You can use the email sent by the script to bypass registration requirements for testing.