이메일 트리거 확장 프로그램으로 Handlebars 템플릿 사용
컬렉션을 사용해 정리하기
내 환경설정을 기준으로 콘텐츠를 저장하고 분류하세요.
확장 프로그램을 구성하는 동안 '템플릿 컬렉션' 파라미터를 지정한 경우 이메일용 Handlebars 템플릿을 만들고 관리할 수 있습니다.
템플릿 컬렉션 구조
템플릿 컬렉션에 작성하는 문서에서 템플릿 이름으로 사용할 기억하기 쉬운 ID를 각 문서에 지정합니다.
템플릿 문서에는 다음과 같은 필드가 포함될 수 있습니다.
- subject: 이메일의 제목에 대한 템플릿 문자열입니다.
- text: 이메일의 일반 텍스트 콘텐츠에 대한 템플릿 문자열입니다.
- html: 이메일의 HTML 콘텐츠에 대한 템플릿 문자열입니다.
- amp: 이메일의 AMP4EMAIL 콘텐츠에 대한 템플릿 문자열입니다.
- attachments: 템플릿 문자열을 값으로 포함하는 첨부파일 배열입니다. 지원되는 Nodemailer 옵션에는 utf-8 문자열, 커스텀 콘텐츠 유형, URL, 인코딩된 문자열, 데이터 URI, 사전 생성된 MIME 노드가 있습니다(이메일은 클라우드 서버 파일 시스템에 액세스할 수 없음).
예시 템플릿에는 following
ID 및 다음과 같은 콘텐츠가 포함될 수 있습니다.
{
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}}"
}
]
}
템플릿을 사용하여 이메일 보내기
템플릿을 사용하여 이메일을 전송하려면 메일 컬렉션에 문서를 추가할 때 name
및 data
속성이 있는 template
필드를 포함합니다. 예를 들어 위의 following
템플릿을 사용하는 경우 다음과 같습니다.
admin
.firestore()
.collection("MAIL_COLLECTION")
.add({
toUids: ["abc123"],
template: {
name: "following",
data: {
username: "ada",
name: "Ada Lovelace",
imagePath: "https://example.com/path/to/file/image-name.jpg"
},
},
});
템플릿 부분
템플릿 문서에서 {partial: true}
를 지정하여 재사용 가능한 부분을 사용해 템플릿을 작성할 수 있습니다. 각 표준 데이터 필드(subject
, html
, text
, amp
)는 자체 환경에서만 부분적으로 사용되는 것으로 정의됩니다. 예를 들어 footer
부분에는 다음과 같은 데이터가 있을 수 있습니다.
{
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"
}
다른 템플릿에서는 다음과 같이 이름(문서 ID)을 참조하여 해당 부분을 포함합니다.
<p>This is my main template content, but it will use a common footer.</p>
{{> footer }}
달리 명시되지 않는 한 이 페이지의 콘텐츠에는 Creative Commons Attribution 4.0 라이선스에 따라 라이선스가 부여되며, 코드 샘플에는 Apache 2.0 라이선스에 따라 라이선스가 부여됩니다. 자세한 내용은 Google Developers 사이트 정책을 참조하세요. 자바는 Oracle 및/또는 Oracle 계열사의 등록 상표입니다.
최종 업데이트: 2025-08-12(UTC)
[null,null,["최종 업데이트: 2025-08-12(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 }}"]]