import { env } from "@pram/env/server"; import nodemailer from "nodemailer"; const smtpConfigured = env.SMTP_HOST && env.SMTP_USER && env.SMTP_PASS && env.SMTP_FROM; if (!smtpConfigured) { console.warn( "[email] SMTP not configured — email features (verification, password reset) will be disabled. Set SMTP_HOST, SMTP_USER, SMTP_PASS, SMTP_FROM in apps/server/.env to enable." ); } const smtpPort = Number(env.SMTP_PORT) || 587; const transporter = smtpConfigured ? nodemailer.createTransport({ host: env.SMTP_HOST, port: smtpPort, secure: smtpPort === 465, auth: { user: env.SMTP_USER, pass: env.SMTP_PASS, }, }) : null; const subjects: Record = { "email-verification": "Verifikasi Email Pram", "forget-password": "Reset Password Pram", }; const messages: Record = { "email-verification": "Gunakan kode berikut untuk memverifikasi email Anda:", "forget-password": "Gunakan kode berikut untuk mereset password Anda:", }; type SendOtpEmailProps = { to: string; otp: string; type: "email-verification" | "forget-password"; }; export async function sendOtpEmail({ to, otp, type }: SendOtpEmailProps) { console.log(`\n======================================================`); console.log(`[BYPASS EMAIL] Kode OTP untuk ${to} adalah: ${otp}`); console.log(`======================================================\n`); if (!transporter) { console.warn(`[email] Skipping OTP email to ${to} — SMTP not configured.`); return; } const subject = subjects[type] || "Kode Verifikasi Pram"; const message = messages[type] || "Kode verifikasi Anda:"; transporter.sendMail({ from: env.SMTP_FROM, to, subject, html: `

Pram

${message}

${otp}

Kode berlaku selama 5 menit. Jangan bagikan kode ini kepada siapapun.

`, }).catch((err) => { console.error("[email] Error sending email (background):", err.message); }); }