Apps Script 快速入門導覽課程

本文將說明如何使用 Apps Script 傳送含有架構的電子郵件給自己,以便測試電子郵件標記。

建立專案

前往 script.google.com。如果這是你第一次前往 script.google.com,系統會將你重新導向至資訊頁面。按一下「Start Scripting」以前往指令碼編輯器。在指令碼編輯器中,為「空白專案」建立指令碼。

Code.gs 中的程式碼替換為以下內容:

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);
  }
}

依序選取「檔案」>「新增」>「HTML 檔案」,建立新的 HTML 檔案。請根據上述 JavaScript 中的參數,將檔案命名為 mail_template。將 HTML 檔案的內容替換成以下內容:

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>

測試指令碼

如何測試指令碼:

  1. 儲存專案。
  2. 選取「Code.gs」分頁標籤。
  3. 確認已在 Select function 下拉式選單中選取 testSchemas 函式。
  4. 按一下 Apps Script 開發環境中的 Run

首次執行指令碼時,系統會要求您授予授權,之後再重新執行指令碼。指令碼執行後,請在收件匣中查看是否收到由「前往動作」按鈕寄出的電子郵件,如以下螢幕截圖所示:

Apps 指令碼教學課程

指令碼的運作方式為何?

testSchemas 函式會從名為 mail_template.html 的檔案讀取 HTML 內容,並以電子郵件形式將內容傳送給目前已驗證的使用者。如「向 Google 註冊」一文所述,Gmail 會顯示您傳送給自己的所有架構,因此在測試時,您可以使用指令碼傳送的電子郵件忽略註冊規定。