Trigger Email 拡張機能の使用
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
Trigger Email 拡張機能(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 テンプレートを使用してメールを生成することもできます。
インストール前の設定
この拡張機能をインストールする前に、次の手順を行ってください。
送信メールサービスを設定します。
Trigger Email 拡張機能をインストールするときに SMTP サーバーの接続と認証に関する情報を指定する必要があります。この拡張機能はそれらの情報を使用してメールを送信します。通常、メール送信機能は Sendgrid、Mailgun、Mailchimp Transactional Email などのメール配信サービスによって提供されますが、自社で運用するサーバーも使用できます。
メール ドキュメント コレクションを作成します。
Trigger Email 拡張機能は、指定された Cloud Firestore コレクション内の新しいドキュメントをリッスンします。新しいドキュメントを検出すると、この拡張機能はドキュメントのフィールドに基づいてメールを送信します。そのために任意の Cloud Firestore コレクションを使用できます。このページの例では、email
という名前のコレクションを使用しています。
メール ドキュメント コレクションのセキュリティ ルールを設定します。
この拡張機能を使用すると、クライアント アプリケーションからメール配信を直接トリガーできます。ただし、不正使用を防止するため、コレクションへのクライアント アクセスは慎重に管理する必要があります(貴社の企業アドレスでユーザーが任意のメールを送信できるようでは問題になります)。
セキュリティ ルールはアプリケーションごとに異なりますが、メールは必ず目的の受信者に対してのみ送信され、自由形式のコンテンツは最小限に抑えるようにする必要があります。ここでテンプレートが役に立ちます。セキュリティ ルールを使用すると、テンプレートに入力されるデータと、ユーザーにトリガーが許可されるべき処理とが一致していることを確認できます。
省略可: ユーザー コレクションを設定します。
この拡張機能の基本的な使用法では、メッセージ ドキュメントの to
、cc
、bcc
フィールドにメールアドレスを指定することによって、メールの受信者を指定します。別の方法として、Cloud Firestore にユーザー データベースがある場合は、ユーザーの UID を使用して受信者を指定することもできます。この方法を使用するには、ユーザー コレクションが次の条件を満たしている必要があります。
- コレクションのキーはユーザー ID に基づくものである必要があります。つまり、コレクション内の各ユーザー ドキュメントのドキュメント ID は、ユーザーの Firebase Authentication にする必要があります。
- 各ユーザー ドキュメントには、ユーザーのメールアドレスを含む
email
フィールドが必要です。
省略可: テンプレート コレクションを設定します。
Handlebars テンプレートを使用してメールを作成できます。これを行うには、テンプレートが含まれている Cloud Firestore コレクションが必要です。
詳しくは、Trigger Email 拡張機能で Handlebars テンプレートを使用するをご覧ください。
拡張機能をインストールする
この拡張機能をインストールするには、Firebase Extension のインストール ページの手順を実行します。簡単に説明すると、次のいずれかの方法でインストールします。
拡張機能をインストールするときに、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: BCC 受信者の UID を含む配列。
- headers: 追加のヘッダー フィールドのオブジェクト(例:
{"X-Custom-Header": "value", "X-Second-Custom-Header": "value"}
)。
注: toUids
、ccUids
、bccUids
の各オプションを使用すると、ユーザーの UID に基づいてメールを配信します。この UID をキーとして、Cloud Firestore ドキュメント内に指定されたメールアドレスが取得されます。これらの受信者オプションを使用するには、この拡張機能の「Users collection」パラメータで Cloud Firestore コレクションを指定する必要があります。この指定を行うことによって、toUids
、ccUids
、bccUids
フィールドに指定された各 UID の email
フィールドを読み取ることができます。
メッセージ フィールド
ドキュメントの message
フィールドにはメールの具体的な配信情報を指定します。通常、このフィールドのデータ設定は、自社サーバーまたは Cloud Functions で実行される信頼されたコードのみによって行う必要があります(下の「セキュリティ ルールとメールの送信」セクションをご覧ください)。
message
フィールドで使用可能なプロパティは次のとおりです。
- messageId: メールのメッセージ ID ヘッダー(ある場合)。
- subject: メールの件名。
- text: メールの平文コンテンツ。
- html: メールの HTML コンテンツ。
- amp: メールの AMP4EMAIL コンテンツ。
- attachments: 添付ファイルを含む配列。Nodemailer のオプションがサポートされており、次の要素を使用できます。utf-8 文字列、カスタム コンテンツ タイプ、URL、エンコードされた文字列、データ URI、事前生成された MIME ノード(メールからクラウド サーバーのファイル システムにアクセスできないことに注意してください)。
高度な使用方法
以下のページでは、この拡張機能の高度な使用方法を説明しています。
特に記載のない限り、このページのコンテンツはクリエイティブ・コモンズの表示 4.0 ライセンスにより使用許諾されます。コードサンプルは Apache 2.0 ライセンスにより使用許諾されます。詳しくは、Google Developers サイトのポリシーをご覧ください。Java は Oracle および関連会社の登録商標です。
最終更新日 2025-08-13 UTC。
[null,null,["最終更新日 2025-08-13 UTC。"],[],[],null,["# Using the Trigger Email extension\n\n\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----------------------\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---------------------\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-----------------\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\n### Example: 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\n### Sender 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\n### Message 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------------\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)"]]