搭配使用處理常式範本與觸發電子郵件擴充功能
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
如果您在設定擴充功能時指定了「範本集合」參數,即可建立及管理電子郵件的 Handlebars 範本。
範本集合結構
為每個文件提供好記的 ID,並在寫入範本集合的文件中,將該 ID 做為範本名稱。
範本文件可包含下列任一欄位:
- subject: 電子郵件主旨的範本字串。
- text:電子郵件明文內容的範本字串。
- html:電子郵件 HTML 內容的範本字串。
- amp: 電子郵件 AMP4EMAIL 內容的範本字串。
- attachments:附件陣列,值為範本字串;支援 Nodemailer 選項:UTF-8 字串、自訂內容類型、網址、編碼字串、資料 URI 和預先產生的 MIME 節點 (請注意,電子郵件無法存取雲端伺服器的檔案系統)。
範本 ID 可能為 following
,內容如下:
{
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}}"
}
]
}
使用範本傳送電子郵件
如要使用範本傳送電子郵件,請在將文件新增至郵件集合時,加入含有 name
和 data
屬性的 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}
,使用可重複使用的局部撰寫範本。每個標準資料欄位 (subject
、html
、text
和 amp
) 都會定義為僅在自身環境中使用的部分。舉例來說,名為 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 }}
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[],[],null,["\u003cbr /\u003e\n\nIf you specified a \"Templates collection\" parameter during configuration of the\nextension, you can create and manage [Handlebars templates](https://handlebarsjs.com/)\ntemplates for your emails.\n\nTemplate collection structure\n\nGive each document a memorable ID that you use as the *template name* in the\ndocuments you write to your templates collection.\n\nThe template document can include any of the following fields:\n\n- **subject:** A template string for the subject of the email.\n- **text:** A template string for the plaintext content of the email.\n- **html:** A template string for the HTML content of the email.\n- **amp:** A template string for the [AMP4EMAIL](https://amp.dev/documentation/guides-and-tutorials/learn/email-spec/amp-email-format/) content of the email.\n- **attachments:** An array of attachments with template strings as values; [Nodemailer options](https://nodemailer.com/message/attachments/) supported: utf-8 string, custom content type, URL, encoded string, data URI, and pre-generated MIME node (be aware that your email has no access to the cloud server's file system).\n\nAn example template might have an ID of `following` and content like: \n\n {\n subject: \"@{{username}} is now following you!\",\n html: \"Just writing to let you know that \u003ccode\u003e@{{username}}\u003c/code\u003e ({{name}}) is now following you.\",\n attachments: [\n {\n filename: \"{{username}}.jpg\",\n path: \"{{imagePath}}\"\n }\n ]\n }\n\nSend emails using templates\n\nTo deliver email using templates, when adding documents to your mail collection,\ninclude a `template` field with `name` and `data` properties. For example,\nusing our `following` template from above: \n\n admin\n .firestore()\n .collection(\"MAIL_COLLECTION\")\n .add({\n toUids: [\"abc123\"],\n template: {\n name: \"following\",\n data: {\n username: \"ada\",\n name: \"Ada Lovelace\",\n imagePath: \"https://example.com/path/to/file/image-name.jpg\"\n },\n },\n });\n\nTemplate Partials\n\nYou can compose templates using reusable [partials](https://handlebarsjs.com/guide/partials.html)\nby specifying `{partial: true}` in the template document. Each of the standard\ndata fields (`subject`, `html`, `text`, and `amp`) will be defined as a partial\nused only in its own environment. For example, a partial called `footer` might\nhave data like: \n\n {\n partial: true,\n html: \"\u003cp\u003eThis mail was sent by ExampleApp, Inc. \u003ca href='https://example.com/unsubscribe'\u003eUnsubscribe\u003c/a\u003e\u003c/p\u003e\",\n text: \"This mail was sent by ExampleApp, Inc. Unsubscribe here: https://example.com/unsubscribe\"\n }\n\nIn another template, include the partial by referencing its name (document ID): \n\n \u003cp\u003eThis is my main template content, but it will use a common footer.\u003c/p\u003e\n\n {{\u003e footer }}"]]