將 Handlebars 範本與 Trigger Email 擴充功能一起使用

如果您在配置擴充功能期間指定了「範本集合」參數,則可以為您的電子郵件建立和管理Handlebars 範本

模板集合結構

為每個文件提供一個易於記憶的 ID,在寫入範本集合的文件中將其用作範本名稱

範本文件可以包含以下任意欄位:

  • 主題:電子郵件主題的範本字串。
  • text:電子郵件明文內容的範本字串。
  • html:電子郵件 HTML 內容的範本字串。
  • amp:電子郵件的AMP4EMAIL內容的範本字串。
  • Attachments:以範本字串作為值的附件陣列;支援的Nodemailer 選項:utf-8 字串、自訂內容類型、URL、編碼字串、資料 URI 和預先產生的 MIME 節點(請注意,您的電子郵件無法存取雲端伺服器的檔案系統)。

範例範本可能具有following ID 和類似內容:

{
  subject: "@{{username}} is now following you!",
  html: "Just writing to let you know that <code>@{{username}}</code> ({{name}}) is now following you.",
  attachments: [
    {
     filename: "{{username}}.jpg",
     path: "{{imagePath}}"
    }
  ]
}

使用範本發送電子郵件

若要使用範本傳送電子郵件,在將文件新增至郵件集合時,請包含具有namedata屬性的template本欄位。例如,使用上面的following模板:

admin
  .firestore()
  .collection("MAIL_COLLECTION")
  .add({
    toUids: ["abc123"],
    template: {
      name: "following",
      data: {
        username: "ada",
        name: "Ada Lovelace",
        imagePath: "https://example.com/path/to/file/image-name.jpg"
      },
    },
  });

模板部分

您可以透過在範本文件中指定{partial: true}來使用可重複使用的部分來編寫範本。每個標準資料欄位( subjecthtmltextamp )將被定義為僅在自己的環境中使用的部分。例如,稱為footer的部分可能具有以下資料:

{
  partial: true,
  html: "<p>This mail was sent by ExampleApp, Inc. <a href='https://example.com/unsubscribe'>Unsubscribe</a></p>",
  text: "This mail was sent by ExampleApp, Inc. Unsubscribe here: https://example.com/unsubscribe"
}

在另一個範本中,透過引用其名稱(文件 ID)來包含部分:

<p>This is my main template content, but it will use a common footer.</p>

{{> footer }}