使用觸發電子郵件額外資訊
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
透過「觸發電子郵件」擴充功能 (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 Transactional Email) 提供,但也可以是您自行執行的伺服器。
建立電子郵件文件集合。
Trigger Email 擴充功能會監聽您指定的Cloud Firestore集合中是否有新文件。找到新文件時,擴充功能會根據文件欄位傳送電子郵件。您可以使用任何 Cloud Firestore 集合來達成這個目的;本頁的範例使用名為 email
的集合。
為電子郵件文件集合設定安全規則。
這個擴充功能可用於直接從用戶端應用程式觸發電子郵件傳送作業。不過,您應謹慎控管用戶端對集合的存取權,以免發生濫用情形 (您不希望使用者能從貴公司的地址傳送任意電子郵件!)。
安全規則會因應用程式而異,但您應一律確保電子郵件只傳送給預期收件者,並盡量減少自由格式內容。範本可派上用場,您可以使用安全性規則,確認填入範本的資料符合您對使用者應允許觸發的項目。
選用:設定使用者集合。
在這個擴充功能的基礎用法中,您可以在訊息文件的 to
、cc
和 bcc
欄位中指定電子郵件地址,藉此指定電子郵件收件者。或者,如果您在 Cloud Firestore 中有使用者資料庫,可以使用使用者的 UID 指定收件者。如要使用這項功能,使用者集合必須符合下列條件:
- 集合必須以使用者 ID 為索引鍵。也就是說,集合中每個使用者文件的文件 ID 必須是使用者的 Firebase Authentication UID。
- 每份使用者文件都必須含有
email
欄位,其中包含使用者的電子郵件地址。
選用:設定範本集合。
您可以使用 Handlebars 範本呈現電子郵件。
如要這麼做,您需要建立 Cloud Firestore 集合來存放範本。
詳情請參閱「使用 Handlebars 範本搭配 Trigger Email 擴充功能」。
安裝擴充功能
如要安裝擴充功能,請按照「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: 寄件者的電子郵件地址。如未在文件中指定,則會使用設定的「預設寄件者地址」參數。
- replyTo:回覆用的電子郵件地址。如未在文件中指定,則會使用設定的「預設回覆地址」參數。
- to:單一收件者電子郵件地址,或包含多個收件者電子郵件地址的陣列。
- toUids:包含收件者 UID 的陣列。
- cc:單一收件者電子郵件地址,或包含多個收件者電子郵件地址的陣列。
- ccUids:包含副本收件者 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 節點 (請注意,您的電子郵件無法存取雲端伺服器的檔案系統)。
進階用途
進一步瞭解如何進階使用這項擴充功能:
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-07-25 (世界標準時間)。
[null,null,["上次更新時間:2025-07-25 (世界標準時間)。"],[],[],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)"]]