summaryrefslogtreecommitdiff
path: root/lib/mail/sendEmail.ts
blob: c4171082a12bdc4f6597a7122e8be7899a43d589 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import { useTranslation } from '@/i18n';
import { transporter, loadTemplate } from './mailer';
import handlebars from 'handlebars';

interface SendEmailOptions {
  to: string;
  subject: string;
  template: string; // 템플릿 파일명(확장자 제외)
  context: Record<string, any>; // 템플릿에 주입할 데이터
  attachments?: {   // NodeMailer "Attachment" 타입
    filename?: string
    path?: string
    content?: Buffer | string
    // ...
  }[]
}

export async function sendEmail({ to, subject, template, context,  attachments = []}: SendEmailOptions) {
  const { t, i18n } = await useTranslation(context.language ?? "en", "translation");

  handlebars.registerHelper("t", function (key: string, options: any) {
    // 여기서 i18n은 로컬 인스턴스
    return i18n.t(key, options.hash || {});
  });

  const html = loadTemplate(template, context);

  await transporter.sendMail({
    from: `"${process.env.Email_From_Name}" <${process.env.Email_From_Address}>`,
    to,
    subject,
    html,
    attachments
  });
}