Sử dụng mẫu Tay cầm cùng với phần mở rộng Email kích hoạt
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Nếu đã chỉ định một tham số "Templates collection" (Tập hợp mẫu) trong quá trình định cấu hình tiện ích, bạn có thể tạo và quản lý các mẫu Handlebars cho email của mình.
Cấu trúc bộ sưu tập mẫu
Đặt cho mỗi tài liệu một mã nhận dạng dễ nhớ mà bạn dùng làm tên mẫu trong các tài liệu bạn ghi vào bộ sưu tập mẫu.
Tài liệu mẫu có thể bao gồm bất kỳ trường nào sau đây:
subject: Chuỗi mẫu cho tiêu đề của email.
text: Chuỗi mẫu cho nội dung văn bản thuần tuý của email.
html: Một chuỗi mẫu cho nội dung HTML của email.
amp: Một chuỗi mẫu cho nội dung AMP4EMAIL của email.
attachments: Một mảng các tệp đính kèm có chuỗi mẫu làm giá trị; Các lựa chọn của Nodemailer được hỗ trợ: chuỗi utf-8, loại nội dung tuỳ chỉnh, URL, chuỗi được mã hoá, URI dữ liệu và nút MIME được tạo trước (lưu ý rằng email của bạn không có quyền truy cập vào hệ thống tệp của máy chủ đám mây).
Ví dụ: một mẫu có thể có mã nhận dạng là following và nội dung như sau:
{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}}"}]}
Gửi email bằng mẫu
Để gửi email bằng mẫu, khi thêm tài liệu vào bộ sưu tập thư, hãy thêm trường template có thuộc tính name và data. Ví dụ: sử dụng mẫu following ở trên:
Bạn có thể tạo mẫu bằng cách sử dụng các phần có thể dùng lại bằng cách chỉ định {partial: true} trong tài liệu mẫu. Mỗi trường dữ liệu tiêu chuẩn (subject, html, text và amp) sẽ được xác định là một phần chỉ được dùng trong môi trường riêng. Ví dụ: một phần có tên là footer có thể có dữ liệu như:
{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"}
Trong một mẫu khác, hãy thêm mẫu một phần bằng cách tham chiếu tên của mẫu đó (mã nhận dạng tài liệu):
<p>This is my main template content, but it will use a common footer.</p>
{{> footer }}
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-09-06 UTC."],[],[],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 }}"]]