如果指定「範本集合」參數, 您可以建立和管理 Handlebar 範本 電子郵件範本
範本集合結構
為每份文件提供好記的 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 }}