将 Handlebars 模板与触发器电子邮件扩展程序搭配使用
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
如果您在配置扩展程序期间指定了“模板集合”参数,则可以为电子邮件创建和管理 Handlebars 模板。
模板集合结构
为每个文档指定一个容易记住的 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 }}
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-13。
[null,null,["最后更新时间 (UTC):2025-08-13。"],[],[],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 }}"]]