如果您在設定擴充功能時指定了「範本集合」參數,即可建立及管理電子郵件的 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 }}