使用触发器电子邮件扩展程序
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
通过触发器电子邮件扩展程序 (firestore-send-email
),您可以根据 Cloud Firestore 集合中的文档自动发送电子邮件。向集合中添加文档会触发此扩展程序,以发送通过文档字段构建的电子邮件。文档中的顶级字段会指定电子邮件的发件人和收件人,包括 to
、cc
和 bcc
选项(均支持 UID)。文档的 message
字段会指定其他电子邮件要素,例如标题和正文(纯文本或 HTML)。
下面是会触发此扩展程序的基本文档内容示例:
admin.firestore().collection('mail').add({
to: 'someone@example.com',
message: {
subject: 'Hello from Firebase!',
html: 'This is an <code>HTML</code> email body.',
},
})
您也可以选择将此扩展程序配置为使用 Handlebars 模板来呈现电子邮件。
安装前的设置工作
在安装扩展程序之前,请完成以下步骤:
设置外发邮件服务。
在安装触发器电子邮件扩展程序时,您需要指定 SMTP 服务器的连接和身份验证详细信息,因为扩展程序需要使用此服务器来发送电子邮件。此服务器通常是由电子邮件传送服务提供的,例如 Sendgrid、Mailgun 或 Mailchimp 事务性邮件,但也可以是由您自己运行的服务器。
创建电子邮件文档集合。
触发器电子邮件扩展程序会监听您指定的 Cloud Firestore 集合中的新文档。如果发现新文档,扩展程序会根据该文档的字段发送电子邮件。为此,您可以使用任何 Cloud Firestore 集合;本页面上的示例使用一个名为 email
的集合。
为电子邮件文档集合设置安全规则。
此扩展程序可用于直接从客户端应用触发电子邮件传送。不过您应该谨慎控制客户端对该集合的访问权限,避免可能出现的滥用问题(您必定不会希望用户能通过公司的电子邮件地址随意乱发电子邮件!)。
安全规则对于不同的应用有所不同,但您应该始终确保电子邮件仅发送给预期收件人,并尽可能减少自由格式的内容。在这种情况下,模板就可以帮上忙 - 您可以使用安全规则来验证填充到模板中的数据,确认其是否符合您针对应该允许用户触发的操作的预期。
可选:设置用户集合。
根据此扩展程序的基本用法,若要指定电子邮件的收件人,在邮件文档的 to
、cc
和 bcc
字段中指定收件人的电子邮件地址即可。或者,如果您在 Cloud Firestore 中有用户数据库,也可以通过用户的 UID 来指定收件人。要使用这种方法,您的用户集合必须满足以下条件:
- 该集合必须以用户 ID 作为键。也就是说,集合中每个用户文档的文档 ID 都必须是用户的 Firebase Authentication UID。
- 每个用户文档都必须有一个
email
字段,其中包含用户的电子邮件地址。
可选:设置模板集合。
您可以使用 Handlebars 模板呈现电子邮件。
为此,您需要一个 Cloud Firestore 集合来包含模板。
如需了解详情,请参阅将 Handlebars 模板与触发器电子邮件扩展程序结合使用。
安装扩展程序
如需安装该扩展程序,请按照安装 Firebase Extension 扩展程序页面上的步骤操作。总的来说,请执行以下任一操作:
Firebase 控制台:点击以下按钮:
安装触发器电子邮件扩展程序
CLI:运行以下命令:
firebase ext:install firebase/firestore-send-email --project=projectId-or-alias
安装扩展程序时,系统会提示您指定 SMTP 连接信息和您之前设置的 Cloud Firestore 集合。
使用扩展程序
安装后,此扩展程序会监控所有写入您配置的集合的文档。电子邮件会根据文档字段的内容进行传送。顶级字段指定电子邮件的发件人和收件人。message
字段包含要传送的电子邮件的详细信息,包括电子邮件正文。
示例:发送电子邮件
如需发送简单邮件,请向邮件集合添加一个文档,其中具有包含以下内容的 to
字段和 message
字段:
to: ['someone@example.com'],
message: {
subject: 'Hello from Firebase!',
text: 'This is the plaintext section of the email body.',
html: 'This is the <code>HTML</code> section of the email body.',
}
发件人和收件人字段
文档的顶级字段提供电子邮件发件人和收件人信息。可用字段包括:
- from:发件人的电子邮件地址。如果未在文档中指定,则使用配置好的“Default FROM address”参数。
- replyTo:回复电子邮件地址。如果文档中未指定,则使用配置好的“Default REPLY-TO address”参数。
- to:单个收件人电子邮件地址或包含多个收件人电子邮件地址的数组。
- toUids:包含收件人 UID 的数组。
- cc:单个收件人电子邮件地址或包含多个收件人电子邮件地址的数组。
- ccUids:包含 CC 收件人 UID 的数组。
- bcc:单个收件人电子邮件地址或包含多个收件人电子邮件地址的数组。
- bccUids:包含密送收件人 UID 的数组。
- headers:其他标题字段的对象(例如
{"X-Custom-Header": "value", "X-Second-Custom-Header": "value"}
)。
注意:toUids
、ccUids
和 bccUids
选项可以将电子邮件传送到 Cloud Firestore 文档中以用户 UID 作为键的电子邮件地址中。要使用这些收件人选项,您需要为此扩展程序的“用户集合”参数指定一个 Cloud Firestore 集合。然后,此扩展程序可以读取 toUids
、ccUids
和/或 bccUids
字段中指定的每个 UID 的 email
字段。
邮件字段
文档的 message
字段包含电子邮件的原始递送信息。此字段通常应仅由在您自己的服务器或 Cloud Functions 中运行的可信代码填充(请参阅下面的“安全规则和发送电子邮件”部分)。
message
字段的可用属性包括:
- messageId:电子邮件的邮件 ID 标题(如果有)。
- subject:电子邮件的主题。
- text:电子邮件的纯文本内容。
- html:电子邮件的 HTML 内容。
- amp:电子邮件的 AMP4EMAIL 内容。
- attachments:含有附件的数组;支持的 Nodemailer 选项:utf-8 字符串、自定义内容类型、网址、编码字符串、数据 URI 和预先生成的 MIME 节点(请注意,您的电子邮件无权访问云端服务器的文件系统)。
高级用法
了解此扩展程序的高级用法:
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-08-13。
[null,null,["最后更新时间 (UTC):2025-08-13。"],[],[],null,["\u003cbr /\u003e\n\nThe Trigger Email extension (`firestore-send-email`) lets you automatically send\nemails based on documents in a Cloud Firestore collection. Adding a document to\nthe collection triggers this extension to send an email built from the\ndocument's fields. The document's top-level fields specify the email sender and\nrecipients, including `to`, `cc`, and `bcc` options (each supporting UIDs). The\ndocument's `message` field specifies the other email elements, like subject line\nand email body (either plaintext or HTML).\n\nHere's a basic example document write that would trigger this extension: \n\n admin.firestore().collection('mail').add({\n to: 'someone@example.com',\n message: {\n subject: 'Hello from Firebase!',\n html: 'This is an \u003ccode\u003eHTML\u003c/code\u003e email body.',\n },\n })\n\nYou can also optionally configure this extension to render emails using\n[Handlebars](/docs/extensions/official/firestore-send-email/templates) templates.\n\nPre-installation setup\n\nBefore you install the extension, complete these steps:\n\n1. Set up your outgoing mail service.\n\n When you install the Trigger Email extension, you will need to specify the\n connection and authentication details of an SMTP server, which the extension\n uses to send emails. This is typically provided by an email delivery service\n such as Sendgrid, Mailgun, or Mailchimp Transactional Email, but it could\n also be a server you run yourself.\n2. Create an email documents collection.\n\n The Trigger Email extension listens for new documents in a Cloud Firestore\n collection that you specify. When it finds a new document, the extension\n sends an email based on the document's fields. You can use any Cloud Firestore\n collection for this purpose; the examples on this page use a collection\n named `email`.\n3. Set up security rules for your email documents collection.\n\n This extension can be used to trigger email delivery directly from client\n applications. However, you should carefully control client access to the\n collection to avoid potential abuse (you don't want users able to send\n arbitrary emails from your company's address!).\n\n Security rules will vary from application to application, but you should\n always make sure that emails are sent only to intended recipients and\n free-form content is kept to a minimum. Templates can help here---you\n can use security rules to verify that the data being populated to the\n template matches your expectations for what a user should be allowed to\n trigger.\n4. Optional: Set up a users collection.\n\n Under basic usage of this extension, you specify the recipients of an email\n by specifying their email addresses in the `to`, `cc`, and `bcc` fields of\n the message document. As an alternative, if you have a user database in\n Cloud Firestore, you can specify recipients using the users' UIDs. For this\n to work, your users collection must meet these criteria:\n - The collection must be keyed on user IDs. That is, the document ID of each user document in the collection must be the user's Firebase Authentication UID.\n - Each user document must have an `email` field containing the user's email address.\n5. Optional: Set up a templates collection.\n\n You can render emails using [Handlebars templates](https://handlebarsjs.com/).\n To do so, you will need a Cloud Firestore collection to contain your\n templates.\n\n See [Use Handlebars templates with the Trigger Email extension](/docs/extensions/official/firestore-send-email/templates)\n for details.\n\nInstall the extension\n\nTo install the extension, follow the steps on the\n[Install a Firebase Extension](/docs/extensions/install-extensions)\npage. In summary, do one of the following:\n\n- **Firebase console:** Click the following button:\n\n [Install the Trigger Email extension](https://console.firebase.google.com/project/_/extensions/install?ref=firebase/firestore-send-email)\n- **CLI:** Run the following command:\n\n ```\n firebase ext:install firebase/firestore-send-email --project=projectId-or-alias\n ```\n\n \u003cbr /\u003e\n\nWhen you install the extension, you will be prompted to specify your SMTP\nconnection information and the Cloud Firestore collections you set up earlier.\n\nUse the extension\n\nAfter installation, this extension monitors all document writes to the\ncollection you configured. Email is delivered based on the contents of the\ndocument's fields. The top-level fields specify the email's sender and\nrecipients. The `message` field contains the details of the email to deliver,\nincluding the email body.\n\nExample: Send an email\n\nTo send a simple message, add a document to your messages collection with a `to`\nfield and a `message` field with the following content: \n\n to: ['someone@example.com'],\n message: {\n subject: 'Hello from Firebase!',\n text: 'This is the plaintext section of the email body.',\n html: 'This is the \u003ccode\u003eHTML\u003c/code\u003e section of the email body.',\n }\n\nSender and recipient fields\n\nThe top-level fields of the document supply the email sender and recipient\ninformation. Available fields are:\n\n- **from:** The sender's email address. If not specified in the document, uses the configured \"Default FROM address\" parameter.\n- **replyTo:** The reply-to email address. If not specified in the document, uses the configured \"Default REPLY-TO address\" parameter.\n- **to:** A single recipient email address or an array containing multiple recipient email addresses.\n- **toUids:** An array containing the recipient UIDs.\n- **cc:** A single recipient email address or an array containing multiple recipient email addresses.\n- **ccUids:** An array containing the CC recipient UIDs.\n- **bcc:** A single recipient email address or an array containing multiple recipient email addresses.\n- **bccUids:** An array containing the BCC recipient UIDs.\n- **headers:** An object of additional header fields (for example, `{\"X-Custom-Header\": \"value\", \"X-Second-Custom-Header\": \"value\"}`).\n\n**NOTE:** The `toUids`, `ccUids`, and `bccUids` options deliver emails based on\nuser UIDs keyed to email addresses within a Cloud Firestore document. To use\nthese recipient options, you need to specify a Cloud Firestore collection for\nthe extension's \"Users collection\" parameter. The extension can then read the\n`email` field for each UID specified in the `toUids`, `ccUids`, and/or `bccUids` fields.\n\nMessage field\n\nThe `message` field of the document contains raw delivery information for the\nemail. This field should generally only be populated by trusted code running in\nyour own servers or Cloud Functions (refer to the \"Security rules and sending\nemail\" section below).\n\nAvailable properties for the `message` field are:\n\n- **messageId:** A message ID header for the email, if any.\n- **subject:** The subject of the email.\n- **text:** The plaintext content of the email.\n- **html:** The HTML content of the email.\n- **amp:** The [AMP4EMAIL](https://amp.dev/documentation/guides-and-tutorials/learn/email-spec/amp-email-format/) content of the email.\n- **attachments:** An array containing attachment(s); [Nodemailer options](https://nodemailer.com/message/attachments/) supported: utf-8 string, custom content type, URL, encoded string, data URI, and pregenerated MIME node (be aware that your email has no access to the cloud server's file system).\n\nAdvanced use\n\nLearn about more advanced use of this extension:\n\n- [Use Handlebars templates with the Trigger Email extension](/docs/extensions/official/firestore-send-email/templates)\n- [Manage delivery status with the Trigger Email extension](/docs/extensions/official/firestore-send-email/delivery-status)"]]