קל לארגן דפים בעזרת אוספים
אפשר לשמור ולסווג תוכן על סמך ההעדפות שלך.
התוסף Trigger Email (firestore-send-email) מאפשר לשלוח אימיילים באופן אוטומטי על סמך מסמכים בCloud Firestore אוסף. הוספת מסמך לאוסף מפעילה את התוסף הזה לשליחת אימייל שנבנה מהשדות של המסמך. השדות ברמה העליונה של המסמך מציינים את השולח ואת הנמענים של האימייל, כולל האפשרויות to, cc ו-bcc (כל אחת מהן תומכת במזהים ייחודיים). השדה 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
אוסף שאתם מציינים. כשהתוסף מוצא מסמך חדש, הוא שולח אימייל על סמך השדות במסמך. אפשר להשתמש בכל אוסף למטרה הזו. בדוגמאות שבדף הזה נעשה שימוש באוסף בשם email.Cloud Firestore
מגדירים כללי אבטחה לאוסף מסמכי האימייל.
אפשר להשתמש בתוסף הזה כדי להפעיל שליחת אימיילים ישירות מאפליקציות לקוח. עם זאת, חשוב לשלוט בקפידה בגישת הלקוח לאוסף כדי למנוע שימוש לרעה (אתם לא רוצים שמשתמשים יוכלו לשלוח אימיילים שרירותיים מכתובת החברה שלכם!).
כללי האבטחה משתנים מאפליקציה לאפליקציה, אבל תמיד צריך לוודא שהאימיילים נשלחים רק לנמענים המיועדים, ושהתוכן החופשי מוגבל למינימום. תבניות יכולות לעזור כאן – אפשר להשתמש בכללי אבטחה כדי לוודא שהנתונים שמאכלסים את התבנית תואמים לציפיות שלכם לגבי הפעולות שמותר למשתמש להפעיל.
אופציונלי: הגדרת אוסף משתמשים.
בשימוש בסיסי בתוסף הזה, מציינים את הנמענים של האימייל על ידי הזנת כתובות האימייל שלהם בשדות to, cc ו-bcc של מסמך ההודעה. לחלופין, אם יש לכם מסד נתונים של משתמשים ב-Cloud Firestore, אתם יכולים לציין נמענים באמצעות מזהי המשתמשים (UID). כדי שהתכונה הזו תפעל, אוסף המשתמשים שלכם צריך לעמוד בקריטריונים הבאים:
האיסוף צריך להתבסס על מזהי משתמשים. כלומר, מזהה המסמך של כל מסמך משתמש באוסף חייב להיות Firebase Authentication UID של המשתמש.
לכל מסמך משתמש צריך להיות שדה email שמכיל את כתובת האימייל של המשתמש.
אופציונלי: הגדרת אוסף תבניות.
אפשר לעבד הודעות אימייל באמצעות תבניות Handlebars.
כדי לעשות זאת, תצטרכו Cloud Firestore אוסף שיכיל את התבניות שלכם.
כשמתקינים את התוסף, מתבקשים לציין את פרטי החיבור ל-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: מערך שמכיל את מזהי המשתמשים של הנמענים שמופיעים בשדה 'עותק'.
bcc: כתובת אימייל אחת של נמען או מערך שמכיל כמה כתובות אימייל של נמענים.
bccUids: מערך שמכיל את מזהי ה-UID של הנמענים שהועתקו לעותק מוסתר.
headers: אובייקט של שדות כותרת נוספים (לדוגמה, {"X-Custom-Header": "value", "X-Second-Custom-Header": "value"}).
הערה: האפשרויות toUids, ccUids ו-bccUids שולחות אימיילים על סמך מזהי משתמש (UID) שמשויכים לכתובות אימייל במסמך Cloud Firestore. כדי להשתמש באפשרויות האלה של נמענים, צריך לציין אוסף Cloud Firestore לפרמטר 'אוסף משתמשים' של התוסף. התוסף יכול לקרוא את השדה email לכל UID שצוין בשדות toUids, ccUids או bccUids.
שדה הודעה
השדה message במסמך מכיל מידע גולמי על המסירה של האימייל. בדרך כלל, רק קוד מהימן שפועל בשרתים שלכם או ב-Cloud Functions אמור לאכלס את השדה הזה (ראו את הקטע 'כללי אבטחה ושליחת אימייל' בהמשך).
המאפיינים הזמינים בשדה message הם:
messageId: כותרת של מזהה הודעה לאימייל, אם יש כזו.
attachments: מערך שמכיל קבצים מצורפים. האפשרויות הנתמכות של Nodemailer הן: מחרוזת utf-8, סוג תוכן מותאם אישית, כתובת URL, מחרוזת מקודדת, URI של נתונים וצומת MIME שנוצר מראש (חשוב לזכור שלא ניתן לגשת מאימייל למערכת הקבצים של שרת הענן).
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 2025-07-25 (שעון UTC)."],[],[],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)"]]