From 3bf1952c1dad9d479bb8b22031b06a7434d37c37 Mon Sep 17 00:00:00 2001 From: dujinkim Date: Thu, 26 Jun 2025 02:58:20 +0000 Subject: (최겸) 공통 > 이메일 템플릿 관리 개발 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/mail/mailer.ts | 48 +++++++++--------------------------------------- 1 file changed, 9 insertions(+), 39 deletions(-) (limited to 'lib/mail/mailer.ts') diff --git a/lib/mail/mailer.ts b/lib/mail/mailer.ts index 3474a373..329e2e52 100644 --- a/lib/mail/mailer.ts +++ b/lib/mail/mailer.ts @@ -15,48 +15,18 @@ const transporter = nodemailer.createTransport({ }, }); -// // Handlebars 템플릿 로더 함수 -// function loadTemplate(templateName: string, data: Record) { -// const templatePath = path.join(process.cwd(), 'lib', 'mail', 'templates', `${templateName}.hbs`); -// const source = fs.readFileSync(templatePath, 'utf8'); -// const template = handlebars.compile(source); -// return template(data); -// } -function applyLayout(layoutName: string, content: string, context: Record) { - const layoutPath = path.join(process.cwd(), 'lib', 'mail', 'layouts', `${layoutName}.hbs`); - const layoutSource = fs.readFileSync(layoutPath, 'utf8'); - const layoutTemplate = handlebars.compile(layoutSource); - return layoutTemplate({ ...context, body: content }); -} - -// Partials 자동 등록 -function registerPartials() { - const partialsDir = path.join(process.cwd(), 'lib', 'mail', 'partials'); - const filenames = fs.readdirSync(partialsDir); - - filenames.forEach((filename) => { - const name = path.parse(filename).name; - const filepath = path.join(partialsDir, filename); - const source = fs.readFileSync(filepath, 'utf8'); - handlebars.registerPartial(name, source); // {{> header }}, {{> footer }} - }); -} - - -// 템플릿 불러오기 + layout/partials 적용 -function loadTemplate(templateName: string, context: Record, layout = 'base') { - registerPartials(); - +// 템플릿 로더 함수 - 단순화된 버전 +function loadTemplate(templateName: string, data: Record) { const templatePath = path.join(process.cwd(), 'lib', 'mail', 'templates', `${templateName}.hbs`); const source = fs.readFileSync(templatePath, 'utf8'); const template = handlebars.compile(source); - - const content = template(context); // 본문 먼저 처리 - return applyLayout(layout, content, context); // base.hbs로 감싸기 + return template(data); } -handlebars.registerHelper('t', function(key: string, options: any) { - // options.hash에는 Handlebars에서 넘긴 named parameter들(location=location 등)이 들어있음 - return i18next.t(key, options.hash || {}); - }); + +// i18next 헬퍼 등록 +handlebars.registerHelper('t', function(key: string, options: { hash?: Record }) { + // options.hash에는 Handlebars에서 넘긴 named parameter들이 들어있음 + return i18next.t(key, options.hash || {}); +}); export { transporter, loadTemplate }; \ No newline at end of file -- cgit v1.2.3